toaruos/kernel/sys/task.c

559 lines
17 KiB
C
Raw Normal View History

2011-12-07 05:46:22 +04:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
2011-12-14 12:29:23 +04:00
* Task Switching and Management Functions
*
*/
#include <system.h>
#include <process.h>
#include <logging.h>
2012-02-06 00:16:59 +04:00
#include <shm.h>
#include <mem.h>
2012-01-25 04:40:25 +04:00
#define TASK_MAGIC 0xDEADBEEF
2011-03-03 10:39:26 +03:00
uint32_t next_pid = 0;
2011-12-14 12:29:23 +04:00
/*
* Clone a page directory and its contents.
* (If you do not intend to clone the contents, do it yourself!)
*
* @param src Pointer to source directory to clone from.
* @return A pointer to a new directory.
*/
page_directory_t *
clone_directory(
page_directory_t * src
) {
2011-12-14 12:29:23 +04:00
/* Allocate a new page directory */
uintptr_t phys;
page_directory_t * dir = (page_directory_t *)kvmalloc_p(sizeof(page_directory_t), &phys);
2011-12-14 12:29:23 +04:00
/* Clear it out */
memset(dir, 0, sizeof(page_directory_t));
2012-02-16 08:56:16 +04:00
dir->ref_count = 1;
2011-12-14 12:29:23 +04:00
/* Calculate the physical address offset */
uintptr_t offset = (uintptr_t)dir->physical_tables - (uintptr_t)dir;
2011-12-14 12:29:23 +04:00
/* And store it... */
dir->physical_address = phys + offset;
uint32_t i;
for (i = 0; i < 1024; ++i) {
2011-12-14 12:29:23 +04:00
/* Copy each table */
2011-12-07 05:46:22 +04:00
if (!src->tables[i] || (uintptr_t)src->tables[i] == (uintptr_t)0xFFFFFFFF) {
continue;
}
if (kernel_directory->tables[i] == src->tables[i]) {
2011-12-14 12:29:23 +04:00
/* Kernel tables are simply linked together */
dir->tables[i] = src->tables[i];
dir->physical_tables[i] = src->physical_tables[i];
} else {
2011-12-14 12:29:23 +04:00
/* User tables must be cloned */
uintptr_t phys;
dir->tables[i] = clone_table(src->tables[i], &phys);
dir->physical_tables[i] = phys | 0x07;
}
}
return dir;
}
/*
* Free a directory and its tables
*/
2012-02-16 08:56:16 +04:00
void release_directory(page_directory_t * dir) {
dir->ref_count--;
if (dir->ref_count < 1) {
uint32_t i;
for (i = 0; i < 1024; ++i) {
if (!dir->tables[i] || (uintptr_t)dir->tables[i] == (uintptr_t)0xFFFFFFFF) {
continue;
}
if (kernel_directory->tables[i] != dir->tables[i]) {
for (uint32_t j = 0; j < 1024; ++j) {
if (dir->tables[i]->pages[j].frame) {
free_frame(&(dir->tables[i]->pages[j]));
}
}
2012-02-16 08:56:16 +04:00
free(dir->tables[i]);
}
}
2012-02-16 08:56:16 +04:00
free(dir);
}
}
void reap_process(process_t * proc) {
list_free(proc->wait_queue);
free(proc->wait_queue);
2012-02-08 12:40:44 +04:00
list_free(proc->signal_queue);
free(proc->signal_queue);
2012-02-17 00:31:40 +04:00
if (proc->image.stack - KERNEL_STACK_SIZE > heap_end) {
kprintf("\033[1;41;32mNot sure what's happening, this seems wrong:\n");
kprintf(" Process' claimed kernel stack is at 0x%x?\033[0m\n", proc->image.stack - KERNEL_STACK_SIZE);
} else {
free((void *)(proc->image.stack - KERNEL_STACK_SIZE));
}
2012-02-16 08:56:16 +04:00
release_directory(proc->thread.page_directory);
2012-02-17 00:31:40 +04:00
/* XXX: Free file descriptors! */
//free((void *)(proc->fds->entries));
2012-02-08 12:40:44 +04:00
2012-02-06 00:16:59 +04:00
shm_release_all(proc);
}
2011-12-14 12:29:23 +04:00
/*
* Clone a page table
*
* @param src Pointer to a page table to clone.
* @param physAddr [out] Pointer to the physical address of the new page table
* @return A pointer to a new page table.
*/
page_table_t *
clone_table(
page_table_t * src,
uintptr_t * physAddr
) {
2011-12-14 12:29:23 +04:00
/* Allocate a new page table */
page_table_t * table = (page_table_t *)kvmalloc_p(sizeof(page_table_t), physAddr);
memset(table, 0, sizeof(page_table_t));
uint32_t i;
for (i = 0; i < 1024; ++i) {
2011-12-14 12:29:23 +04:00
/* For each frame in the table... */
if (!src->pages[i].frame) {
continue;
}
2011-12-14 12:29:23 +04:00
/* Allocate a new frame */
alloc_frame(&table->pages[i], 0, 0);
2011-12-14 12:29:23 +04:00
/* Set the correct access bit */
if (src->pages[i].present) table->pages[i].present = 1;
if (src->pages[i].rw) table->pages[i].rw = 1;
if (src->pages[i].user) table->pages[i].user = 1;
if (src->pages[i].accessed) table->pages[i].accessed = 1;
if (src->pages[i].dirty) table->pages[i].dirty = 1;
2011-12-14 12:29:23 +04:00
/* Copy the contents of the page from the old table to the new one */
copy_page_physical(src->pages[i].frame * 0x1000, table->pages[i].frame * 0x1000);
}
return table;
}
2011-12-14 12:29:23 +04:00
/*
* Install multitasking functionality.
*/
void
tasking_install() {
2011-12-27 05:23:58 +04:00
blog("Initializing multitasking...");
2011-12-14 12:29:23 +04:00
IRQ_OFF; /* Disable interrupts */
LOG(NOTICE, "Initializing multitasking");
2011-12-14 12:29:23 +04:00
/* Initialize the process tree */
initialize_process_tree();
2011-12-14 12:29:23 +04:00
/* Spawn the initial process */
current_process = spawn_init();
2011-12-14 12:29:23 +04:00
/* Initialize the paging environment */
set_process_environment((process_t *)current_process, current_directory);
2011-12-14 12:29:23 +04:00
/* Switch to the kernel directory */
switch_page_directory(current_process->thread.page_directory);
2011-12-14 12:29:23 +04:00
/* Reenable interrupts */
IRQ_RES;
2011-12-27 05:23:58 +04:00
bfinish(0);
}
2011-12-14 12:29:23 +04:00
/*
* Fork.
*
* @return To the parent: PID of the child; to the child: 0
*/
uint32_t
fork() {
IRQ_OFF;
2011-12-14 12:29:23 +04:00
2012-01-25 04:40:25 +04:00
unsigned int magic = TASK_MAGIC;
2011-12-16 01:31:18 +04:00
uintptr_t esp, ebp, eip;
2011-12-14 12:29:23 +04:00
/* Make a pointer to the parent process (us) on the stack */
process_t * parent = (process_t *)current_process;
assert(parent && "Forked from nothing??");
2011-12-14 12:29:23 +04:00
/* Clone the current process' page directory */
page_directory_t * directory = clone_directory(current_directory);
assert(directory && "Could not allocate a new page directory!");
2011-12-14 12:29:23 +04:00
/* Spawn a new process from this one */
process_t * new_proc = spawn_process(current_process);
assert(new_proc && "Could not allocate a new process!");
2011-12-14 12:29:23 +04:00
/* Set the new process' page directory to clone */
set_process_environment(new_proc, directory);
2011-12-14 12:29:23 +04:00
/* Read the instruction pointer */
2011-12-16 01:31:18 +04:00
eip = read_eip();
2011-12-14 12:29:23 +04:00
if (current_process == parent) {
2011-12-14 12:29:23 +04:00
/* Returned as the parent */
2011-12-16 01:31:18 +04:00
/* Verify magic */
2012-01-25 04:40:25 +04:00
assert(magic == TASK_MAGIC && "Bad process fork magic (parent)!");
/* Collect the stack and base pointers */
asm volatile ("mov %%esp, %0" : "=r" (esp));
asm volatile ("mov %%ebp, %0" : "=r" (ebp));
/* Calculate new ESP and EBP for the child process */
if (current_process->image.stack > new_proc->image.stack) {
new_proc->thread.esp = esp - (current_process->image.stack - new_proc->image.stack);
new_proc->thread.ebp = ebp - (current_process->image.stack - new_proc->image.stack);
} else {
new_proc->thread.esp = esp + (new_proc->image.stack - current_process->image.stack);
new_proc->thread.ebp = ebp - (current_process->image.stack - new_proc->image.stack);
}
/* Copy the kernel stack from this process to new process */
memcpy((void *)(new_proc->image.stack - KERNEL_STACK_SIZE), (void *)(current_process->image.stack - KERNEL_STACK_SIZE), KERNEL_STACK_SIZE);
2012-01-29 03:06:07 +04:00
/* Move the syscall_registers pointer */
uintptr_t o_stack = ((uintptr_t)current_process->image.stack - KERNEL_STACK_SIZE);
uintptr_t n_stack = ((uintptr_t)new_proc->image.stack - KERNEL_STACK_SIZE);
uintptr_t offset = ((uintptr_t)current_process->syscall_registers - o_stack);
new_proc->syscall_registers = (struct regs *)(n_stack + offset);
2012-01-25 04:40:25 +04:00
/* Set the new process instruction pointer (to the return from read_eip) */
new_proc->thread.eip = eip;
/* Clear page table tie-ins for shared memory mappings */
assert((new_proc->shm_mappings->length == 0) && "Spawned process had shared memory mappings!");
foreach (n, current_process->shm_mappings) {
shm_mapping_t * mapping = (shm_mapping_t *)n->value;
for (uint32_t i = 0; i < mapping->num_vaddrs; i++) {
/* Get the vpage address (it's the same for the cloned directory)... */
uintptr_t vpage = mapping->vaddrs[i];
assert(!(vpage & 0xFFF) && "shm_mapping_t contained a ptr to the middle of a page (bad)");
/* ...and from that, the cloned dir's page entry... */
page_t * page = get_page(vpage, 0, new_proc->thread.page_directory);
assert(test_frame(page->frame * 0x1000) && "ptr wasn't mapped in?");
/* ...which refers to a bogus frame that we don't want. */
clear_frame(page->frame * 0x1000);
memset(page, 0, sizeof(page_t));
}
}
2012-01-25 04:40:25 +04:00
/* Add the new process to the ready queue */
make_process_ready(new_proc);
IRQ_RES;
/* Return the child PID */
return new_proc->id;
} else {
assert(magic == TASK_MAGIC && "Bad process fork magic (child)!");
/* Child fork is complete, return */
return 0;
}
}
/*
* clone the current thread and create a new one in the same
* memory space with the given pointer as its new stack.
*/
uint32_t
2012-02-16 08:56:16 +04:00
clone(uintptr_t new_stack, uintptr_t thread_func, uintptr_t arg) {
2012-01-25 04:40:25 +04:00
unsigned int magic = TASK_MAGIC;
uintptr_t esp, ebp, eip;
2012-01-29 03:06:07 +04:00
struct regs * r = current_process->syscall_registers;
2012-01-25 04:40:25 +04:00
/* Make a pointer to the parent process (us) on the stack */
process_t * parent = (process_t *)current_process;
assert(parent && "Cloned from nothing??");
page_directory_t * directory = current_directory;
/* Spawn a new process from this one */
process_t * new_proc = spawn_process(current_process);
assert(new_proc && "Could not allocate a new process!");
/* Set the new process' page directory to the original process' */
set_process_environment(new_proc, directory);
2012-02-16 08:56:16 +04:00
directory->ref_count++;
2012-01-25 04:40:25 +04:00
/* Read the instruction pointer */
eip = read_eip();
if (current_process == parent) {
/* Returned as the parent */
/* Verify magic */
assert(magic == TASK_MAGIC && "Bad process fork magic (parent clone)!");
2011-12-14 12:29:23 +04:00
/* Collect the stack and base pointers */
asm volatile ("mov %%esp, %0" : "=r" (esp));
asm volatile ("mov %%ebp, %0" : "=r" (ebp));
2011-12-14 12:29:23 +04:00
/* Calculate new ESP and EBP for the child process */
if (current_process->image.stack > new_proc->image.stack) {
new_proc->thread.esp = esp - (current_process->image.stack - new_proc->image.stack);
new_proc->thread.ebp = ebp - (current_process->image.stack - new_proc->image.stack);
} else {
new_proc->thread.esp = esp + (new_proc->image.stack - current_process->image.stack);
new_proc->thread.ebp = ebp - (current_process->image.stack - new_proc->image.stack);
}
2011-12-14 12:29:23 +04:00
/* Copy the kernel stack from this process to new process */
memcpy((void *)(new_proc->image.stack - KERNEL_STACK_SIZE), (void *)(current_process->image.stack - KERNEL_STACK_SIZE), KERNEL_STACK_SIZE);
2011-12-16 01:31:18 +04:00
2012-01-29 03:06:07 +04:00
/* Move the syscall_registers pointer */
uintptr_t o_stack = ((uintptr_t)current_process->image.stack - KERNEL_STACK_SIZE);
uintptr_t n_stack = ((uintptr_t)new_proc->image.stack - KERNEL_STACK_SIZE);
uintptr_t offset = ((uintptr_t)current_process->syscall_registers - o_stack);
new_proc->syscall_registers = (struct regs *)(n_stack + offset);
2012-02-16 08:56:16 +04:00
/* Set the gid */
if (current_process->group) {
new_proc->group = current_process->group;
} else {
/* We are the session leader */
new_proc->group = current_process->id;
}
2012-02-16 08:56:16 +04:00
2012-02-17 00:38:50 +04:00
new_proc->syscall_registers->ebp = new_stack;
new_proc->syscall_registers->eip = thread_func;
2012-02-16 08:56:16 +04:00
/* Push arg, bogus return address onto the new thread's stack */
new_stack -= sizeof(uintptr_t);
*((uintptr_t *)new_stack) = arg;
new_stack -= sizeof(uintptr_t);
*((uintptr_t *)new_stack) = THREAD_RETURN;
/* Set esp, ebp, and eip for the new thread */
new_proc->syscall_registers->esp = new_stack;
2012-02-17 00:31:40 +04:00
new_proc->syscall_registers->useresp = new_stack;
free(new_proc->fds);
new_proc->fds = current_process->fds;
2012-01-29 03:06:07 +04:00
2011-12-14 12:29:23 +04:00
/* Set the new process instruction pointer (to the return from read_eip) */
new_proc->thread.eip = eip;
2011-12-14 12:29:23 +04:00
/* Add the new process to the ready queue */
make_process_ready(new_proc);
2011-12-16 01:31:18 +04:00
2011-12-14 12:29:23 +04:00
/* Return the child PID */
return new_proc->id;
} else {
2012-02-16 08:56:16 +04:00
assert(magic == TASK_MAGIC && "Bad process clone magic (child clone)!");
2011-12-14 12:29:23 +04:00
/* Child fork is complete, return */
return 0;
}
}
2011-12-14 12:29:23 +04:00
/*
* Get the process ID of the current process.
*
* @return The PID of the current process.
*/
uint32_t
getpid() {
2011-12-14 12:29:23 +04:00
/* Fairly self-explanatory. */
return current_process->id;
}
void
switch_from_cross_thread_lock() {
if (!process_available()) {
IRQS_ON_AND_PAUSE;
}
switch_task(1);
}
2011-12-14 12:29:23 +04:00
/*
* Switch to the next ready task.
*
* This is called from the interrupt handler for the interval timer to
* perform standard task switching.
*/
void
switch_task(uint8_t reschedule) {
if (!current_process) {
2011-12-14 12:29:23 +04:00
/* Tasking is not yet installed. */
return;
}
if (!process_available()) {
2011-12-14 12:29:23 +04:00
/* There is no process available in the queue, do not bother switching */
return;
}
2011-03-30 06:08:56 +04:00
2011-12-14 12:29:23 +04:00
/* Collect the current kernel stack and instruction pointers */
uintptr_t esp, ebp, eip;
asm volatile ("mov %%esp, %0" : "=r" (esp));
asm volatile ("mov %%ebp, %0" : "=r" (ebp));
eip = read_eip();
if (eip == 0x10000) {
2011-12-14 12:29:23 +04:00
/* Returned from EIP after task switch, we have
* finished switching. */
while (should_reap()) {
process_t * proc = next_reapable_process();
2011-12-16 07:08:48 +04:00
if (proc) {
reap_process(proc);
}
}
2012-02-08 12:40:44 +04:00
fix_signal_stacks();
/* XXX: Signals */
if (!current_process->finished) {
if (current_process->signal_queue->length > 0) {
node_t * node = list_dequeue(current_process->signal_queue);
signal_t * sig = node->value;
free(node);
2012-02-09 02:07:54 +04:00
handle_signal((process_t *)current_process, sig);
2012-02-08 12:40:44 +04:00
}
}
return;
}
2011-12-14 12:29:23 +04:00
/* Remember this process' ESP/EBP/EIP */
current_process->thread.eip = eip;
current_process->thread.esp = esp;
current_process->thread.ebp = ebp;
if (reschedule) {
/* And reinsert it into the ready queue */
make_process_ready((process_t *)current_process);
}
2011-12-14 12:29:23 +04:00
/* Switch to the next task */
switch_next();
}
2011-12-14 12:29:23 +04:00
/*
* Immediately switch to the next task.
*
* Does not store the ESP/EBP/EIP of the current thread.
*/
void
switch_next() {
uintptr_t esp, ebp, eip;
2011-12-14 12:29:23 +04:00
/* Get the next available process */
while (!process_available()) {
/* Uh, no. */
return;
}
current_process = next_ready_process();
2011-12-14 12:29:23 +04:00
/* Retreive the ESP/EBP/EIP */
eip = current_process->thread.eip;
esp = current_process->thread.esp;
ebp = current_process->thread.ebp;
2012-01-11 05:14:30 +04:00
/* Validate */
2012-02-09 06:09:28 +04:00
if ((eip < (uintptr_t)&code) || (eip > (uintptr_t)&end)) {
kprintf("[warning] Skipping broken procress %d!\n", current_process->id);
switch_next();
}
if (current_process->finished) {
switch_next();
}
2012-01-11 05:14:30 +04:00
2012-02-17 00:31:40 +04:00
/* Set the page directory */
current_directory = current_process->thread.page_directory;
switch_page_directory(current_directory);
/* Set the kernel stack in the TSS */
set_kernel_stack(current_process->image.stack);
2012-02-09 06:09:28 +04:00
if (current_process->started) {
2012-02-08 12:40:44 +04:00
if (current_process->signal_queue->length > 0) {
current_process->signal_kstack = malloc(KERNEL_STACK_SIZE);
current_process->signal_state.esp = current_process->thread.esp;
current_process->signal_state.eip = current_process->thread.eip;
current_process->signal_state.ebp = current_process->thread.ebp;
2012-02-09 02:07:54 +04:00
memcpy(current_process->signal_kstack, (void *)(current_process->image.stack - KERNEL_STACK_SIZE), KERNEL_STACK_SIZE);
2012-02-08 12:40:44 +04:00
}
2012-02-09 06:09:28 +04:00
} else {
current_process->started = 1;
2012-02-08 12:40:44 +04:00
}
2011-12-14 12:29:23 +04:00
/* Jump, baby, jump */
asm volatile (
"mov %0, %%ebx\n"
"mov %1, %%esp\n"
"mov %2, %%ebp\n"
"mov %3, %%cr3\n"
2011-12-14 12:29:23 +04:00
"mov $0x10000, %%eax\n" /* read_eip() will return 0x10000 */
"jmp *%%ebx"
: : "r" (eip), "r" (esp), "r" (ebp), "r" (current_directory->physical_address)
: "%ebx", "%esp", "%eax");
}
2011-03-30 06:08:56 +04:00
#define PUSH(stack, type, item) stack -= sizeof(type); \
*((type *) stack) = item
2011-12-14 12:29:23 +04:00
/*
* Enter ring 3 and jump to `location`.
*
* @param location Address to jump to in user space
* @param argc Argument count
* @param argv Argument pointers
* @param stack Userspace stack address
*/
2011-03-30 06:08:56 +04:00
void
2011-04-18 02:44:29 +04:00
enter_user_jmp(uintptr_t location, int argc, char ** argv, uintptr_t stack) {
IRQ_OFF;
set_kernel_stack(current_process->image.stack);
/* Push arg, bogus return address onto the new thread's stack */
#if 0
location -= sizeof(uintptr_t);
*((uintptr_t *)location) = arg;
location -= sizeof(uintptr_t);
*((uintptr_t *)location) = THREAD_RETURN;
#endif
PUSH(stack, uintptr_t, argv);
PUSH(stack, int, (uintptr_t)argc);
asm volatile(
2011-04-18 02:44:29 +04:00
"mov %3, %%esp\n"
2012-01-11 05:14:30 +04:00
"pushl $0xDECADE21\n" /* Magic */
2011-12-14 12:29:23 +04:00
"mov $0x23, %%ax\n" /* Segment selector */
"mov %%ax, %%ds\n"
"mov %%ax, %%es\n"
"mov %%ax, %%fs\n"
"mov %%ax, %%gs\n"
2011-12-14 12:29:23 +04:00
"mov %%esp, %%eax\n" /* Stack -> EAX */
"pushl $0x23\n" /* Segment selector again */
"pushl %%eax\n"
2011-12-14 12:29:23 +04:00
"pushf\n" /* Push flags */
"popl %%eax\n" /* Fix the Interrupt flag */
"orl $0x200, %%eax\n"
"pushl %%eax\n"
"pushl $0x1B\n"
2011-12-14 12:29:23 +04:00
"pushl %0\n" /* Push the entry point */
"iret\n"
: : "m"(location), "r"(argc), "r"(argv), "r"(stack) : "%ax", "%esp", "%eax");
}
2011-12-14 12:29:23 +04:00
/*
* Dequeue the current task and set it as finished
*
* @param retval Set the return value to this.
*/
void task_exit(int retval) {
2011-04-09 02:53:52 +04:00
/* Free the image memory */
2012-02-09 05:30:35 +04:00
if (__builtin_expect(current_process->id == 0,0)) {
/* This is probably bad... */
switch_next();
return;
}
2011-12-14 12:29:23 +04:00
current_process->status = retval;
current_process->finished = 1;
wakeup_queue(current_process->wait_queue);
2011-12-14 12:29:23 +04:00
#if 0
/*
* These things should be done by another thread.
*/
2011-12-07 05:46:22 +04:00
#if 0
for (uintptr_t i = 0; i < current_process->image.size; i += 0x1000) {
free_frame(get_page(current_process->image.entry + i, 0, current_process->image.page_directory));
}
#endif
2011-12-14 12:29:23 +04:00
free((void *)(current_process->image.stack - KERNEL_STACK_SIZE));
free((void *)current_process->thread.page_directory);
2012-02-17 00:31:40 +04:00
free((void *)current_process->fds->entries);
2011-12-14 12:29:23 +04:00
free((void *)current_process);
#endif
make_process_reapable((process_t *)current_process);
switch_next();
}
2011-12-14 12:29:23 +04:00
/*
* Call task_exit() and immediately STOP if we can't.
*/
void kexit(int retval) {
task_exit(retval);
STOP;
}