[tyndur-devel] [Lost] [PATCH v2 10/10] kernel2: Makros fuer aktuellen Thread/Prozess
Antoine Kaufmann ·
On Sat, Dec 20 23:49, Kevin Wolf wrote:
> * kernel2: Makros current_task und current_process verwenden, statt sich von der CPU ausgehend durchzuhangeln
> ---
> src/kernel2/include/tasks.h | 4 ++++
> src/kernel2/src/arch/i386/debug.c | 2 +-
> src/kernel2/src/arch/i386/mm/mm_context.c | 2 +-
> src/kernel2/src/arch/i386/syscall.c | 6 ++----
> src/kernel2/src/interrupts/im.c | 25 ++++++++++++-------------
> src/kernel2/src/mm/shm.c | 4 ++--
> src/kernel2/src/syscalls/misc.c | 2 +-
> src/kernel2/src/syscalls/pm.c | 20 ++++++++++----------
> src/kernel2/src/syscalls/pv.c | 8 +++-----
> src/kernel2/src/syscalls/rpc.c | 27 +++++++++++++--------------
> 10 files changed, 49 insertions(+), 51 deletions(-)
>
> diff --git a/src/kernel2/include/tasks.h b/src/kernel2/include/tasks.h
> index b766b04..2f89065 100644
> --- a/src/kernel2/include/tasks.h
> +++ b/src/kernel2/include/tasks.h
> @@ -39,6 +39,7 @@
> #include <types.h>
> #include <collections.h>
>
> +#include "cpu.h"
> #include "mm.h"
> #include "lock.h"
>
> @@ -193,5 +194,8 @@ void load_init_module(struct multiboot_info* multiboot_info);
> /// Alle weiteren Module an init uebergeben
> void load_multiboot_modules(struct multiboot_info* multiboot_info);
>
> +#define current_thread (cpu_get_current()->thread)
> +#define current_process (cpu_get_current()->thread->process)
Eigentlich hätte ich da noch ein pm_ davor erwartet, aber jo, damit kann
ich leben. ;-)
> +
> #endif //ifndef _TASKS_H_
>
> diff --git a/src/kernel2/src/arch/i386/debug.c b/src/kernel2/src/arch/i386/debug.c
> index 5642643..edb2b82 100644
> --- a/src/kernel2/src/arch/i386/debug.c
> +++ b/src/kernel2/src/arch/i386/debug.c
> @@ -207,5 +207,5 @@ void print_tasks_backtrace(void)
> }
> }
>
> - mmc_activate(&cpu_get_current()->thread->process->context);
> + mmc_activate(¤t_process->context);
> }
> diff --git a/src/kernel2/src/arch/i386/mm/mm_context.c b/src/kernel2/src/arch/i386/mm/mm_context.c
> index 49a2d52..e220857 100644
> --- a/src/kernel2/src/arch/i386/mm/mm_context.c
> +++ b/src/kernel2/src/arch/i386/mm/mm_context.c
> @@ -109,7 +109,7 @@ mmc_context_t mmc_create_kernel_context()
> (uintptr_t) page_directory | PTE_W | PTE_P;
>
> // Die Rueckgabe von mmc_current_context muss von Anfang an korrekt sein
> - cpu_get_current()->thread->process->context = context;
> + current_process->context = context;
>
> //context.page_directory_virt = mmc_automap(&context, context.page_directory,
> // 1, KERNEL_MEM_START, KERNEL_MEM_END, MM_FLAGS_KERNEL_DATA);
> diff --git a/src/kernel2/src/arch/i386/syscall.c b/src/kernel2/src/arch/i386/syscall.c
> index e67af12..268d4e4 100644
> --- a/src/kernel2/src/arch/i386/syscall.c
> +++ b/src/kernel2/src/arch/i386/syscall.c
> @@ -98,10 +98,8 @@ void syscall_arch(machine_state_t* isf)
>
> // 512 Bytes Sicherheitsabstand auf dem Stack, denn wenn der Kernel
> // hier einen Stack Overflow produziert, haben wir ein Problem.
> - if ((void*) (isf->esp - 0x200) <
> - cpu_get_current()->thread->user_stack_bottom)
> - {
> - increase_user_stack_size(cpu_get_current()->thread, 1);
> + if ((void*) (isf->esp - 0x200) < current_thread->user_stack_bottom) {
> + increase_user_stack_size(current_thread, 1);
> }
>
> // Die Syscallhandler werden mit dem Userspace-Stack aufgerufen, damit
> diff --git a/src/kernel2/src/interrupts/im.c b/src/kernel2/src/interrupts/im.c
> index d8e12fc..5057303 100644
> --- a/src/kernel2/src/interrupts/im.c
> +++ b/src/kernel2/src/interrupts/im.c
> @@ -69,17 +69,17 @@ static void handle_exception(interrupt_stack_frame_t* isf, uint8_t int_num)
> asm("mov %%cr2, %0" : "=r" (cr2));
>
> // kprintf("user_stack_bottom = %x\n",
> -// cpu_get_current()->thread->user_stack_bottom);
> +// current_thread->user_stack_bottom);
>
> if (int_num == 0x0e) {
> // Ueberpr??fen ob der Pagefault durch einen Stackoverflow
> // hervorgerufen wurde. Falls ja: Stack vergroessern und weitermachen
> if ((cr2 <= isf->esp + 0x800) && (cr2 >= isf->esp - 0x20)
> - && (cpu_get_current()->thread != NULL)
> - && ((void*) cr2 >= cpu_get_current()->thread->user_stack_bottom - 0x1000000)
> - && ((void*) cr2 < cpu_get_current()->thread->user_stack_bottom))
> + && (current_thread != NULL)
> + && ((void*) cr2 >= current_thread->user_stack_bottom - 0x1000000)
> + && ((void*) cr2 < current_thread->user_stack_bottom))
> {
> - increase_user_stack_size(cpu_get_current()->thread, 1);
> + increase_user_stack_size(current_thread, 1);
> return;
> }
> }
> @@ -90,8 +90,7 @@ static void handle_exception(interrupt_stack_frame_t* isf, uint8_t int_num)
> cpu_get_current()->id, int_num, isf->error_code);
>
> kprintf("PID %d: %s\n",
> - cpu_get_current()->thread->process->pid,
> - cpu_get_current()->thread->process->cmdline);
> + current_process->pid, current_process->cmdline);
> cpu_dump(isf);
> kprintf("\033[0;40m");
>
> @@ -119,7 +118,7 @@ static void handle_exception(interrupt_stack_frame_t* isf, uint8_t int_num)
> interrupt_stack_frame_t* im_handler(interrupt_stack_frame_t* isf)
> {
> // Pointer auf den aktuellen Thread holen
> - pm_thread_t* thread = cpu_get_current()->thread;
> + pm_thread_t* thread = current_thread;
> pm_thread_t* old_thread = thread;
> thread->kernel_stack = (vaddr_t) isf;
> byte int_num = isf->interrupt_number;
> @@ -140,7 +139,7 @@ interrupt_stack_frame_t* im_handler(interrupt_stack_frame_t* isf)
> pm_scheduler_push(thread);
>
> // Einen neuen Thread holen.
> - cpu_get_current()->thread = pm_scheduler_pop();
> + current_thread = pm_scheduler_pop();
>
> // Timer ausloesen, wenn noetig
> timer_notify(timer_ticks);
> @@ -165,7 +164,7 @@ interrupt_stack_frame_t* im_handler(interrupt_stack_frame_t* isf)
>
> im_send_interrupts();
>
> - thread = cpu_get_current()->thread;
> + thread = current_thread;
> if (thread != old_thread) {
> cpu_get_current()->tss.ss0 = 2 << 3;
> cpu_get_current()->tss.esp0 =
> @@ -226,7 +225,7 @@ bool im_add_handler(uint32_t intr, pm_process_t* handler)
> void im_send_interrupts(void)
> {
> uint32_t intr;
> - pm_thread_t* old_thread = cpu_get_current()->thread;
> + pm_thread_t* old_thread = current_thread;
>
> for (intr = 0; intr < IM_NUM_INTERRUPTS; intr++)
> {
> @@ -239,7 +238,7 @@ void im_send_interrupts(void)
> // Dieses Wechseln des Tasks ist wichtig: IRQs werden nur
> // angenommen, wenn der Task sie sich selbst schickt
> // TODO schedule_to_task(intr_handling_task[intr], esp);
> - cpu_get_current()->thread = list_get_element_at(intr_handling_task[intr]->threads, 0);
> + current_thread = list_get_element_at(intr_handling_task[intr]->threads, 0);
>
> if (!fastrpc_irq(intr_handling_task[intr], 0, 0,
> sizeof(intr), (char*) &intr, intr - IM_IRQ_BASE))
> @@ -250,5 +249,5 @@ void im_send_interrupts(void)
> }
> }
>
> - cpu_get_current()->thread = old_thread;
> + current_thread = old_thread;
> }
> diff --git a/src/kernel2/src/mm/shm.c b/src/kernel2/src/mm/shm.c
> index 6953369..72c5eac 100644
> --- a/src/kernel2/src/mm/shm.c
> +++ b/src/kernel2/src/mm/shm.c
> @@ -120,7 +120,7 @@ void* shm_attach(uint32_t id)
> }
>
> shm_proc = calloc(1, sizeof(*shm_proc));
> - shm_proc->process = cpu_get_current()->thread->process;
> + shm_proc->process = current_process;
> shm_proc->vaddr = ret;
> list_push(shm->processes, shm_proc);
>
> @@ -137,7 +137,7 @@ void shm_detach(uint32_t id)
> {
> struct shm_desc* shm = tree_search(shms, id);
> struct shm_process* shm_proc;
> - pm_process_t* current_proc = cpu_get_current()->thread->process;
> + pm_process_t* current_proc = current_process;
> int i;
>
> if (shm == NULL) {
> diff --git a/src/kernel2/src/syscalls/misc.c b/src/kernel2/src/syscalls/misc.c
> index 5095c9b..bd29073 100644
> --- a/src/kernel2/src/syscalls/misc.c
> +++ b/src/kernel2/src/syscalls/misc.c
> @@ -68,5 +68,5 @@ uint64_t syscall_get_tick_count(void)
> */
> void syscall_add_timer(uint32_t timer_id, uint32_t usec)
> {
> - timer_register(cpu_get_current()->thread->process, timer_id, usec);
> + timer_register(current_process, timer_id, usec);
> }
> diff --git a/src/kernel2/src/syscalls/pm.c b/src/kernel2/src/syscalls/pm.c
> index 34e1888..b4f5cf7 100644
> --- a/src/kernel2/src/syscalls/pm.c
> +++ b/src/kernel2/src/syscalls/pm.c
> @@ -69,11 +69,11 @@ pid_t syscall_pm_create_process(vaddr_t start, uid_t uid, const char* cmdline,
> */
> void syscall_pm_exit_process(void)
> {
> - pm_process_t* old_task = cpu_get_current()->thread->process;
> + pm_process_t* old_task = current_process;
> pm_thread_t* thread;
>
> // Task abgeben
> - pm_scheduler_push(cpu_get_current()->thread);
> + pm_scheduler_push(current_thread);
>
> // Genau jetzt muss der Prozess geloescht werden: Der Thread ist nicht mehr
> // aktiv, und er darf auch nicht wieder aktiviert werden.
> @@ -86,7 +86,7 @@ void syscall_pm_exit_process(void)
> cpu_get_current()->tss.esp0 = (uintptr_t) thread->kernel_stack + sizeof(interrupt_stack_frame_t);
> cpu_get_current()->tss.ss0 = 2 << 3;
>
> - cpu_get_current()->thread = thread;
> + current_thread = thread;
> }
>
> /**
> @@ -96,7 +96,7 @@ void syscall_pm_exit_process(void)
> */
> pid_t syscall_pm_get_pid()
> {
> - return cpu_get_current()->thread->process->pid;
> + return current_process->pid;
> }
>
> /**
> @@ -111,7 +111,7 @@ pid_t syscall_pm_get_parent_pid(pid_t pid)
> pm_process_t* process;
>
> if (pid == 0) {
> - process = cpu_get_current()->thread->process;
> + process = current_process;
> } else {
> process = pm_get(pid);
> }
> @@ -130,8 +130,8 @@ pid_t syscall_pm_get_parent_pid(pid_t pid)
> */
> const char* syscall_pm_get_cmdline()
> {
> - const char* cmdline = cpu_get_current()->thread->process->cmdline;
> - if (cpu_get_current()->thread->process->cmdline == NULL) {
> + const char* cmdline = current_process->cmdline;
> + if (cmdline == NULL) {
> return NULL;
> }
> size_t cmdlinesize = strlen(cmdline) + 1;
> @@ -210,7 +210,7 @@ void syscall_init_child_page(pid_t pid, vaddr_t dest, vaddr_t src, size_t size)
> */
> void syscall_pm_sleep(void)
> {
> - pm_thread_t* thread = cpu_get_current()->thread;
> + pm_thread_t* thread = current_thread;
>
> // Den aktuellen Thread an den Scheduler zurueckgeben
> pm_scheduler_push(thread);
> @@ -221,7 +221,7 @@ void syscall_pm_sleep(void)
> cpu_get_current()->tss.esp0 = (uintptr_t) thread->kernel_stack + sizeof(interrupt_stack_frame_t);
> cpu_get_current()->tss.ss0 = 2 << 3;
>
> - cpu_get_current()->thread = thread;
> + current_thread = thread;
> }
>
> /**
> @@ -230,7 +230,7 @@ void syscall_pm_sleep(void)
> */
> void syscall_pm_wait_for_rpc(void)
> {
> - pm_thread_t* thread = cpu_get_current()->thread;
> + pm_thread_t* thread = current_thread;
> syscall_pm_sleep();
> thread->status = PM_STATUS_WAIT_FOR_RPC;
> }
> diff --git a/src/kernel2/src/syscalls/pv.c b/src/kernel2/src/syscalls/pv.c
> index 7f73e61..e587ca0 100644
> --- a/src/kernel2/src/syscalls/pv.c
> +++ b/src/kernel2/src/syscalls/pv.c
> @@ -47,8 +47,7 @@
> */
> int syscall_pm_p(void)
> {
> - pm_block_rpc(cpu_get_current()->thread->process,
> - cpu_get_current()->thread->process->pid);
> + pm_block_rpc(current_process, current_process->pid);
>
> return 0;
> }
> @@ -62,15 +61,14 @@ int syscall_pm_p(void)
> int syscall_pm_v(pid_t pid)
> {
> if (pid == 0) {
> - pm_unblock_rpc(cpu_get_current()->thread->process,
> - cpu_get_current()->thread->process->pid);
> + pm_unblock_rpc(current_process, current_process->pid);
> } else {
> pm_process_t* process = pm_get(pid);
> if (process == NULL) {
> return -1;
> }
> pm_unblock(process);
> - pm_unblock_rpc(process, cpu_get_current()->thread->process->pid);
> + pm_unblock_rpc(process, current_process->pid);
> }
>
> return 0;
> diff --git a/src/kernel2/src/syscalls/rpc.c b/src/kernel2/src/syscalls/rpc.c
> index aa978de..688c4ea 100644
> --- a/src/kernel2/src/syscalls/rpc.c
> +++ b/src/kernel2/src/syscalls/rpc.c
> @@ -64,7 +64,7 @@ typedef struct {
> */
> void syscall_set_rpc_handler(vaddr_t address)
> {
> - cpu_get_current()->thread->process->rpc_handler = address;
> + current_process->rpc_handler = address;
> }
>
>
> @@ -154,7 +154,7 @@ int syscall_fastrpc(pid_t callee_pid, size_t metadata_size, void* metadata,
> }
>
> /*
> - if (callee == cpu_get_current()->thread) {
> + if (callee == current_thread) {
> kprintf("PID %d: Self-RPC ignoriert\n", callee->process->pid);
> return 0;
> }
> @@ -214,9 +214,9 @@ int syscall_fastrpc(pid_t callee_pid, size_t metadata_size, void* metadata,
> rpc->old_esp = callee_isf->esp;
> rpc->eax = callee_isf->eax;
> rpc->eflags = callee_isf->eflags;
> - rpc->caller = cpu_get_current()->thread;
> + rpc->caller = current_thread;
>
> - if (callee == cpu_get_current()->thread) {
> + if (callee == current_thread) {
> // Bei einem Self-RPC muss hinterher der Rueckgabewert
> // des RPC-Syscalls in eax stehen
> rpc->eax = 0;
> @@ -239,7 +239,7 @@ int syscall_fastrpc(pid_t callee_pid, size_t metadata_size, void* metadata,
> callee_isf->esp -= rounded_data_size;
>
> void* first_stack_page = mmc_automap_user(
> - &cpu_get_current()->thread->process->context,
> + ¤t_process->context,
> &callee->process->context,
> (vaddr_t) PAGE_ALIGN_ROUND_DOWN(callee_isf->esp),
> NUM_PAGES(rounded_data_size + ((uintptr_t) callee_isf->esp % PAGE_SIZE)),
> @@ -248,11 +248,11 @@ int syscall_fastrpc(pid_t callee_pid, size_t metadata_size, void* metadata,
> void* stack = first_stack_page + ((uintptr_t) callee_isf->esp % PAGE_SIZE);
>
> ((uint32_t*) stack)[0] = data_size + metadata_size;
> - ((uint32_t*) stack)[1] = cpu_get_current()->thread->process->pid;
> + ((uint32_t*) stack)[1] = current_process->pid;
>
> #if 0
> kprintf("RPC PID %d => %d\n",
> - cpu_get_current()->thread->process->pid,
> + current_process->pid,
> callee->process->pid);
> kprintf("Kopiere: %08x => %08x, %d Bytes\n",
> metadata, stack + 8, metadata_size);
> @@ -268,13 +268,13 @@ int syscall_fastrpc(pid_t callee_pid, size_t metadata_size, void* metadata,
> callee_isf->eip = (uintptr_t) callee->process->rpc_handler;
>
> // Temporaere Mappings wieder aufheben
> - mmc_unmap(&cpu_get_current()->thread->process->context, first_stack_page,
> + mmc_unmap(¤t_process->context, first_stack_page,
> NUM_PAGES(rounded_data_size));
>
> vmm_kernel_unmap(callee_isf, sizeof(*callee_isf));
>
> // kprintf("[%d => %d] RPC durchgefuehrt.\n",
> -// cpu_get_current()->thread->process->pid,
> +// current_process->pid,
> // callee->process->pid);
>
> // Der aufgerufene Task darf wieder laufen
> @@ -345,9 +345,8 @@ bool fastrpc_irq(pm_process_t* callee, size_t metadata_size, void* metadata,
> */
> void syscall_fastrpc_ret(void)
> {
> - rpc_t* rpc = list_pop(cpu_get_current()->thread->process->rpcs);
> - interrupt_stack_frame_t* callee_isf =
> - cpu_get_current()->thread->kernel_stack;
> + rpc_t* rpc = list_pop(current_process->rpcs);
> + interrupt_stack_frame_t* callee_isf = current_thread->kernel_stack;
>
> // Wenn der Task vom RPC-Handler zur??ckkehrt, obwohl der Handler
> // gar nicht aufgerufen wurde, l??uft was schief
> @@ -370,7 +369,7 @@ void syscall_fastrpc_ret(void)
>
> // kprintf("[%d => %d] RPC fertig.\n",
> // rpc->caller->process->pid,
> -// cpu_get_current()->thread->process->pid);
> +// current_process->pid);
>
> // TODO Wechsel zum aufrufenden Task
> #if 0
> @@ -414,7 +413,7 @@ void rpc_destroy_task_backlinks(struct task* destroyed_task)
>
> void syscall_add_interrupt_handler(uint32_t intr)
> {
> - im_add_handler(intr, cpu_get_current()->thread->process);
> + im_add_handler(intr, current_process);
> }
>
> #endif
Sieht gut aus, rein damit.
Puh... das wäre geschafft. ;-)
--
Antoine Kaufmann
<toni at famkaufmann.info>
-------------- nächster Teil --------------
Ein Dateianhang mit Binärdaten wurde abgetrennt...
Dateiname : nicht verfügbar
Dateityp : application/pgp-signature
Dateigröße : 197 bytes
Beschreibung: nicht verfügbar
URL : <http://list.tyndur.org/pipermail/tyndur-devel/attachments/20090118/a0a48067/attachment.pgp>