3 * 2003-10 by SONE Takeshi
7 #include "kernel/kernel.h"
9 #include "libopenbios/sys_info.h"
13 #define MAIN_STACK_SIZE 16384
14 #define IMAGE_STACK_SIZE 4096*2
18 static void start_main(void); /* forward decl. */
19 void __exit_context(void); /* assembly routine */
22 * Main context structure
23 * It is placed at the bottom of our stack, and loaded by assembly routine
26 static struct context main_ctx = {
27 .regs[REG_SP] = (uint32_t) &_estack - 96,
28 .pc = (uint32_t) start_main,
29 .npc = (uint32_t) start_main + 4,
30 .return_addr = (uint32_t) __exit_context,
33 /* This is used by assembly routine to load/store the context which
34 * it is to switch/switched. */
35 struct context *__context = &main_ctx;
37 /* Stack for loaded ELF image */
38 static uint8_t image_stack[IMAGE_STACK_SIZE];
40 /* Pointer to startup context (physical address) */
41 unsigned long __boot_ctx;
45 * This is the C function that runs first.
47 static void start_main(void)
49 /* Save startup context, so we can refer to it later.
50 * We have to keep it in physical address since we will relocate. */
51 __boot_ctx = virt_to_phys(__context);
53 /* Start the real fun */
56 /* Returning from here should jump to __exit_context */
60 /* Setup a new context using the given stack.
63 init_context(uint8_t *stack, uint32_t stack_size, int num_params)
67 ctx = (struct context *)
68 (stack + stack_size - (sizeof(*ctx) + num_params*sizeof(uint32_t)));
69 memset(ctx, 0, sizeof(*ctx));
71 /* Fill in reasonable default for flat memory model */
72 ctx->regs[REG_SP] = virt_to_phys(SP_LOC(ctx));
73 ctx->return_addr = virt_to_phys(__exit_context);
78 /* Switch to another context. */
79 struct context *switch_to(struct context *ctx)
81 volatile struct context *save;
84 debug("switching to new context:\n");
87 asm __volatile__ ("\n\tcall __switch_context"
88 "\n\tnop" ::: "g1", "g2", "g3", "g4", "g5", "g6", "g7",
89 "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
90 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
91 "i0", "i1", "i2", "i3", "i4", "i5", "i7",
92 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9",
93 "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19",
94 "f20", "f21", "f22", "f23", "f24", "f25", "f26", "f27", "f28", "f29",
98 __context = (struct context *)save;
102 /* Start ELF Boot image */
103 unsigned int start_elf(unsigned long entry_point, unsigned long param)
107 ctx = init_context(image_stack, sizeof image_stack, 1);
108 ctx->pc = entry_point;
109 ctx->param[0] = param;
111 ctx = switch_to(ctx);
112 return ctx->regs[REG_O0];