These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / openbios / arch / sparc64 / 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*4
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] = (uint64_t) &_estack - STACK_BIAS - 96,
28     .pc = (uint64_t) start_main,
29     .npc = (uint64_t) start_main + 4,
30     .return_addr = (uint64_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 * volatile __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 /* Pointer to Forth context stack */
44 void *_fcstack_ptr = &_efcstack;
45
46
47 /*
48  * Main starter
49  * This is the C function that runs first.
50  */
51 static void start_main(void)
52 {
53     /* Save startup context, so we can refer to it later.
54      * We have to keep it in physical address since we will relocate. */
55     __boot_ctx = virt_to_phys(__context);
56
57     /* Start the real fun */
58     openbios();
59
60     /* Returning from here should jump to __exit_context */
61     __context = boot_ctx;
62 }
63
64 static uint64_t ALIGN_SIZE(uint64_t x, uint64_t a)
65 {
66     return (x + a - 1) & ~(a-1);
67 }
68
69 /* Setup a new context using the given stack.
70  */
71 struct context *
72 init_context(uint8_t *stack, uint64_t stack_size, int num_params)
73 {
74     struct context *ctx;
75     uint8_t *stack_top = stack + stack_size;
76
77     ctx = (struct context *)
78         (stack_top - ALIGN_SIZE(sizeof(*ctx) + num_params*sizeof(uint64_t), sizeof(uint64_t)));
79     memset(ctx, 0, sizeof(*ctx));
80
81     /* Fill in reasonable default for flat memory model */
82     ctx->regs[REG_SP] = virt_to_phys(stack_top - STACK_BIAS - 192);
83     ctx->return_addr = virt_to_phys(__exit_context);
84
85     return ctx;
86 }
87
88 /* Switch to another context. */
89 struct context *switch_to(struct context *ctx)
90 {
91     struct context *save, *ret;
92
93     debug("switching to new context: entry point %#llx stack 0x%016llx\n", ctx->pc, ctx->regs[REG_SP]);
94     save = __context;
95     __context = ctx;
96     //asm ("pushl %cs; call __switch_context");
97     asm ("call __switch_context_nosave; nop");
98     ret = __context;
99     __context = save;
100     return ret;
101 }
102
103 /* Start ELF Boot image */
104 uint64_t start_elf(uint64_t entry_point, uint64_t param)
105 {
106     struct context *ctx;
107
108     ctx = init_context(image_stack, sizeof image_stack, 1);
109     ctx->pc = entry_point;
110     ctx->param[0] = param;
111     //ctx->eax = 0xe1fb007;
112     //ctx->ebx = param;
113
114     ctx = switch_to(ctx);
115     //return ctx->eax;
116     return 0;
117 }
118
119 /* Start client image */
120 uint64_t start_client_image(uint64_t entry_point, uint64_t cif_handler)
121 {
122     struct context *ctx;
123
124     ctx = init_context(image_stack, sizeof image_stack, 0);
125     ctx->pc  = entry_point;
126     ctx->npc = entry_point+4;
127     ctx->regs[REG_O0] = 0;
128     ctx->regs[REG_O0+4] = cif_handler;
129
130     ctx = switch_to(ctx);
131
132     return 0;
133 }