[1] | 1 | /* |
---|
| 2 | * cpu-context.c - thread's execution context operations |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
---|
| 6 | * |
---|
| 7 | * This file is part of ALMOS-kernel. |
---|
| 8 | * |
---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
---|
| 10 | * under the terms of the GNU General Public License as published by |
---|
| 11 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 12 | * |
---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 16 | * General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #include <types.h> |
---|
| 24 | #include <cpu.h> |
---|
| 25 | #include <thread.h> |
---|
| 26 | #include <cpu-internal.h> |
---|
| 27 | |
---|
| 28 | extern struct cpu_idt_ptr_s idt_ptr; |
---|
| 29 | |
---|
| 30 | void __cpu_context_init(struct cpu_context_s* ctx, struct cpu_context_attr_s *attr) |
---|
| 31 | { |
---|
| 32 | register struct thread_s *thread; |
---|
| 33 | register uint_t *ptr; |
---|
| 34 | register uint_t isUserMode; |
---|
| 35 | register reg_t cpu_id; |
---|
| 36 | |
---|
| 37 | thread = (struct thread_s*)attr->tid; |
---|
| 38 | cpu_id = thread->local_cpu->gid; |
---|
| 39 | ptr = (uint_t*) attr->stack_ptr; |
---|
| 40 | thread->info.usr_tls = (uint_t)thread; |
---|
| 41 | isUserMode = (attr->mode == CPU_USR_MODE) ? 1 : 0; |
---|
| 42 | |
---|
| 43 | *(--ptr) = attr->arg2; |
---|
| 44 | *(--ptr) = attr->arg1; |
---|
| 45 | *(--ptr) = attr->exit_func; |
---|
| 46 | attr->stack_ptr = (reg_t)ptr; |
---|
| 47 | *(--ptr) = (isUserMode) ? 0x23 : 0x10; |
---|
| 48 | *(--ptr) = attr->stack_ptr; |
---|
| 49 | *(--ptr) = 0x0200; |
---|
| 50 | *(--ptr) = (isUserMode) ? 0x1b : 0x08; |
---|
| 51 | *(--ptr) = attr->entry_func; |
---|
| 52 | |
---|
| 53 | ctx->loadable = 1; |
---|
| 54 | ctx->tss_ptr = (reg_t)cpu_get_tss(cpu_id); |
---|
| 55 | ctx->cpu_id = cpu_id; |
---|
| 56 | ctx->kstack_ptr = (isUserMode) ? (reg_t)thread->sys_stack_top : attr->stack_ptr; |
---|
| 57 | ctx->mode = 0; |
---|
| 58 | ctx->stack_ptr = (reg_t)ptr; |
---|
| 59 | ctx->thread_ptr = attr->tid; |
---|
| 60 | ctx->entry_func = attr->entry_func; |
---|
| 61 | ctx->exit_func = attr->exit_func; |
---|
| 62 | ctx->arg1 = attr->arg1; |
---|
| 63 | ctx->arg2 = attr->arg2; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | extern void dump(uint8_t*, int); |
---|
| 67 | extern void gdt_print_entry(struct cpu_gdt_entry_s*); |
---|
| 68 | |
---|
| 69 | void cpu_context_load(struct cpu_context_s *ctx) |
---|
| 70 | { |
---|
| 71 | struct cpu_tss_s *tss = (struct cpu_tss_s*)ctx->tss_ptr; |
---|
| 72 | ctx->loadable = 0; |
---|
| 73 | |
---|
| 74 | tss->ss_r0 = 0x10; /* KERNEL DATA/STACK */ |
---|
| 75 | tss->esp_r0 = ctx->kstack_ptr; |
---|
| 76 | tss->esp_r1 = ctx->thread_ptr; |
---|
| 77 | |
---|
| 78 | //printk(DEBUG,"cpu_idt.base %x, cpu_idt.limit %d\n",idt_ptr.base, idt_ptr.limit); |
---|
| 79 | |
---|
| 80 | #if 0 |
---|
| 81 | int tss_index = (ctx->cpu_id + GDT_FIXED_NR) << 3; |
---|
| 82 | uint32_t *ptr; |
---|
| 83 | int32_t i; |
---|
| 84 | gdt_print_entry(cpu_get_gdt_entry(5)); |
---|
| 85 | ptr = (uint32_t*)ctx->stack_ptr; |
---|
| 86 | printk(DEBUG, "Thread %x, stack %x, mode %x, tss idx %x\t", ctx->thread_ptr, ptr, ctx->mode, tss_index); |
---|
| 87 | for(i=6; i >= 0; i--) |
---|
| 88 | { |
---|
| 89 | printk(DEBUG, "stack[%d] = %x\n", i, ptr[i]); |
---|
| 90 | } |
---|
| 91 | #endif |
---|
| 92 | __asm__ __volatile__ ("" ::: "memory"); |
---|
| 93 | |
---|
| 94 | __asm__ volatile |
---|
| 95 | ("movl %0, %%esp \n" |
---|
| 96 | "xor $0x0, %1 \n" |
---|
| 97 | "jz 1f \n" |
---|
| 98 | "movl %%edx, %%ecx \n" |
---|
| 99 | "shr $16, %%ecx \n" |
---|
| 100 | "movw $0x23, %%ax \n" /* UDATA */ |
---|
| 101 | "movw %%ax, %%ds \n" |
---|
| 102 | "movw %%ax, %%es \n" |
---|
| 103 | "movw %%dx, %%fs \n" |
---|
| 104 | "movw %%cx, %%gs \n" |
---|
| 105 | "1: \n" |
---|
| 106 | "iret \n" |
---|
| 107 | ::"r"(ctx->stack_ptr), "r"(ctx->mode), "d"(ctx->thread_ptr)); |
---|
| 108 | } |
---|