]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/kernel/arch.c
da91adf747ac7e645a3b2374e80af0cfd6502615
[arc.git] / arch / ppc / mpc55xx / kernel / arch.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 #include "internal.h"\r
16 #include "asm_ppc.h"\r
17 #include "mpc55xx.h"\r
18 #include "asm_book_e.h"\r
19 \r
20 #define USE_LDEBUG_PRINTF\r
21 #include "debug.h"\r
22 \r
23 /**\r
24  * Function make sure that we switch to supervisor mode(rfi) before\r
25  * we call a task for the first time.\r
26  */\r
27 void Os_ArchFirstCall( void )\r
28 {\r
29 #if USE_MM_USER_MODE\r
30 \r
31         // Assume that regs[0] is setup before and contains the settings\r
32         // to switch to user mode.\r
33         register uint32_t msr asm("r3") = os_sys.curr_pcb->regs[0];\r
34         register void *ea asm("r4") = (void *) os_sys.curr_pcb->entry;\r
35 \r
36         // Do the switch\r
37         asm volatile(\r
38                         "mtspr 26,%0;\r\t"      // srr0\r
39                         "mtspr 27,%1;\r\t"      // srr1\r
40                         "rfi;\r\t"\r
41                         :\r
42                         : "r" (ea), "r" (msr)   );\r
43 #else\r
44 // TODO: This really depends on if scheduling policy\r
45         Irq_Enable();\r
46         os_sys.curr_pcb->entry();\r
47 #endif\r
48 }\r
49 \r
50 \r
51 /* TODO: This actually gives the stack ptr here...not the callers stack ptr\r
52  * Should probably be a macro instead..... in some arch part..\r
53  */\r
54 void *Os_ArchGetStackPtr( void ) {\r
55         void *stackp;\r
56         // Get stack ptr(r1) from current context\r
57         asm volatile(" mr %0,1":"=r" (stackp));\r
58 \r
59         return stackp;\r
60 }\r
61 \r
62 unsigned int Os_ArchGetScSize( void ) {\r
63         return SC_SIZE;\r
64 }\r
65 \r
66 extern void os_arch_setup_context_asm( void *context,unsigned int msr);\r
67 \r
68 // TODO: I have no clue why I wrote this????\r
69 void os_arch_stack_to_small(OsPcbType *pcb ,uint32_t size_min) {\r
70         OsPcbType *t;\r
71         uint32_t min;\r
72 \r
73         while(1) {\r
74                 t = pcb;\r
75                 min = size_min;\r
76         }\r
77 }\r
78 \r
79 /*\r
80  * Stack grows from high -> low addresses\r
81  *\r
82 \r
83  *\r
84  *\r
85 \r
86 \r
87  * ----------------  bottom of the stack( high address )\r
88  *  small context\r
89  * ----------------\r
90  *\r
91  * ----------------  top of the stack( low address )\r
92  *\r
93  */\r
94 \r
95 void Os_ArchSetupContext( OsPcbType *pcb ) {\r
96         uint32_t msr;\r
97 \r
98         msr = MSR_EE;\r
99 \r
100 #if defined(CFG_SPE)\r
101         msr |= MSR_SPE;\r
102 #endif\r
103 \r
104 #if (  OS_SC3 == STD_ON) || (  OS_SC4== STD_ON)\r
105         if( !pcb->application->trusted ) {\r
106                 // Non-trusted = User mode..\r
107                 msr |= MSR_PR | MSR_DS | MSR_IS;\r
108         }\r
109 #endif\r
110         pcb->regs[0] = msr;\r
111 }\r
112 \r
113 /**\r
114  *\r
115  * @param pcbPtr\r
116  */\r
117 \r
118 void Os_ArchSetTaskEntry(OsPcbType *pcbPtr ) {\r
119         uint32_t *context = (uint32_t *)pcbPtr->stack.curr;\r
120 \r
121         context[C_CONTEXT_OFF/4] = SC_PATTERN;\r
122 \r
123         /* Set LR to start function */\r
124         if( pcbPtr->proc_type == PROC_EXTENDED ) {\r
125                 context[C_LR_OFF/4] = (uint32_t)Os_TaskStartExtended;\r
126         } else if( pcbPtr->proc_type == PROC_BASIC ) {\r
127                 context[C_LR_OFF/4] = (uint32_t)Os_TaskStartBasic;\r
128         }\r
129 }\r
130 \r
131 \r
132 #define C_CONTEXT_OFF   12\r
133 #define C_LR_OFF                16\r
134 #define C_CR_OFF                20\r
135 \r
136 void os_arch_print_context( char *str, OsPcbType *pcb ) {\r
137         uint32_t *stack;\r
138 \r
139         LDEBUG_PRINTF("%s CONTEXT: %d\n",str, pcb->pid);\r
140         LDEBUG_PRINTF("  stack: curr=%p top=%p bottom=%p\n",\r
141                                         pcb->stack.curr,\r
142                                         pcb->stack.top,\r
143                                         pcb->stack.top+ pcb->stack.size);\r
144         stack = pcb->stack.curr;\r
145         LDEBUG_PRINTF("  val  : context=%08x LR=%08x CR=%08x\n",\r
146                                         (unsigned)stack[C_CONTEXT_OFF/4],\r
147                                         (unsigned)stack[C_LR_OFF/4],\r
148                                         (unsigned)stack[C_CR_OFF/4]\r
149                                         );\r
150 }\r
151 \r
152 \r
153 void Os_ArchInit( void ) {\r
154 #if defined(CFG_SPE)\r
155         uint32_t msr = get_msr();\r
156         msr |= MSR_SPE;\r
157         set_msr(msr);\r
158 #endif\r
159 }\r
160 \r
161 #define EXC_VECTOR_CRITICAL_INPUT_OFF   0\r
162 #define EXC_VECTOR_MACHINE_CHECK_OFF    1\r
163 #define EXC_VECTOR_PROGRAM_OFF                  6\r
164 #define EXC_VECTOR_DECREMENTER_OFF              10\r
165 #define EXC_VECTOR_DATA_TLB_OFF                 13\r
166 #define EXC_VECTOR_INSTRUCTION_TLB_OFF  14\r
167 \r
168 \r
169 typedef void (*exc_func_t)(uint32_t *);\r
170 \r
171 /* How do I construct the PTE table ??\r
172  *\r
173  * 1. Calculate the section sizes for each application\r
174  *    ( objdump on the object file)\r
175  * 2. Generate a Table for each application\r
176  *\r
177  * OS207\r
178  * Trusted OS-applications CAN write to\r
179  *\r
180  * What do I do with the global data ???\r
181  *\r
182  *\r
183  */\r
184 \r
185 \r
186 /* Move these somewhere else if we need the speed */\r
187 void os_arch_data_tlb( uint32_t *regs ) {\r
188         uint32_t dear = regs[EXC_DEAR_OFF];\r
189         (void)dear;\r
190 }\r
191 \r
192 void os_arch_instruction_tlb( uint32_t *regs ) {\r
193         uint32_t srr0 = regs[EXC_SRR0_OFF];\r
194         (void)srr0;\r
195         /* What information can I get here??\r
196          * - The pcb to MMU mapping ???\r
197          */\r
198 \r
199         /* TODO: How do I construct the PTE(Page Table Entry) ??*/\r
200 \r
201 }\r
202 \r
203 void os_arch_exc_program( uint32_t *regs ) {\r
204         uint32_t esr = regs[EXC_ESR_OFF];\r
205 \r
206         if( esr & ESR_PTR ) {\r
207                 // Trap\r
208                 if( !(regs[EXC_SRR1_OFF] & MSR_PR) ) {\r
209                         // User -> Supervisor\r
210                         regs[EXC_SRR1_OFF] |= MSR_PR;\r
211                 }\r
212         }\r
213 }\r
214 \r
215 \r