u32 wrapWaitSynchronizationN(u32 nanoseconds1,u32 handles_ptr,u32 handles_count,u32 wait_all,u32 nanoseconds2,u32 out) // TODO: timeouts { bool all_unlocked = true; for (u32 i = 0; i < handles_count; i++) { u32 handle = mem_Read32(handles_ptr + i * 4); handleinfo* hi = handle_Get(handle); if (hi == NULL) { arm11_SetR(1, i); ERROR("handle %08x not found.\n", handle); PAUSE(); #ifdef EXIT_ON_ERROR exit(1); #endif return -1; } if (hi->type >= NUM_HANDLE_TYPES) { // This should never happen. ERROR("handle %08x has non-defined type.\n", handle); PAUSE(); exit(1); } // Lookup actual callback in table. if (handle_types[hi->type].fnWaitSynchronization != NULL) { bool locked = false; handle_types[hi->type].fnWaitSynchronization(hi, &locked); if (!locked && !wait_all) { arm11_SetR(1, i); return 0; } else all_unlocked = false; } else { ERROR("WaitSynchronization undefined for handle-type \"%s\".\n", handle_types[hi->type].name); PAUSE(); arm11_SetR(1, i); //we just say this one is open return 0; } } if(wait_all && all_unlocked) { arm11_SetR(1, handles_count); return 0; } // Put thread in WAITING state if not all handles were unlocked. u32* wait_list = malloc(handles_count*4); mem_Read((u8 *) wait_list, handles_ptr, handles_count * 4); threads_SetCurrentThreadWaitList(wait_list, wait_all, handles_count); return 0; }
u32 svcAcceptSession() { u32 session = arm11_R(0); u32 old_port = arm11_R(1); handleinfo* newhi = handle_Get(old_port); if (newhi == NULL) { ERROR("getting handle.\n"); return 0x0; } u32 newhand = handle_New(HANDLE_TYPE_SERVICE_SERVER, SERVICE_DIRECT); handleinfo* newhi2 = handle_Get(newhand); if (newhi2 == NULL) { ERROR("getting handle.\n"); return 0x0; } newhi2->misc[0] = newhi->misc[1]; //unlock handleinfo* anewhi = handle_Get(newhi->misc[1]); if (anewhi == NULL) { ERROR("getting handle.\n"); return 0x0; } anewhi->misc[0] |= HANDLE_SERV_STAT_OPENING; DEBUG("AcceptSession %08x %08x\n", session, newhi->misc[1]); arm11_SetR(1, newhand); return 0; }
u32 svcCreateThread() { u32 prio = arm11_R(0); u32 ent_pc = arm11_R(1); u32 ent_r0 = arm11_R(2); u32 ent_sp = arm11_R(3); u32 cpu = arm11_R(4); DEBUG("entrypoint=%08x, r0=%08x, sp=%08x, prio=%x, cpu=%x\n", ent_pc, ent_r0, ent_sp, prio, cpu); u32 hand = handle_New(HANDLE_TYPE_THREAD, 0); u32 numthread = threads_New(hand); threads[numthread].priority = prio; threads[numthread].r[0] = ent_r0; threads[numthread].sp = ent_sp; threads[numthread].r15 = ent_pc &~0x1; if (ent_pc & 0x1) { threads[numthread].cpsr = 0x3F; //usermode } else { threads[numthread].cpsr = 0x1F; //usermode } threads[numthread].mode = RESUME; arm11_SetR(1, hand); // r1 = handle_out return 0; }
u32 svcCreateMemoryBlock() //todo ichfly { u32 memblock = arm11_R(0); u32 addr = arm11_R(1); u32 size = arm11_R(2); arm11_SetR(1, 0); //for tests //handle_New(HANDLE_TYPE_SHAREDMEM, 0)); // is this realy what it is I am not sure return 0; }
u32 svcAcceptSession() { s32 session = arm11_R(0); u32 port = arm11_R(1); arm11_SetR(1, handle_New(HANDLE_TYPE_SESSION, port)); DEBUG("AcceptSession %08x %08x\n", session, port); return 0; }
u32 svcGetThreadPriority() { u32 out = arm11_R(0); u32 hand = arm11_R(1); s32 prio = 0; u32 threadid = threads_FindIdByHandle(hand); if (threadid != -1) { DEBUG("Thread Priority : %d\n", threads[threadid].priority); prio = threads[threadid].priority; } arm11_SetR(1, prio); // r1 = prio out return 0; }
u32 srv_InitHandle() { // Create a handle for srv: port. arm11_SetR(1, handle_New(HANDLE_TYPE_PORT, PORT_TYPE_SRV)); eventhandle = handle_New(HANDLE_TYPE_SEMAPHORE, 0); handleinfo* h = handle_Get(eventhandle); if (h == NULL) { DEBUG("failed to get newly created semaphore\n"); PAUSE(); return -1; } h->locked = true; h->misc[0] = 0x10; //there are 0x10 events we know 2 non of them are used here h->misc[1] = 0x10; return 0; }
u32 svcCreateMemoryBlock() // TODO { u32 memblock = arm11_R(0); u32 addr = arm11_R(1); u32 size = arm11_R(2); ERROR("CreateMemoryBlock addr=%08x size=%08x\n",addr,size); u32 handle = handle_New(HANDLE_TYPE_SHAREDMEM, MEM_TYPE_ALLOC); handleinfo* h = handle_Get(handle); if (h == NULL) { DEBUG("failed to get handle\n"); PAUSE(); return -1; } h->misc[0] = addr; h->misc[1] = size; h->misc_ptr[0] = malloc(size); arm11_SetR(1, handle); return 0; }
u32 svcControlMemory() { u32 op = arm11_R(0); u32 addr0 = arm11_R(1); u32 addr1 = arm11_R(2); u32 size = arm11_R(3); u32 perm = arm11_R(4); const char* ops; switch(op & 0xFF) { case 1: ops = "FREE"; break; case 2: ops = "RESERVE"; break; case 3: ops = "COMMIT"; break; case 4: ops = "MAP"; break; case 5: ops = "UNMAP"; break; case 6: ops = "PROTECT"; break; default: ops = "UNDEFINED"; break; } const char* perms; switch(perm) { case 0: perms = "--"; break; case 1: perms = "-R"; break; case 2: perms = "W-"; break; case 3: perms = "WR"; break; case 0x10000000: perms = "DONTCARE"; break; default: perms = "UNDEFINED"; } DEBUG("op=%s %s (%x), addr0=%x, addr1=%x, size=%x, perm=%s (%x)\n", ops, op & CONTROL_GSP_FLAG ? "GSP" : "", op, addr0, addr1, size, perms, perm); PAUSE(); if(addr0 & 0xFFF) return SVCERROR_ALIGN_ADDR; if(addr1 & 0xFFF) return SVCERROR_ALIGN_ADDR; if(size & 0xFFF) return SVCERROR_INVALID_SIZE; if(op == 0x10003) { // FFF680A4 if(addr0 == 0) { // FFF680C4 if(addr1 != 0) return SVCERROR_INVALID_PARAMS; } else if(size == 0) { // FFF680D0 if(addr0 < 0x14000000) return SVCERROR_INVALID_PARAMS; if((addr0+size) >= 0x1C000000) return SVCERROR_INVALID_PARAMS; if(addr1 != 0) return SVCERROR_INVALID_PARAMS; } else { if(addr0 < 0x14000000) return SVCERROR_INVALID_PARAMS; if(addr0 >= 0x1C000000) return SVCERROR_INVALID_PARAMS; if(addr1 != 0) return SVCERROR_INVALID_PARAMS; } } else if(op == 1) { if(size == 0) { // FFF68110 if(addr0 < 0x08000000) // FFF68130 return SVCERROR_INVALID_PARAMS; if(addr0 <= 0x1C000000) return SVCERROR_INVALID_PARAMS; } else { if(addr0 < 0x08000000) return SVCERROR_INVALID_PARAMS; if((addr0+size) <= 0x1C000000) return SVCERROR_INVALID_PARAMS; } } else { if(size == 0) { // FFF68148 if(addr0 < 0x08000000) return SVCERROR_INVALID_PARAMS; if(addr0 >= 0x14000000) return SVCERROR_INVALID_PARAMS; } else { if(addr0 < 0x08000000) return SVCERROR_INVALID_PARAMS; if((addr0+size) >= 0x14000000) return SVCERROR_INVALID_PARAMS; } if(op == 4 || op == 5) { // FFF680E8 if(size == 0) { if(addr1 < 0x100000) // FFF681CC return SVCERROR_INVALID_PARAMS; if(addr1 >= 0x14000000) return SVCERROR_INVALID_PARAMS; } if(addr1 < 0x100000) return SVCERROR_INVALID_PARAMS; if((addr1+size) >= 0x14000000) return SVCERROR_INVALID_PARAMS; } } // ???? switch(op & 0xff) { case 1: case 3: case 4: case 5: case 6: break; default: return SVCERROR_INVALID_OPERATION; } if(size == 0) return 0; //kprocess = *0xFFFF9004; //*(SP+0x10) = kprocess + 0x1c; // ??? /* u32 flags = outaddr & 0xff; if(flags != 1) { if(perms != 0 && perms != 1 && perms != 2 && perms != 3) return SVCERROR_INVALID_OPERATION; } */ /*if ((op&0xF) == 3) //COMMIT { arm11_SetR(1, addr0); // outaddr is in R1 return mem_AddSegment(addr0, size, NULL); }*/ /*if(op == 0x10003) { DEBUG("Mapping GSP heap..\n"); arm11_SetR(1, 0x08000000); // outaddr is in R1 return mem_AddSegment(0x08000000, size, NULL); }*/ if ((op & 0xF) == 0x3 || (op & 0xF) == 0x0) { //COMMIT if ((op & 0x10000) == 0x10000) { //LINEAR if (size > 0x08000000) { //Console.WriteLine("out of linear mem"); return 0xFFFFFFFF; } } if (addr0 != 0) { if ((op & 0x10000) == 0x10000) { //LINEAR addr0 = 0x08000000; } arm11_SetR(1, addr0); // outaddr is in R1 return mem_AddSegment(addr0, size, NULL); } else { if ((op & 0x10000) == 0x10000) { //LINEAR addr0 = 0x14000000 + linearalloced; linearalloced += size; arm11_SetR(1, addr0); // outaddr is in R1 return mem_AddMappingShared(addr0, size, LINEmembuffer); } /*else { addr0 = mallocarm11(0x20000000, 0xFFFFF000, size); }*/ arm11_SetR(1, addr0); // outaddr is in R1 return mem_AddSegment(addr0, size, NULL); } } if ((op & 0xF) == 0x4) { //MAP u8* buffer = mem_rawaddr(addr1, size); if (buffer == 0)return -1; return mem_AddMappingShared(addr0, size, buffer); } if ((op & 0xF) == 0x6) { //Protect we don't protect mem sorry DEBUG("STUBBED!\n"); return 0; } DEBUG("STUBBED!\n"); PAUSE(); /* // FFF6824C r11 = outaddr & 0xFFFFFF; is_ldr = GetKProcessID() == 1 ? 0xFFFFFFFF : 0; r2 = r2 & r11; if(r2 & 0xF00) { r2 = *(kprocess + 0xa0); r11 = (r11 & 0xFFFFF0FF) | (r2 & 0xF00); } if(flags == 3 && !is_ldr) { if(sub_FFF72828(*r10, 1, r5) == 0) return 0xC860180A; } s32 rc = sub_FFF741B4(*(SP+16), (returnval in r1) SP+12, r4, r6, r5, r11, r7); if(rc < 0) { //FFF682F8 if(flags == 1) sub_FFF7A0E8(*r10, 1, r5); } if(flags == 3) sub_FFF7A0E8(*r10, 1, r5); */ return -1; }
u32 svcReplyAndReceive() { s32 index = arm11_R(0); u32 handles = arm11_R(1); u32 handleCount = arm11_R(2); u32 replyTarget = arm11_R(3); DEBUG("svcReplyAndReceive %08x %08x %08x %08x\n", index, handles, handleCount, replyTarget); #ifdef MODULE_SUPPORT for (u32 i = 0; i < handleCount; i++) { DEBUG("%08x\n", mem_Read32(handles+i*4)); handleinfo* h = handle_Get(eventhandle); if (h == NULL) { PAUSE(); return -1; } if (h->type == HANDLE_TYPE_SERVICE) { h->misc[0] |= HANDLE_SERV_STAT_WAITING; h->misc[1] = curprocesshandle; h->misc[2] = threads_GetCurrentThreadHandle(); } } #endif for (u32 i = 0; i < handleCount; i++) { DEBUG("%08x\n", mem_Read32(handles + i * 4)); } /*wrapWaitSynchronizationN(0xFFFFFFFF, handles, handleCount, 0, 0xFFFFFFFF,0); //feed module data here switch (times) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: RESP(0, 0x00160042); RESP(1, 0x0); RESP(2, 0x0); RESP(3, 0x12345); break; case 7: RESP(0, 0x00130042); RESP(1, 0x0); RESP(2, 0x0); RESP(3, handle_New(HANDLE_TYPE_EVENT, 0)); break; default: RESP(0, 0x000C0000); break; } //feed end times++;*/ arm11_SetR(1, 0); return 1; }
u32 svcWaitSynchronizationN() //todo timeout { u32 *handelist; u32 nanoseconds1 = arm11_R(0); u32 handles = arm11_R(1); u32 handlecount = arm11_R(2); u32 waitAll = arm11_R(3); u32 nanoseconds2 = arm11_R(4); bool allunlockde = true; for (u32 i = 0; i < handlecount; i++) { u32 curhandel = mem_Read32(handles + i * 4); handleinfo* hi = handle_Get(curhandel); if (hi == NULL) { ERROR("handle %08x not found.\n", curhandel); PAUSE(); #ifdef exitonerror exit(1); #else return 0; #endif } if (hi->type >= NUM_HANDLE_TYPES) { // This should never happen. ERROR("handle %08x has non-defined type.\n", curhandel); PAUSE(); exit(1); } u32 temp; bool locked = false; // Lookup actual callback in table. if (handle_types[hi->type].fnWaitSynchronization != NULL) { temp = handle_types[hi->type].fnWaitSynchronization(hi, &locked); if (!locked && waitAll == 0) { arm11_SetR(1,i); return 0; } else { allunlockde = false; } } else { ERROR("svcCloseHandle undefined for handle-type \"%s\".\n", handle_types[hi->type].name); PAUSE(); return 0; } } if (waitAll && allunlockde)return 0; handelist = malloc(handlecount*4); mem_Read((u8*)handelist, handles, handlecount * 4); lockcpu(handelist, waitAll, handlecount); return 0; }
u32 svcReplyAndReceive() { s32 index = arm11_R(0); u32 handles = arm11_R(1); u32 handleCount = arm11_R(2); u32 replyTarget = arm11_R(3); DEBUG("svcReplyAndReceive %08x %08x %08x %08x\n", index, handles, handleCount, replyTarget); if (replyTarget) //respond { IPC_debugprint(arm11_ServiceBufferAddress() + 0x80); handleinfo* h2 = handle_Get(replyTarget); if (h2 == NULL) { ERROR("handle not there"); } eventhandle = h2->misc[0]; h2 = handle_Get(eventhandle); if (h2 == NULL) { ERROR("handle not there"); } if (h2->misc[0] & HANDLE_SERV_STAT_SYNCING) { mem_Read(h2->misc_ptr[0], arm11_ServiceBufferAddress() + 0x80, 0x80); //todo h2->misc[0] |= HANDLE_SERV_STAT_ACKING; } } for (u32 i = 0; i < handleCount; i++) { DEBUG("%08x\n", mem_Read32(handles + i * 4)); } /*wrapWaitSynchronizationN(0xFFFFFFFF, handles, handleCount, 0, 0xFFFFFFFF,0); //feed module data here switch (times) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: RESP(0, 0x00160042); RESP(1, 0x0); RESP(2, 0x0); RESP(3, 0x12345); break; case 7: RESP(0, 0x00130042); RESP(1, 0x0); RESP(2, 0x0); RESP(3, handle_New(HANDLE_TYPE_EVENT, 0)); break; default: RESP(0, 0x000C0000); break; }*/ //RESP(0, 0x00010800); //feed end times++; arm11_SetR(1, 0); return 0; }