3 * 2003-10 by SONE Takeshi
7 #include "kernel/kernel.h"
11 #define MAIN_STACK_SIZE 16384
12 #define IMAGE_STACK_SIZE 4096
16 static void start_main(void); /* forward decl. */
17 void __exit_context(void); /* assembly routine */
20 * Main context structure
21 * It is placed at the bottom of our stack, and loaded by assembly routine
24 struct context main_ctx __attribute__((section (".initctx"))) = {
25 .gdt_base = (uint64_t) gdt,
26 .gdt_limit = GDT_LIMIT,
33 .esp = (uint32_t) ESP_LOC(&main_ctx),
34 .eip = (uint32_t) start_main,
35 .return_addr = (uint32_t) __exit_context,
38 /* This is used by assembly routine to load/store the context which
39 * it is to switch/switched. */
40 struct context *__context = &main_ctx;
42 /* Stack for loaded ELF image */
43 static uint8_t image_stack[IMAGE_STACK_SIZE];
45 /* Pointer to startup context (physical address) */
46 unsigned long __boot_ctx;
50 * This is the C function that runs first.
52 static void start_main(void)
55 extern int openbios(void);
57 /* Save startup context, so we can refer to it later.
58 * We have to keep it in physical address since we will relocate. */
59 __boot_ctx = virt_to_phys(__context);
61 /* Start the real fun */
64 /* Pass return value to startup context. Bootloader may see it. */
65 boot_ctx->eax = retval;
67 /* Returning from here should jump to __exit_context */
71 /* Setup a new context using the given stack.
74 init_context(uint8_t *stack, uint32_t stack_size, int num_params)
78 ctx = (struct context *)
79 (stack + stack_size - (sizeof(*ctx) + num_params*sizeof(uint32_t)));
80 memset(ctx, 0, sizeof(*ctx));
82 /* Fill in reasonable default for flat memory model */
83 ctx->gdt_base = virt_to_phys(gdt);
84 ctx->gdt_limit = GDT_LIMIT;
91 ctx->esp = virt_to_phys(ESP_LOC(ctx));
92 ctx->return_addr = virt_to_phys(__exit_context);
97 /* Switch to another context. */
98 struct context *switch_to(struct context *ctx)
100 struct context *save, *ret;
102 debug("switching to new context:\n");
105 asm ("pushl %cs; call __switch_context");
111 /* Start ELF Boot image */
112 uint32_t start_elf(uint32_t entry_point, uint32_t param)
116 ctx = init_context(image_stack, sizeof image_stack, 1);
117 ctx->eip = entry_point;
118 ctx->param[0] = param;
119 ctx->eax = 0xe1fb007;
122 ctx = switch_to(ctx);