int main()
{
    unsigned int currentGS = TLS_GET_GS_REG();
    unsigned int currentFS = TLS_GET_FS_REG();

    if ((currentGS == 0) || ((currentGS & 0x4) == 0x4))
    {
        unsigned int ldtEntry = currentGS >> 3;
        unsigned int ldtNextEntry = ldtEntry+1;

        UserDesc tableEntry;
        memset(&tableEntry, 0, sizeof(UserDesc));
        tableEntry.entry_number = ldtNextEntry;
        tableEntry.base_addr = reinterpret_cast<unsigned int>(new unsigned int(0xabcd));
        tableEntry.limit = 0x4000;

        int res = syscall(SYS_modify_ldt, LDT_SET, &tableEntry, sizeof(UserDesc));
        if (res < 0)
        {
            printf("LDT_SET for entry %d failed, code %d, %s\n", ldtNextEntry, errno, strerror(errno) );
            return 0;
        }

        unsigned int newSegVal = (ldtNextEntry << 3) + 7;

        unsigned int GSRes = 0x1;
        unsigned int FSRes = 0x2;
        FarPointer farPtrGS(GSRes, newSegVal);
        FarPointer farPtrFS(FSRes, newSegVal);

        if ((SetGs(&farPtrGS) != GSRes) || (SetFs(&farPtrFS) != FSRes))
        {
            TLS_SET_GS_REG(currentGS);
            TLS_SET_FS_REG(currentFS);
            printf("LFS or LGS failed\n");
            return 0;
        }

        if ((TLS_GET_GS_REG() != newSegVal) || (TLS_GET_FS_REG() != newSegVal))
        {
            TLS_SET_GS_REG(currentGS);
            TLS_SET_FS_REG(currentFS);
            printf("GS or FS wrong value\n");
            return 0;
        }

        if ((GetGsBase() != 0xabcd) || (GetFsBase() != 0xabcd))
        {
            TLS_SET_GS_REG(currentGS);
            TLS_SET_FS_REG(currentFS);
            printf("GS or FS wrong base address\n");
            return 0;
        }
        TLS_SET_GS_REG(currentGS);
        TLS_SET_FS_REG(currentFS);
        printf("LFS and LGS passed successfully\n");
        printf("Base address of GS and FS was synchronuized successfully;\n");

    }
int main (int argc, char *argv[])
{
    int rc;
    UserDesc tr;
    int res;
    
   	tr.entry_number = GdtFirstEntry();
    
    res = syscall(SYS_get_thread_area, &tr);
    if (res != 0)
    {
        printf("SYS_get_thread_area failed with error: %s\n", strerror(errno));
        return 0;
    }

    tr.entry_number = (unsigned)-1;
    tr.base_addr = (unsigned int)new UserInfo();
    ((UserInfo*)(tr.base_addr))->d1 = value1;
    ((UserInfo*)(tr.base_addr))->d2 = value2;
    

    res = syscall(SYS_set_thread_area, &tr);
    
    if (res != 0)
    {
        printf("SYS_set_thread_area failed with error: %s\n", strerror(errno));
        return 0;
    }
    
    TLS_SET_FS_REG((tr.entry_number << 3) + 3);

	if ((GetVal(0) == value1) && (GetVal(4) == value2))
	{       
	    printf("TEST PASSED\n");
	}
	else
	{       
	    printf("TEST FAILED\n");
	}
    return 0;
}
예제 #3
0
int main (int argc, char *argv[])
{
    int rc;
    int i;
    UserDesc tr;
    UserDesc set_tr;
    int syscall_res;

    tr.entry_number = 6;
    syscall_res = syscall(SYS_get_thread_area, &tr);
    if (syscall_res != 0)
    {
        printf("SYS_get_thread_area failed with error: %s\n", strerror(errno));
        return 0;
    }

    tr.entry_number = (unsigned)-1;
    tr.base_addr = (unsigned int)new UserInfo();
    ((UserInfo*)(tr.base_addr))->d1 = value1;
    ((UserInfo*)(tr.base_addr))->d2 = value2;


    syscall_res = syscall(SYS_set_thread_area, &tr);

    if (syscall_res != 0)
    {
        printf("SYS_set_thread_area failed with error: %s\n", strerror(errno));
        return 0;
    }

    TLS_SET_FS_REG((tr.entry_number << 3) + 3);

    /* ======== MovsTest =========== */
    unsigned int mem = 0;
    unsigned int res = MovsTest(offsetof(UserInfo, d2), &mem);
    if (res != value2)
    	printf("The \"movs\" test FAILED\n");
    else
    	printf("The \"movs\" test PASSED successfully\n");

    /* ======== MaskMovqTest =========== */
    
    MaskMovqTest(4, 0x9966);
    if (((UserInfo*)(tr.base_addr))->d2 != 0x9966)
    	printf("The \"maskmovq\" test FAILED\n");
    else
    	printf("The \"maskmovq\" test PASSED successfully\n");
    
    /* ======== PushPopTest =========== */
    
    if (PushPopTest(0, 0x7788) != 0x7788)
    {
        printf("The \"PushPopTest\" test FAILED\n");
    }
    else
    {
        printf("The \"PushPopTest\" test PASSED successfully\n");
    }
    
    /* ======== CallTest =========== */
    ((UserInfo*)(tr.base_addr))->d1 = (unsigned int)CallTestCallback;
    CallTest(0);
    

    return 0;

}