POK(kernelpart)
space.c
Go to the documentation of this file.
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Thu Jan 15 23:34:13 2009
15  */
16 
17 
24 #include <types.h>
25 #include <errno.h>
26 #include <libc.h>
27 #include <bsp.h>
28 
29 #include <arch.h>
30 
31 #include <arch/x86/interrupt.h>
32 
33 #include "gdt.h"
34 #include "tss.h"
35 
36 #include "space.h"
37 
38 #define KERNEL_STACK_SIZE 8192
39 
41  uint32_t addr,
42  uint32_t size)
43 {
45  addr, size, GDTE_CODE, 3);
46 
48  addr, size, GDTE_DATA, 3);
49 
50  return (POK_ERRNO_OK);
51 }
52 
54  uint8_t new_partition_id)
55 {
56  gdt_disable (GDT_PARTITION_CODE_SEGMENT(old_partition_id));
57  gdt_disable (GDT_PARTITION_DATA_SEGMENT(old_partition_id));
58  gdt_enable (GDT_PARTITION_CODE_SEGMENT(new_partition_id));
59  gdt_enable (GDT_PARTITION_DATA_SEGMENT(new_partition_id));
60 
61  return (POK_ERRNO_OK);
62 }
63 
65 {
66  (void) addr;
67  return (0);
68 }
69 
71  uint32_t entry_rel,
72  uint32_t stack_rel,
73  uint32_t arg1,
74  uint32_t arg2)
75 {
76  char* stack_addr;
77  space_context_t* sp;
78 
79  stack_addr = pok_bsp_mem_alloc (KERNEL_STACK_SIZE);
80 
81  sp = (space_context_t *)
82  (stack_addr + KERNEL_STACK_SIZE - 4 - sizeof (space_context_t));
83 
84  memset (sp, 0, sizeof (space_context_t));
85 
86  sp->ctx.__esp = (uint32_t)(&sp->ctx.eip); /* for pusha */
88  sp->ctx.cs = GDT_CORE_CODE_SEGMENT << 3;
89  sp->ctx.eflags = 1 << 9;
90 
91  sp->arg1 = arg1;
92  sp->arg2 = arg2;
93  sp->kernel_sp = (uint32_t)sp;
94  sp->user_sp = stack_rel;
95  sp->user_pc = entry_rel;
96  sp->partition_id = partition_id;
97 
98  return ((uint32_t) sp);
99 }
100 
101 #ifdef POK_NEEDS_ERROR_HANDLING
102 void pok_space_context_restart (uint32_t sp, uint32_t entry, uint32_t user_stack)
103 {
104  space_context_t* ct;
105 
106  ct = (space_context_t*) sp;
107  ct->ctx.__esp = (uint32_t)(&ct->ctx.eip); /* for pusha */
109  ct->user_pc = entry;
110  ct->user_sp = user_stack;
111 }
112 #endif
113 
114 void pok_dispatch_space (uint8_t partition_id,
115  uint32_t user_pc,
116  uint32_t user_sp,
117  uint32_t kernel_sp,
118  uint32_t arg1,
119  uint32_t arg2)
120 {
121  interrupt_frame ctx;
122  uint32_t code_sel;
123  uint32_t data_sel;
124  uint32_t sp;
125 
126  code_sel = GDT_BUILD_SELECTOR (GDT_PARTITION_CODE_SEGMENT (partition_id), 0, 3);
127  data_sel = GDT_BUILD_SELECTOR (GDT_PARTITION_DATA_SEGMENT (partition_id), 0, 3);
128 
129  sp = (uint32_t) &ctx;
130 
131  memset (&ctx, 0, sizeof (interrupt_frame));
132 
134 
135  ctx.es = ctx.ds = ctx.ss = data_sel;
136 
137  ctx.__esp = (uint32_t) (&ctx.error); /* for pusha */
138  ctx.eip = user_pc;
139  ctx.eax = arg1;
140  ctx.ebx = arg2;
141  ctx.cs = code_sel;
142  ctx.eflags = 1 << 9;
143  ctx.esp = user_sp;
144 
145  tss_set_esp0 (kernel_sp);
146 
147  asm ("mov %0, %%esp \n"
148  "pop %%es \n"
149  "pop %%ds \n"
150  "popa \n"
151  "addl $4, %%esp \n"
152  "iret \n"
153  :
154  : "m" (sp)
155  );
156 }