malloc_zone_t *
malloc_zone_from_ptr(const void *ptr)
{
	OSLog("malloc_zone_from_ptr(%p): searching ... ", ptr);

	lll_lock(&malloc_lock);

	OSMemoryZone* osm = NULL;
	for (int i = 0; i < MAX_ZONES; i++) {
		OSMemoryZone* tosm = &zones[i];

		if (tosm->registred && tosm->memory_space != NULL) {
			if (XXX_mspace_has_pointer(tosm->memory_space, (void*)ptr))
			{
				osm = tosm;
				break;
			}
		}
	}

	lll_unlock(&malloc_lock);

	if (osm == NULL) {
		osm = default_zone;
		OSLog("malloc_zone_from_ptr(%p): no hits, returinig default_zone", ptr);
	}
	else {
		OSLog("malloc_zone_from_ptr(%p): found zone [%p] ", ptr, osm);
	}

	return (malloc_zone_t*)osm;
}
Exemplo n.º 2
0
void _dyld_register_func_for_add_image(AddImageHandler hn) {
	OSLog("_dyld_register_func_for_add_image(%p)", hn);
	sAddHandlers[sAddCount] = hn;
	sAddCount += 1;
	
	lnk::runAddImageHandler(hn);
}
Exemplo n.º 3
0
void
dyld_register_image_state_change_handler(enum dyld_image_states state, bool batch, dyld_image_state_change_handler handler)
{
	OSLog("dyld_register_image_state_change_handler(%d, %d, %p)", state, batch, handler);
	
	sChangeHandlers[sChangeCount].fn = handler;
	sChangeHandlers[sChangeCount].state = state;
	sChangeHandlers[sChangeCount].batch = batch;
	sChangeCount += 1;
	
	lnk::runBatchNotificationsWithHandler(sChangeHandlers[sChangeCount-1]);
}
Exemplo n.º 4
0
	void runAddImageHandler(AddImageHandler fn) {
		Image* im;
		im = lnk::firstImage();
		while (im != NULL) {
			/* XXX: check if mach object */
			MachObject* obj = (MachObject*)im;
			
			OSLog("calling add handler @ %p, image %s", fn, im->getShortName());
			fn(obj->getMachHeader() ,obj->getSlide());
			
			/* move on */
			im = im->nextImage();
		}
	}
void _OSInitializeMemoryZones(void) {
	/* create the default memory zone */

	if (default_zone != NULL) {
		OSHalt("default_zone already present!");
	}

	/* 0xfee1dead is the magic that stops the create thing from creating a mspace */
	default_zone = (OSMemoryZone*)malloc_create_zone(0, 0xfee1dead);
	default_zone->global_zone = TRUE;
	default_zone->basic_zone.zone_name = "DefaultMallocZone";

	OSLog("_OSInitializeMemoryZones: all set, %d zones inited, default zone @ %p", MAX_ZONES, default_zone);
}
Exemplo n.º 6
0
	void runBatchNotificationsWithHandler(ChangeHandler hn)
	{
		OSLog("runBatchNotificationsWithHandler(%d, %d, %p)", hn.state, hn.batch, hn.fn);
		
		
		dyld_image_info infos[1024];
		uint32_t count = 0;
		
		Image* im;
		im = lnk::firstImage();
		while (im != NULL) {
			if (im->getImageType() != kImageTypeMachO)
			{
				lnk::halt("(im->getImageType() != kImageTypeMachO)");
			}
			
			/* XXX: check if mach object */
			MachObject* obj = (MachObject*)im;
			
			infos[count].imageLoadAddress = obj->getMachHeader();
			infos[count].imageFilePath = "";
			
			/* move on */
			im = im->nextImage();
			count += 1;
		}

		/* main image needs to be here too */
		MachObject* obj = (MachObject*)(lnk::mainExecutableImage());
		infos[count].imageLoadAddress = obj->getMachHeader();
		infos[count].imageFilePath = "";
		count += 1;
		
		dyld_image_state_change_handler fn = hn.fn;
		fn(dyld_image_state_bound, count, infos);
	}
/*
	Abstract.
 */
malloc_zone_t *
malloc_create_zone(vm_size_t start_size, unsigned flags) {
	OSMemoryZone* osm = _OSGetFirstAvailableMemoryZone();

	/* mkay, get default impls */
	osm->basic_zone.calloc = (void*)Impl_malloc_zone_calloc;
	osm->basic_zone.free = (void*)Impl_malloc_zone_free;
	osm->basic_zone.malloc = (void*)Impl_malloc_zone_malloc;
	osm->basic_zone.valloc = (void*)Impl_malloc_zone_valloc;
	osm->basic_zone.memalign = (void*)Impl_malloc_zone_memalign;
	osm->basic_zone.realloc = (void*)Impl_malloc_zone_realloc;

	if (flags != 0xfee1dead) {
		/* make a mspace */
		osm->memory_space = create_mspace(start_size, FALSE);
	}

	OSLog("malloc_create_zone: created zone {size=%d, space=%p, addr=%p}",
		  start_size,
		  osm->memory_space,
		  osm);

	return (malloc_zone_t*)osm;
}
Exemplo n.º 8
0
void _dyld_register_func_for_remove_image(void (*func)(const struct mach_header* mh, intptr_t vmaddr_slide)) {
	OSLog("_dyld_register_func_for_remove_image(%p)", func);
}
Exemplo n.º 9
0
	/*
		Entry point for the linker.
		This is called from the linker bootstrapping code, after the
		critical bits have been initialized.
	 */
	uintptr_t
	_main(const macho_header* mainExecutableMH,
		  uintptr_t mainExecutableSlide,
		  int argc,
		  const char* argv[],
		  const char* envp[],
		  const char* apple[])
	{	
		char* lolo = (char*)0x80000000;
		lnk::log("WeirdPages: %c%c%c%c ", lolo[0], lolo[1], lolo[2], lolo[3]);
		
		lnk::log("size_t: %d", sizeof(size_t));
		
		/*
			Set up global pointers.
			XXX: I need to actually fix this.
		 */
		int i = 0;
		while (i < argc)
		{
			gArgv[i] = (char*)argv[i];
			i++;
		}
		gArgv[i] = (char*)NULL;
		gArgc = argc;
		
		/*
		 * Start the embedded runtime.
		 * Do not try to load anything before this is run.
		 */
		OSInitializeMainRuntime();
		
		OSLog("mach_task_self(): %d", mach_task_self());
		
		//elfTest();
		
		kern_return_t ret;
		mach_port_name_t pr;
		int dd = 0;
		
		ret = task_for_pid(mach_task_self(), getpid(), &pr);
		OSLog("task_for_pid(): ret: %d, port: %d", ret, pr);
		
		ret = pid_for_task(pr, &dd);
		OSLog("pid_for_task(): ret: %d, pid: %d", ret, dd);
		
		vm_address_t addr = 0x70000000;
		ret = vm_allocate(mach_task_self(), &addr, 4096, false);
		
		uint32_t* tt = (uint32_t*)addr;
		
		lnk::log("vm_allocate(): ret: %d, addr: %p, deref: %p", ret, addr, *tt);
		
		uint32_t time0;
		uint32_t time1;
		uint32_t time2;
		uint32_t time3;
		
		time0 = get_u_time(NULL);
		
		/*
		 * Set up the first image in the chain.
		 * The first image is the image of the core framework.
		 * This image contains a significant portion of the linkable libSystem code.
		 *
		 * The linker image is presented as 'libSystem'
		 */
		sDylinker =
		MachObject::instantiateFromMemory("libSystem.B.dylib", lnk::lnkHeader(), lnk::lnkSlide());
		sDylinker->init();
		sImageCount++;
		sFirstImage = (Image*)sDylinker;
		
		/*
		 * Now, load the auxiliary libraries.
		 */
		lnk::loadAuxiliarySystemLibraries();
		
		/*
		 * Set up the main executable's object.
		 * This object is NOT added to the chain.
		 */
		sMainExecutable =
		MachObject::instantiateFromMemory("MainImage", mainExecutableMH, 0);
		sMainExecutable->init();
		sMainHeader = mainExecutableMH;
		
		time1 = get_u_time(NULL);

		/*
		 * cas
		 */
		cas::get()->init();
		
		/*
		 * Bind the symbols for everything else.
		 */
		lnk::bindChain();
		
		/*
		 * Bind the symbols for the main executable.
		 */
		sMainExecutable->doBindSymbols();
		
		/*
		 * Run the initializers for the rest of libs that were just linked
		 * in by the core framework thing. This can only run after stuff has
		 * been linked in.
		 */
		OSInitializeAuxiliaryRuntime();
		
		time2 = get_u_time(NULL);
		
		/*
		 * Now run the initializers (constructors) for all loaded images.
		 */
		lnk::initializeChain();
		
		/*
		 * At last, run the initializers in the main image.
		 */
		sMainExecutable->doInitialize();
		
		/*
		 * Some test stuff is in Tester.c. Call it.
		 */
		TesterStart();
	
		time3 = get_u_time(NULL);
		
		lnk::log("time: [load: %d, bind: %d, init: %d]", time1 - time0, time2 - time1, time3 - time2);
		
		/*
		 *  Execute the main image.
		 */
		if (sVerboseLnkLog) {
			lnk::log("starting '%s' {pc=%p} ...", sMainExecutable->getShortName(), sMainExecutable->getMain());
		}
		
		/*
		 * See 'crt.s'.
		 *
		 * This function calls the entry point in the main image.
		 * It should never return. At least, I don't think so.
		 * Remind me to look into this, it might need an _exit call.
		 */
		_call_main(gArgc, gArgv, sMainExecutable->getMain());
		
		return 0;
	}
Exemplo n.º 10
0
void malloc_set_zone_name(malloc_zone_t *zone, const char *name) {
	OSLog("malloc_set_zone_name(%p, %s)", zone, name);
}
Exemplo n.º 11
0
// OSScheduleThread ***********************************************************
// Start tasks, triggered by hScheduleEvent
void OSScheduleThread(INT32U param)
{   char temp[256];
    INT16S oldIndex, nextIndex;
#if OS_CRITICAL_METHOD == 3
    OS_CPU_SR cpu_sr = TRUE;
#endif
    DBGPRINT(0x00000001, "*** OSScheduleThread First Call\n");

    while (1)
    {   if (WaitForSingleObject(hScheduleEvent, OS_SCHEDULER_TIMEOUT) == WAIT_TIMEOUT)	//Wait for a scheduler event (with timeout)
        {   sprintf(temp, "ERROR: Scheduler timed out in OSScheduleThread  %u --> %u   IF=%u  <-%u<%u<%u<%u<%u<%u<%u<%u<-\n",
                                                OSPrioCur, OSPrioHighRdy, virtualInterruptFlag,
                                                taskLog[0], taskLog[1], taskLog[2], taskLog[3],
                                                taskLog[4], taskLog[5], taskLog[6], taskLog[7]
                                                );
            DBGPRINT(0x00000040, temp);
            MessageBox(NULL, temp, "UCOS-II WIN32", MB_OK | MB_SETFOREGROUND | MB_ICONERROR);	//In case of timeout, display an error message ...
            OSRunning=0;

            SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlBreakHandler, FALSE);
            return;										//... and exit (will return to OSStartHighRdy())
        }
        OSTaskSwHook();									//Call the task switch hook function
        EnterCriticalSection(&criticalSection);

	if (taskSuspended[OSPrioCur]==-1)
	{   DBGPRINT(0x00000001, "Old Task %d marked for deletion - %X - %X\n", OSPrioCur, hTaskThread[OSPrioCur], OSTCBCur);
	    SuspendThread(hTaskThread[OSPrioCur]);
	    ExecuteDeleteTask(OSPrioCur);
	    oldIndex = OSPrioCur;
        } else
        {   oldIndex = GetThreadIndexForTask(OSTCBCur);
        }
	nextIndex= GetThreadIndexForTask(OSTCBHighRdy);
	if (oldIndex==-1 || nextIndex==-1)
	{   printf("ERROR: Internal scheduling error: oldIndex=%d - %d   nextIndex=%d - %d\n",oldIndex, OSTCBCur->OSTCBPrio, nextIndex, OSTCBHighRdy->OSTCBPrio);
	    exit(-1);
	}

	OSTCBCur  = OSTCBHighRdy;							//Now logically switch to the new task
        OSPrioCur = (INT8U) nextIndex;

        idleTrigger = TRUE;
        DBGPRINT(0x00000001, "*** OSScheduleThread from %2u to %2u\n", oldIndex, nextIndex);

        if (OSTCBPrioTbl[oldIndex] == NULL)						//If a task has been deleted
        {   if (hTaskThread[oldIndex])							//... remove it, normally this should never happen
            {   if (TerminateThread(hTaskThread[oldIndex], 0)==FALSE)
            	{   printf("ERROR: Terminate thread for task %d (in OSScheduleThread) WIN32 error code: %Xh\n", oldIndex, GetLastError());
        	}
		CloseHandle(hTaskThread[oldIndex]);
            }
            hTaskThread[oldIndex] = NULL;
            taskSuspended[oldIndex]=0;
	} else
	if (oldIndex != nextIndex && taskSuspended[oldIndex]==0)			//If switching context to a new task ...
        {   if (SuspendThread(hTaskThread[oldIndex])!=0)				//... suspend the thread associated with the current task
                printf("ERROR: SuspendThread() (in OSSCheduleThread) failed with error %Xh - task %d - %d\n", GetLastError(), oldIndex, taskSuspended[oldIndex]);
            taskSuspended[oldIndex]++;							//(update suspend counter to avoid multiple suspends of the same task)
        }

        if (taskSuspended[nextIndex]>0)
        {   taskSuspended[nextIndex]--;							//(updates suspend counter to avoid multiple resumes of the same task)
            if (taskSuspended[nextIndex] < 0)
                taskSuspended[nextIndex]=0;
            if (virtualInterruptFlag==FALSE)
#if OS_CRITICAL_METHOD == 3
                OSEnableInterruptFlag(&cpu_sr);
#else
                OSEnableInterruptFlag();
#endif
            OSLog(nextIndex);
            if (ResumeThread(hTaskThread[nextIndex])!=1)					//... and resume the thread associated with the new task
                printf("ERROR: ResumeThread() (in OSSCheduleThread) failed with error %Xh\n", GetLastError());
        } else
        {   if (virtualInterruptFlag==FALSE)
#if OS_CRITICAL_METHOD == 3
                OSEnableInterruptFlag(&cpu_sr);
#else
                OSEnableInterruptFlag();
#endif
        }
        LeaveCriticalSection(&criticalSection);
    }
}