Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / openbios / arch / sparc32 / context.c
1 /*
2  * context switching
3  * 2003-10 by SONE Takeshi
4  */
5
6 #include "config.h"
7 #include "kernel/kernel.h"
8 #include "context.h"
9 #include "libopenbios/sys_info.h"
10 #include "boot.h"
11 #include "openbios.h"
12
13 #define MAIN_STACK_SIZE 16384
14 #define IMAGE_STACK_SIZE 4096*2
15
16 #define debug printk
17
18 static void start_main(void); /* forward decl. */
19 void __exit_context(void); /* assembly routine */
20
21 /*
22  * Main context structure
23  * It is placed at the bottom of our stack, and loaded by assembly routine
24  * to start us up.
25  */
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,
31 };
32
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;
36
37 /* Stack for loaded ELF image */
38 static uint8_t image_stack[IMAGE_STACK_SIZE];
39
40 /* Pointer to startup context (physical address) */
41 unsigned long __boot_ctx;
42
43 /*
44  * Main starter
45  * This is the C function that runs first.
46  */
47 static void start_main(void)
48 {
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);
52
53     /* Start the real fun */
54     openbios();
55
56     /* Returning from here should jump to __exit_context */
57     __context = boot_ctx;
58 }
59
60 /* Setup a new context using the given stack.
61  */
62 struct context *
63 init_context(uint8_t *stack, uint32_t stack_size, int num_params)
64 {
65     struct context *ctx;
66
67     ctx = (struct context *)
68         (stack + stack_size - (sizeof(*ctx) + num_params*sizeof(uint32_t)));
69     memset(ctx, 0, sizeof(*ctx));
70
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);
74
75     return ctx;
76 }
77
78 /* Switch to another context. */
79 struct context *switch_to(struct context *ctx)
80 {
81     volatile struct context *save;
82     struct context *ret;
83
84     debug("switching to new context:\n");
85     save = __context;
86     __context = ctx;
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",
95                       "f30", "f31",
96                       "memory");
97     ret = __context;
98     __context = (struct context *)save;
99     return ret;
100 }
101
102 /* Start ELF Boot image */
103 unsigned int start_elf(unsigned long entry_point, unsigned long param)
104 {
105     struct context *ctx;
106
107     ctx = init_context(image_stack, sizeof image_stack, 1);
108     ctx->pc = entry_point;
109     ctx->param[0] = param;
110
111     ctx = switch_to(ctx);
112     return ctx->regs[REG_O0];
113 }