rem: int state;
add: cpu_status state;
context: _create_thread(const char *name, team_id pid, addr entry, void *args, int priority, bool kernel){	struct thread *t;	struct team *p;	int state;	char stack_name[64];	bool abort = false;	t = create_thread_struct(name);	if (t == NULL)		return ENOMEM;	t->priority = priority == -1 ? B_NORMAL_PRIORITY : priority;	// ToDo: revisit this one - the thread might go to early to B_THREAD_SUSPENDED//	t->state = THREAD_STATE_BIRTH;	t->state = B_THREAD_SUSPENDED;	// Is this right?	t->next_state = B_THREAD_SUSPENDED;	state = disable_interrupts();	GRAB_THREAD_LOCK();	// insert into global list	hash_insert(thread_hash, t);	RELEASE_THREAD_LOCK();	GRAB_TEAM_LOCK();	// look at the team, make sure it's not being deleted	p = team_get_team_struct_locked(pid);	if (p != NULL && p->state != TEAM_STATE_DEATH)		insert_thread_into_team(p, t);	else		abort = true;	RELEASE_TEAM_LOCK();	if (abort) {		GRAB_THREAD_LOCK();		hash_remove(thread_hash, t);		RELEASE_THREAD_LOCK();	}	restore_interrupts(state);	if (abort) {		delete_thread_struct(t);		return ERR_TASK_PROC_DELETED;	}	sprintf(stack_name, "%s_kstack", name);	t->kernel_stack_region_id = vm_create_anonymous_region(vm_get_kernel_aspace_id(), stack_name,		(void **)&t->kernel_stack_base, REGION_ADDR_ANY_ADDRESS, KSTACK_SIZE,		REGION_WIRING_WIRED, LOCK_RW|LOCK_KERNEL);	if (t->kernel_stack_region_id < 0)		panic("_create_thread: error creating kernel stack!\n");	t->args = args;	t->entry = entry;	if (kernel) {		// this sets up an initial kthread stack that runs the entry		arch_thread_initialize_kthread_stack(t, &_create_kernel_thread_kentry, &thread_kthread_entry, &thread_kthread_exit);	} else {		// create user stack		// XXX make this better. For now just keep trying to create a stack		// until we find a spot.		t->user_stack_base = (USER_STACK_REGION - STACK_SIZE - ENV_SIZE) + USER_STACK_REGION_SIZE;		while (t->user_stack_base > USER_STACK_REGION) {			sprintf(stack_name, "%s_stack%ld", p->name, t->id);			t->user_stack_region_id = vm_create_anonymous_region(p->_aspace_id, stack_name,				(void **)&t->user_stack_base,				REGION_ADDR_ANY_ADDRESS, STACK_SIZE, REGION_WIRING_LAZY, LOCK_RW);			if (t->user_stack_region_id < 0) {				t->user_stack_base -= STACK_SIZE;			} else {				// we created a region				break;			}		}		if (t->user_stack_region_id < 0)			panic("_create_thread: unable to create user stack!\n");		// copy the user entry over to the args field in the thread struct		// the function this will call will immediately switch the thread into		// user space.		arch_thread_initialize_kthread_stack(t, &_create_user_thread_kentry, &thread_kthread_entry, &thread_kthread_exit);	}	t->state = B_THREAD_SUSPENDED;	return t->id;}