SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /sos/main.c (Article 6) and /sos/main.c (Article 9)


001 /* Copyright (C) 2004  The SOS Team               001 /* Copyright (C) 2004  The SOS Team
002    Copyright (C) 1999  Free Software Foundatio << 
003                                                   002 
004    This program is free software; you can redi    003    This program is free software; you can redistribute it and/or
005    modify it under the terms of the GNU Genera    004    modify it under the terms of the GNU General Public License
006    as published by the Free Software Foundatio    005    as published by the Free Software Foundation; either version 2
007    of the License, or (at your option) any lat    006    of the License, or (at your option) any later version.
008                                                   007    
009    This program is distributed in the hope tha    008    This program is distributed in the hope that it will be useful,
010    but WITHOUT ANY WARRANTY; without even the     009    but WITHOUT ANY WARRANTY; without even the implied warranty of
011    MERCHANTABILITY or FITNESS FOR A PARTICULAR    010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012    GNU General Public License for more details    011    GNU General Public License for more details.
013                                                   012    
014    You should have received a copy of the GNU     013    You should have received a copy of the GNU General Public License
015    along with this program; if not, write to t    014    along with this program; if not, write to the Free Software
016    Foundation, Inc., 59 Temple Place - Suite 3    015    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
017    USA.                                           016    USA. 
018 */                                                017 */
019                                                   018 
                                                   >> 019 #include <sos/errno.h>
                                                   >> 020 
020 /* Include definitions of the multiboot standa    021 /* Include definitions of the multiboot standard */
021 #include <bootstrap/multiboot.h>                  022 #include <bootstrap/multiboot.h>
022 #include <hwcore/idt.h>                           023 #include <hwcore/idt.h>
023 #include <hwcore/gdt.h>                           024 #include <hwcore/gdt.h>
024 #include <hwcore/irq.h>                           025 #include <hwcore/irq.h>
025 #include <hwcore/exception.h>                     026 #include <hwcore/exception.h>
026 #include <hwcore/i8254.h>                         027 #include <hwcore/i8254.h>
027 #include <sos/list.h>                             028 #include <sos/list.h>
028 #include <sos/physmem.h>                          029 #include <sos/physmem.h>
029 #include <hwcore/paging.h>                        030 #include <hwcore/paging.h>
                                                   >> 031 #include <hwcore/mm_context.h>
                                                   >> 032 #include <hwcore/swintr.h>
030 #include <sos/kmem_vmm.h>                         033 #include <sos/kmem_vmm.h>
031 #include <sos/kmalloc.h>                          034 #include <sos/kmalloc.h>
                                                   >> 035 #include <sos/time.h>
                                                   >> 036 #include <sos/thread.h>
                                                   >> 037 #include <sos/process.h>
                                                   >> 038 #include <sos/umem_vmm.h>
032 #include <sos/klibc.h>                            039 #include <sos/klibc.h>
033 #include <sos/assert.h>                           040 #include <sos/assert.h>
034 #include <drivers/x86_videomem.h>                 041 #include <drivers/x86_videomem.h>
035 #include <drivers/bochs.h>                        042 #include <drivers/bochs.h>
036                                                !! 043 #include <sos/calcload.h>
                                                   >> 044 #include <sos/umem_vmm.h>
                                                   >> 045 #include <sos/binfmt_elf32.h>
                                                   >> 046 #include <drivers/zero.h>
                                                   >> 047 #include <sos/fs.h>
                                                   >> 048 #include <drivers/fs_virtfs.h>
                                                   >> 049 #include <drivers/devices.h>
                                                   >> 050 #include <drivers/mem.h>
                                                   >> 051 #include <drivers/tty.h>
                                                   >> 052 #include <drivers/console.h>
                                                   >> 053 #include <drivers/serial.h>
037                                                   054 
038 /* Helper function to display each bits of a 3    055 /* Helper function to display each bits of a 32bits integer on the
039    screen as dark or light carrets */             056    screen as dark or light carrets */
040 void display_bits(unsigned char row, unsigned     057 void display_bits(unsigned char row, unsigned char col,
041                   unsigned char attribute,        058                   unsigned char attribute,
042                   sos_ui32_t integer)             059                   sos_ui32_t integer)
043 {                                                 060 {
044   int i;                                          061   int i;
045   /* Scan each bit of the integer, MSb first *    062   /* Scan each bit of the integer, MSb first */
046   for (i = 31 ; i >= 0 ; i--)                     063   for (i = 31 ; i >= 0 ; i--)
047     {                                             064     {
048       /* Test if bit i of 'integer' is set */     065       /* Test if bit i of 'integer' is set */
049       int bit_i = (integer & (1 << i));           066       int bit_i = (integer & (1 << i));
050       /* Ascii 219 => dark carret, Ascii 177 =    067       /* Ascii 219 => dark carret, Ascii 177 => light carret */
051       unsigned char ascii_code = bit_i?219:177    068       unsigned char ascii_code = bit_i?219:177;
052       sos_x86_videomem_putchar(row, col++,        069       sos_x86_videomem_putchar(row, col++,
053                                attribute,         070                                attribute,
054                                ascii_code);       071                                ascii_code);
055     }                                             072     }
056 }                                                 073 }
057                                                   074 
058                                                << 
059 /* Clock IRQ handler */                           075 /* Clock IRQ handler */
060 static void clk_it(int intid,                  !! 076 static void clk_it(int intid)
061                    const struct sos_cpu_kstate << 
062 {                                                 077 {
063   static sos_ui32_t clock_count = 0;              078   static sos_ui32_t clock_count = 0;
064                                                   079 
065   display_bits(0, 48,                             080   display_bits(0, 48,
066                SOS_X86_VIDEO_FG_LTGREEN | SOS_    081                SOS_X86_VIDEO_FG_LTGREEN | SOS_X86_VIDEO_BG_BLUE,
067                clock_count);                      082                clock_count);
068   clock_count++;                                  083   clock_count++;
                                                   >> 084 
                                                   >> 085   /* Execute the expired timeout actions (if any) */
                                                   >> 086   sos_time_do_tick();
                                                   >> 087 
                                                   >> 088   /* Update scheduler statistics and status */
                                                   >> 089   sos_sched_do_timer_tick();
069 }                                                 090 }
070                                                   091 
071                                                   092 
072 /* ===========================================    093 /* ======================================================================
073  * Page fault exception handling                  094  * Page fault exception handling
074  */                                               095  */
075                                                   096 
076 /* Helper function to dump a backtrace on boch << 
077 static void dump_backtrace(const struct sos_cp << 
078                            sos_vaddr_t stack_b << 
079                            sos_size_t  stack_s << 
080                            sos_bool_t on_conso << 
081                            sos_bool_t on_bochs << 
082 {                                              << 
083   static void backtracer(sos_vaddr_t PC,       << 
084                          sos_vaddr_t params,   << 
085                          sos_ui32_t depth,     << 
086                          void *custom_arg)     << 
087     {                                          << 
088       sos_ui32_t invalid = 0xffffffff, *arg1,  << 
089                                                << 
090       /* Get the address of the first 3 argume << 
091          frame. Among these arguments, 0, 1, 2 << 
092          meaningful (depending on how many arg << 
093          take). */                             << 
094       arg1 = (sos_ui32_t*)params;              << 
095       arg2 = (sos_ui32_t*)(params+4);          << 
096       arg3 = (sos_ui32_t*)(params+8);          << 
097       arg4 = (sos_ui32_t*)(params+12);         << 
098                                                << 
099       /* Make sure the addresses of these argu << 
100          stack boundaries */                   << 
101 #define INTERVAL_OK(b,v,u) ( ((b) <= (sos_vadd << 
102                              && ((sos_vaddr_t) << 
103       if (!INTERVAL_OK(stack_bottom, arg1, sta << 
104         arg1 = &invalid;                       << 
105       if (!INTERVAL_OK(stack_bottom, arg2, sta << 
106         arg2 = &invalid;                       << 
107       if (!INTERVAL_OK(stack_bottom, arg3, sta << 
108         arg3 = &invalid;                       << 
109       if (!INTERVAL_OK(stack_bottom, arg4, sta << 
110         arg4 = &invalid;                       << 
111                                                << 
112       /* Print the function context for this f << 
113       if (on_bochs)                            << 
114         sos_bochs_printf("[%d] PC=0x%x arg1=0x << 
115                          (unsigned)depth, (uns << 
116                          (unsigned)*arg1, (uns << 
117                          (unsigned)*arg3);     << 
118                                                << 
119       if (on_console)                          << 
120         sos_x86_videomem_printf(23-depth, 3,   << 
121                                 SOS_X86_VIDEO_ << 
122                                   | SOS_X86_VI << 
123                                 "[%d] PC=0x%x  << 
124                                 (unsigned)dept << 
125                                 (unsigned)*arg << 
126                                 (unsigned)*arg << 
127                                                << 
128     }                                          << 
129                                                << 
130   sos_backtrace(cpu_kstate, 15, stack_bottom,  << 
131 }                                              << 
132                                                << 
133                                                   097 
134 /* Page fault exception handler with demand pa    098 /* Page fault exception handler with demand paging for the kernel */
135 static void pgflt_ex(int intid, const struct s !! 099 static void pgflt_ex(int intid, struct sos_cpu_state *ctxt)
136 {                                                 100 {
137   static sos_ui32_t demand_paging_count = 0;      101   static sos_ui32_t demand_paging_count = 0;
138   sos_vaddr_t faulting_vaddr = sos_cpu_kstate_ !! 102   struct sos_thread * cur_thr = sos_thread_get_current();
                                                   >> 103   sos_vaddr_t faulting_vaddr  = sos_cpu_context_get_EX_faulting_vaddr(ctxt);
139   sos_paddr_t ppage_paddr;                        104   sos_paddr_t ppage_paddr;
140                                                   105 
                                                   >> 106   if (sos_cpu_context_is_in_user_mode(ctxt)
                                                   >> 107       || (cur_thr->fixup_uaccess.return_vaddr))
                                                   >> 108     {
                                                   >> 109       __label__ unforce_address_space;
                                                   >> 110       sos_bool_t need_to_setup_mmu;
                                                   >> 111       sos_ui32_t errcode = sos_cpu_context_get_EX_info(ctxt);
                                                   >> 112 
                                                   >> 113       /* Make sure to always stay in the interrupted thread's MMU
                                                   >> 114          configuration */
                                                   >> 115       need_to_setup_mmu = (cur_thr->squatted_mm_context
                                                   >> 116                            != sos_process_get_mm_context(cur_thr->process));
                                                   >> 117       if (need_to_setup_mmu)
                                                   >> 118         sos_thread_prepare_user_space_access(NULL, 0);
                                                   >> 119 
                                                   >> 120       if (SOS_OK ==
                                                   >> 121           sos_umem_vmm_try_resolve_page_fault(faulting_vaddr,
                                                   >> 122                                               errcode & (1 << 1),
                                                   >> 123                                               TRUE))
                                                   >> 124         goto unforce_address_space;
                                                   >> 125 
                                                   >> 126       /* If the page fault occured in kernel mode, return to kernel to
                                                   >> 127          the fixup address */
                                                   >> 128       if (! sos_cpu_context_is_in_user_mode(ctxt))
                                                   >> 129         {
                                                   >> 130           cur_thr->fixup_uaccess.faulted_uaddr = faulting_vaddr;
                                                   >> 131           sos_cpu_context_set_EX_return_address(ctxt,
                                                   >> 132                                                 cur_thr->fixup_uaccess.return_vaddr);
                                                   >> 133           goto unforce_address_space;
                                                   >> 134         }
                                                   >> 135 
                                                   >> 136       if (need_to_setup_mmu)
                                                   >> 137         sos_thread_end_user_space_access();
                                                   >> 138 
                                                   >> 139       sos_bochs_printf("Unresolved USER page Fault at instruction 0x%x on access to address 0x%x (info=%x)!\n",
                                                   >> 140                        sos_cpu_context_get_PC(ctxt),
                                                   >> 141                        (unsigned)faulting_vaddr,
                                                   >> 142                        (unsigned)sos_cpu_context_get_EX_info(ctxt));
                                                   >> 143       sos_bochs_printf("Terminating User thread\n");
                                                   >> 144       sos_thread_exit();
                                                   >> 145 
                                                   >> 146     unforce_address_space:
                                                   >> 147       if (need_to_setup_mmu)
                                                   >> 148         sos_thread_end_user_space_access();
                                                   >> 149       return;
                                                   >> 150     }
                                                   >> 151 
141   /* Check if address is covered by any VMM ra    152   /* Check if address is covered by any VMM range */
142   if (! sos_kmem_vmm_is_valid_vaddr(faulting_v    153   if (! sos_kmem_vmm_is_valid_vaddr(faulting_vaddr))
143     {                                             154     {
144       /* No: The page fault is out of any kern    155       /* No: The page fault is out of any kernel virtual region. For
145          the moment, we don't handle this. */     156          the moment, we don't handle this. */
146       dump_backtrace(ctxt,                     !! 157       sos_display_fatal_error("Unresolved page Fault at instruction 0x%x on access to address 0x%x (info=%x)!",
147                      bootstrap_stack_bottom,   !! 158                               sos_cpu_context_get_PC(ctxt),
148                      bootstrap_stack_size,     << 
149                      TRUE, TRUE);              << 
150       sos_display_fatal_error("Unresolved page << 
151                               (unsigned)faulti    159                               (unsigned)faulting_vaddr,
152                               (unsigned)sos_cp !! 160                               (unsigned)sos_cpu_context_get_EX_info(ctxt));
153       SOS_ASSERT_FATAL(! "Got page fault (note    161       SOS_ASSERT_FATAL(! "Got page fault (note: demand paging is disabled)");
154     }                                             162     }
155                                                   163 
156                                                   164 
157   /*                                              165   /*
158    * Demand paging                             !! 166    * Demand paging in kernel space
159    */                                             167    */
160                                                   168  
161   /* Update the number of demand paging reques    169   /* Update the number of demand paging requests handled */
162   demand_paging_count ++;                         170   demand_paging_count ++;
163   display_bits(0, 0,                              171   display_bits(0, 0,
164                SOS_X86_VIDEO_FG_LTRED | SOS_X8    172                SOS_X86_VIDEO_FG_LTRED | SOS_X86_VIDEO_BG_BLUE,
165                demand_paging_count);              173                demand_paging_count);
166                                                   174 
167   /* Allocate a new page for the virtual addre    175   /* Allocate a new page for the virtual address */
168   ppage_paddr = sos_physmem_ref_physpage_new(F    176   ppage_paddr = sos_physmem_ref_physpage_new(FALSE);
169   if (! ppage_paddr)                              177   if (! ppage_paddr)
170     SOS_ASSERT_FATAL(! "TODO: implement swap.     178     SOS_ASSERT_FATAL(! "TODO: implement swap. (Out of mem in demand paging because no swap for kernel yet !)");
171   SOS_ASSERT_FATAL(SOS_OK == sos_paging_map(pp    179   SOS_ASSERT_FATAL(SOS_OK == sos_paging_map(ppage_paddr,
172                                             SO    180                                             SOS_PAGE_ALIGN_INF(faulting_vaddr),
173                                             FA    181                                             FALSE,
174                                             SO    182                                             SOS_VM_MAP_PROT_READ
175                                             |     183                                             | SOS_VM_MAP_PROT_WRITE
176                                             |     184                                             | SOS_VM_MAP_ATOMIC));
177   sos_physmem_unref_physpage(ppage_paddr);        185   sos_physmem_unref_physpage(ppage_paddr);
178                                                   186 
179   /* Ok, we can now return to interrupted cont    187   /* Ok, we can now return to interrupted context */
180 }                                                 188 }
181                                                   189 
182                                                   190 
183                                                   191 
184 /* ===========================================    192 /* ======================================================================
185  * Demonstrate the use of the CPU kernet conte !! 193  * An operating system MUST always have a ready thread ! Otherwise:
186  *  - A coroutine prints "Hlowrd" and switches !! 194  * what would the CPU have to execute ?!
187  *    letter                                   << 
188  *  - A coroutine prints "el ol\n" and switche << 
189  *    each letter.                             << 
190  * The first to reach the '\n' returns back to << 
191  */                                               195  */
192 struct sos_cpu_kstate *ctxt_hello1;            !! 196 static void idle_thread()
193 struct sos_cpu_kstate *ctxt_hello2;            << 
194 struct sos_cpu_kstate *ctxt_main;              << 
195 sos_vaddr_t hello1_stack, hello2_stack;        << 
196                                                << 
197 static void reclaim_stack(sos_vaddr_t stack_va << 
198 {                                                 197 {
199   sos_kfree(stack_vaddr);                      !! 198   sos_ui32_t idle_twiddle = 0;
200 }                                              << 
201                                                << 
202                                                << 
203 static void exit_hello12(sos_vaddr_t stack_vad << 
204 {                                              << 
205   sos_cpu_kstate_exit_to(ctxt_main,            << 
206                          (sos_cpu_kstate_funct << 
207                          stack_vaddr);         << 
208 }                                              << 
209                                                << 
210                                                   199 
211 static void hello1 (char *str)                 !! 200   while (1)
212 {                                              << 
213   for ( ; *str != '\n' ; str++)                << 
214     {                                             201     {
215       sos_bochs_printf("hello1: %c\n", *str);  !! 202       /* Remove this instruction if you get an "Invalid opcode" CPU
216       sos_cpu_kstate_switch(& ctxt_hello1, ctx !! 203          exception (old 80386 CPU) */
217     }                                          !! 204       asm("hlt\n");
218                                                << 
219   /* You can uncomment this in case you explic << 
220      now. But returning from the function will << 
221   /* sos_cpu_kstate_exit_to(ctxt_main,         << 
222                          (sos_cpu_kstate_funct << 
223                          hello1_stack); */     << 
224 }                                              << 
225                                                << 
226                                                << 
227 static void hello2 (char *str)                 << 
228 {                                              << 
229   for ( ; *str != '\n' ; str++)                << 
230     {                                          << 
231       sos_bochs_printf("hello2: %c\n", *str);  << 
232       sos_cpu_kstate_switch(& ctxt_hello2, ctx << 
233     }                                          << 
234                                                << 
235   /* You can uncomment this in case you explic << 
236      now. But returning from the function will << 
237   /* sos_cpu_kstate_exit_to(ctxt_main,         << 
238                          (sos_cpu_kstate_funct << 
239                          hello2_stack); */     << 
240 }                                              << 
241                                                << 
242                                                << 
243 void print_hello_world ()                      << 
244 {                                              << 
245 #define DEMO_STACK_SIZE 1024                   << 
246   /* Allocate the stacks */                    << 
247   hello1_stack = sos_kmalloc(DEMO_STACK_SIZE,  << 
248   hello2_stack = sos_kmalloc(DEMO_STACK_SIZE,  << 
249                                                << 
250   /* Initialize the coroutines' contexts */    << 
251   sos_cpu_kstate_init(&ctxt_hello1,            << 
252                       (sos_cpu_kstate_function << 
253                       (sos_ui32_t) "Hlowrd",   << 
254                       (sos_vaddr_t) hello1_sta << 
255                       (sos_cpu_kstate_function << 
256                       (sos_ui32_t) hello1_stac << 
257   sos_cpu_kstate_init(&ctxt_hello2,            << 
258                       (sos_cpu_kstate_function << 
259                       (sos_ui32_t) "el ol\n",  << 
260                       (sos_vaddr_t) hello2_sta << 
261                       (sos_cpu_kstate_function << 
262                       (sos_ui32_t) hello2_stac << 
263                                                << 
264   /* Go to first coroutine */                  << 
265   sos_bochs_printf("Printing Hello World\\n... << 
266   sos_cpu_kstate_switch(& ctxt_main, ctxt_hell << 
267                                                << 
268   /* The first coroutine to reach the '\n' swi << 
269   sos_bochs_printf("Back in main !\n");        << 
270 }                                              << 
271                                                << 
272                                                << 
273 /* =========================================== << 
274  * Generate page faults on an unmapped but all << 
275  * region, which results in a series of physic << 
276  * faulted pages.                              << 
277  */                                            << 
278 static void test_demand_paging(int nb_alloc_vp << 
279 {                                              << 
280   int i;                                       << 
281   sos_vaddr_t base_vaddr;                      << 
282                                                << 
283   sos_x86_videomem_printf(10, 0,               << 
284                           SOS_X86_VIDEO_BG_BLU << 
285                           "Demand paging test  << 
286                           nb_alloc_vpages >> 8 << 
287                                                << 
288   /* Allocate virtual memory */                << 
289   base_vaddr = sos_kmem_vmm_alloc(nb_alloc_vpa << 
290                                                << 
291   SOS_ASSERT_FATAL(base_vaddr != (sos_vaddr_t) << 
292   sos_x86_videomem_printf(11, 0,               << 
293                           SOS_X86_VIDEO_BG_BLU << 
294                           "Allocated virtual r << 
295                           base_vaddr,          << 
296                           base_vaddr + nb_allo << 
297                                                   205 
298   /* Now use part of it in physical memory */  !! 206       idle_twiddle ++;
299   for (i = 0 ; (i < nb_alloc_ppages) && (i < n !! 207       display_bits(0, 0, SOS_X86_VIDEO_FG_GREEN | SOS_X86_VIDEO_BG_BLUE,
300     {                                          !! 208                    idle_twiddle);
301       /* Compute an address inside the range * !! 209       
302       sos_ui32_t *value, j;                    !! 210       /* Lend the CPU to some other thread */
303       sos_vaddr_t vaddr = base_vaddr;          !! 211       sos_thread_yield();
304       vaddr += (nb_alloc_vpages - (i + 1))*SOS << 
305       vaddr += 2345;                           << 
306                                                << 
307       sos_x86_videomem_printf(12, 0,           << 
308                               SOS_X86_VIDEO_BG << 
309                               "Writing %d at v << 
310                               i, vaddr);       << 
311                                                << 
312       /* Write at this address */              << 
313       value = (sos_ui32_t*)vaddr;              << 
314       *value = i;                              << 
315                                                << 
316       /* Yep ! A new page should normally have << 
317       sos_x86_videomem_printf(13, 0,           << 
318                               SOS_X86_VIDEO_BG << 
319                               "Value read at a << 
320                               vaddr, (unsigned << 
321     }                                             212     }
322                                                << 
323   SOS_ASSERT_FATAL(SOS_OK == sos_kmem_vmm_free << 
324   /* Yep ! A new page should normally have bee << 
325   sos_x86_videomem_printf(14, 0,               << 
326                           SOS_X86_VIDEO_BG_BLU << 
327                           "Done (area un-alloc << 
328 }                                                 213 }
329                                                   214 
330                                                   215 
331                                                << 
332 /* ===========================================    216 /* ======================================================================
333  * Shows how the backtrace stuff works         !! 217  * Kernel thread showing some CPU usage statistics on the console every 1s
334  */                                               218  */
335                                                !! 219 #define LOAD_DISPLAY_BASELINE  4
336 /* Recursive function. Print the backtrace fro !! 220 #define LOAD_DISPLAY_STARTROW 34
337 static void test_backtrace(int i, int magic, s !! 221 static void stat_thread()
338                            sos_size_t  stack_s !! 222 {
339 {                                              !! 223   while (1)
340   if (i <= 0)                                  !! 224     {
341     {                                          !! 225       sos_ui32_t flags;
342       /* The page fault exception handler will !! 226       sos_ui32_t load1, load5, load15;
343          this function, because address 0x42 i !! 227       char str1[11], str5[11], str15[11];
344       *((char*)0x42) = 12;                     !! 228       struct sos_time t;
345                                                !! 229       t.sec = 1;
346       /* More direct variant: */               !! 230       t.nanosec = 0;
347       /* dump_backtrace(NULL, stack_bottom, st !! 231 
                                                   >> 232       sos_thread_sleep(& t);
                                                   >> 233 
                                                   >> 234       sos_disable_IRQs(flags);
                                                   >> 235 
                                                   >> 236       /* The IDLE task is EXcluded in the following computation */
                                                   >> 237       sos_load_get_sload(&load1, &load5, &load15);
                                                   >> 238       sos_load_to_string(str1, load1);
                                                   >> 239       sos_load_to_string(str5, load5);
                                                   >> 240       sos_load_to_string(str15, load15);
                                                   >> 241       sos_x86_videomem_printf(LOAD_DISPLAY_BASELINE+0, LOAD_DISPLAY_STARTROW,
                                                   >> 242                               SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
                                                   >> 243                               "Kernel (- Idle): %s %s %s    ",
                                                   >> 244                               str1, str5, str15);
                                                   >> 245 
                                                   >> 246       sos_load_get_uload(&load1, &load5, &load15);
                                                   >> 247       sos_load_to_string(str1, load1);
                                                   >> 248       sos_load_to_string(str5, load5);
                                                   >> 249       sos_load_to_string(str15, load15);
                                                   >> 250       sos_x86_videomem_printf(LOAD_DISPLAY_BASELINE+1, LOAD_DISPLAY_STARTROW,
                                                   >> 251                               SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
                                                   >> 252                               "User: %s %s %s        ",
                                                   >> 253                               str1, str5, str15);
                                                   >> 254 
                                                   >> 255       sos_load_get_uratio(&load1, &load5, &load15);
                                                   >> 256       sos_load_to_string(str1, load1);
                                                   >> 257       sos_load_to_string(str5, load5);
                                                   >> 258       sos_load_to_string(str15, load15);
                                                   >> 259       sos_x86_videomem_printf(LOAD_DISPLAY_BASELINE+2, LOAD_DISPLAY_STARTROW,
                                                   >> 260                               SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
                                                   >> 261                               "User CPU %%: %s %s %s        ",
                                                   >> 262                               str1, str5, str15);
                                                   >> 263 
                                                   >> 264       /* The IDLE task is INcluded in the following computation */
                                                   >> 265       sos_load_get_sratio(&load1, &load5, &load15);
                                                   >> 266       sos_load_to_string(str1, load1);
                                                   >> 267       sos_load_to_string(str5, load5);
                                                   >> 268       sos_load_to_string(str15, load15);
                                                   >> 269       sos_x86_videomem_printf(LOAD_DISPLAY_BASELINE+3, LOAD_DISPLAY_STARTROW,
                                                   >> 270                               SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
                                                   >> 271                               "Kernel CPU %% (+ Idle): %s %s %s    ",
                                                   >> 272                               str1, str5, str15);
                                                   >> 273       sos_restore_IRQs(flags);
348     }                                             274     }
349   else                                         << 
350     test_backtrace(i-1, magic, stack_bottom, s << 
351 }                                                 275 }
352                                                   276 
353                                                   277 
354 /* ===========================================    278 /* ======================================================================
355  * Parsing of Mathematical expressions         !! 279  * Start the "init" (userland) process
356  *                                             << 
357  * This is a recursive lexer/parser/evaluator  << 
358  * expressions. Supports both binary +/-* and  << 
359  * well as parentheses.                        << 
360  *                                             << 
361  * Terminal tokens (Lexer):                    << 
362  *  - Number: positive integer number          << 
363  *  - Variable: ascii name (regexp: [a-zA-Z]+) << 
364  *  - Operator: +*-/                           << 
365  *  - Opening/closing parentheses              << 
366  *                                             << 
367  * Grammar (Parser):                           << 
368  *  Expression ::= Term E'                     << 
369  *  Expr_lr    ::= + Term Expr_lr | - Term Exp << 
370  *  Term       ::= Factor Term_lr              << 
371  *  Term_lr    ::= * Factor Term_lr | / Factor << 
372  *  Factor     ::= - Factor | + Factor | Scala << 
373  *  Scalar     ::= Number | Variable           << 
374  *                                             << 
375  * Note. This is the left-recursive equivalent << 
376  *  Expression ::= Expression + Term | Express << 
377  *  Term       ::= Term * Factor | Term / Fact << 
378  *  factor     ::= - Factor | + Factor | Scala << 
379  *  Scalar     ::= Number | Variable           << 
380  *                                             << 
381  * The parsing is composed of a 3 stages pipel << 
382  *  - The reader: reads a string 1 character a << 
383  *    the control back to lexer after each cha << 
384  *    interest in using coroutines, because it << 
385  *    implicitely stored in the stack between  << 
386  *  - The lexer: consumes the characters from  << 
387  *    the terminal tokens, 1 token at a time,  << 
388  *    to the parser after each token. This fun << 
389  *    in using coroutines, because its state ( << 
390  *    implicitely stored in the stack between  << 
391  *  - The parser: consumes the tokens from the << 
392  *    syntax tree of the expression. There is  << 
393  *    interest in defining a coroutine devoted << 
394  *    do use one for that because this allows  << 
395  *    deeper stack. Actually, the parser is hi << 
396  *    the default 16kB stack of the sos_main() << 
397  *    enough. Here, we switch to a 64kB stack, << 
398  *    recursive functions. The Parser uses int << 
399  *    are defined and implemented as internal  << 
400  *    just for the sake of clarity, and is abs << 
401  *    the algorithm: one can transfer these fu << 
402  *    function without restriction.            << 
403  *                                             << 
404  * The evaluator is another recursive function << 
405  * parser's stack to evaluate the parsed expre << 
406  * values for the variables present in the exp << 
407  * parser function, this function defines and  << 
408  * which can be extracted from the main evalua << 
409  *                                             << 
410  * All these functions support a kind of "exce << 
411  * something goes wrong, control is transferre << 
412  * sos_main() context, without unrolling the r << 
413  * how exceptions basically work, but one shou << 
414  * a reference exceptions implementation. Real << 
415  * (such as that in the C++ language) call the << 
416  * objects allocated on the stack during the " << 
417  * upon exception handling, which complicates  << 
418  * don't have real Objects here (in the OOP se << 
419  * destructors), so we don't have to complicat << 
420  *                                             << 
421  * After this little coroutine demo, one shoul << 
422  * a low-level manual direct manipulation of s << 
423  * probably mess up the whole kernel to do wha << 
424  * resources such as mutex/semaphore won't be  << 
425  * ...). Higher level "kernel thread" primitiv << 
426  * presented, which provide a higher-level set << 
427  * contexts. You'll have to use EXCLUSIVELY th << 
428  * need a huge stack to do recursion for examp << 
429  * think of changing manually the stack for so << 
430  * rethink your algorithm, making it non-recur << 
431  */                                            << 
432                                                << 
433                                                << 
434 /* The stacks involved */                      << 
435 static char stack_reader[1024];                << 
436 static char stack_lexer[1024];                 << 
437 static char deep_stack[65536]; /* For the pars << 
438                                                << 
439 /* The CPU states for the various coroutines * << 
440 static struct sos_cpu_kstate *st_reader, *st_l << 
441   *st_eval, *st_free, *st_main;                << 
442                                                << 
443                                                << 
444 /*                                             << 
445  * Default exit/reclaim functions: return cont << 
446  * context                                     << 
447  */                                               280  */
448 static void reclaim(int unused)                !! 281 static sos_ret_t
                                                   >> 282 start_init(struct sos_fs_manager_instance * rootfs)
449 {                                                 283 {
450 }                                              !! 284   sos_ret_t retval;
451 static void func_exit(sos_ui32_t unused)       !! 285   struct sos_umem_vmm_as *as_init;
452 {                                              !! 286   struct sos_process *proc_init;
453   sos_cpu_kstate_exit_to(st_main, (sos_cpu_kst !! 287   struct sos_thread *new_thr;
454 }                                              !! 288   sos_uaddr_t ustack, start_uaddr;
                                                   >> 289   struct sos_fs_opened_file * init_root, * init_cwd, * unused_of;
455                                                   290 
                                                   >> 291   /* Create the new process */
                                                   >> 292   proc_init = sos_process_create("init", FALSE);
                                                   >> 293   if (! proc_init)
                                                   >> 294     return -SOS_ENOMEM;
                                                   >> 295   as_init = sos_process_get_address_space(proc_init);
456                                                   296 
457 /*                                             << 
458  * The reader coroutine and associated variabl << 
459  * have been a normal function, except that th << 
460  * character would have to be stored somewhere << 
461  */                                            << 
462 static char data_reader_to_lexer;              << 
463                                                   297 
464 static void func_reader(const char *str)       !! 298   /*
465 {                                              !! 299    * Setup the root and CWD directories of the process. The root of
466   for ( ; str && (*str != '\0') ; str++)       !! 300    * this process will correspond to the "global" root of the whole
                                                   >> 301    * system since all the future processes will duplicate it !
                                                   >> 302    */
                                                   >> 303   retval = sos_fs_new_opened_file(proc_init, rootfs->root,
                                                   >> 304                                   SOS_FS_OPEN_READ | SOS_FS_OPEN_WRITE,
                                                   >> 305                                   & init_root);
                                                   >> 306   if (SOS_OK != retval)
467     {                                             307     {
468       data_reader_to_lexer = *str;             !! 308       sos_process_unref(proc_init);
469       sos_cpu_kstate_switch(& st_reader, st_le !! 309       return -SOS_ENOENT;
470     }                                             310     }
471                                                   311 
472   data_reader_to_lexer = '\0';                 !! 312   /* Duplicate the root file to set the current working directory of
473   sos_cpu_kstate_switch(& st_reader, st_lexer) !! 313      the init process */
474 }                                              !! 314   retval = sos_fs_duplicate_opened_file(init_root, proc_init,
475                                                !! 315                                         & init_cwd);
476                                                !! 316   if (SOS_OK != retval)
477 /*                                             << 
478  * The Lexer coroutine and associated types/va << 
479  * could have been a normal function, except t << 
480  * character, token and previous token would h << 
481  * somewhere.                                  << 
482  */                                            << 
483 #define STR_VAR_MAXLEN 16                      << 
484 static struct lex_elem                         << 
485 {                                              << 
486   enum { LEX_IS_NUMBER, LEX_IS_OPER, LEX_IS_VA << 
487          LEX_IS_OPENPAR, LEX_IS_CLOSEPAR, LEX_ << 
488   union {                                      << 
489     int  number;                               << 
490     char operator;                             << 
491     char var[STR_VAR_MAXLEN];                  << 
492   };                                           << 
493 } data_lexer_to_parser;                        << 
494                                                << 
495 static void func_lexer(sos_ui32_t unused)      << 
496 {                                              << 
497   char c;                                      << 
498   enum { GOT_SPACE, GOT_NUM, GOT_OP, GOT_STR,  << 
499          GOT_OPENPAR, GOT_CLOSEPAR } got_what, << 
500                                                << 
501   data_lexer_to_parser.number = 0;             << 
502   got_what_before = GOT_SPACE;                 << 
503   do                                           << 
504     {                                             317     {
505       /* Consume one character from the reader !! 318       sos_fs_close(init_root);
506       sos_cpu_kstate_switch(& st_lexer, st_rea !! 319       sos_process_unref(proc_init);
507       c = data_reader_to_lexer;                !! 320       return -SOS_ENOENT;
508                                                << 
509       /* Classify the consumed character */    << 
510       if ( (c >= '0') && (c <= '9') )          << 
511         got_what = GOT_NUM;                    << 
512       else if ( (c == '+') || (c == '-') || (c << 
513         got_what = GOT_OP;                     << 
514       else if ( ( (c >= 'a') && (c <= 'z') )   << 
515                 || ( (c >= 'A') && (c <= 'Z')  << 
516         got_what = GOT_STR;                    << 
517       else if (c == '(')                       << 
518         got_what = GOT_OPENPAR;                << 
519       else if (c == ')')                       << 
520         got_what = GOT_CLOSEPAR;               << 
521       else                                     << 
522         got_what = GOT_SPACE;                  << 
523                                                << 
524       /* Determine whether the current token i << 
525       if ( (got_what != got_what_before)       << 
526            || (got_what_before == GOT_OP)      << 
527            || (got_what_before == GOT_OPENPAR) << 
528            || (got_what_before == GOT_CLOSEPAR << 
529         {                                      << 
530           /* return control back to the parser << 
531              has been recognized */            << 
532           if ( (got_what_before != GOT_SPACE)  << 
533             sos_cpu_kstate_switch(& st_lexer,  << 
534                                                << 
535           data_lexer_to_parser.number = 0;     << 
536         }                                      << 
537                                                << 
538       /* Update the token being currently reco << 
539       if (got_what == GOT_OP)                  << 
540         {                                      << 
541           data_lexer_to_parser.type = LEX_IS_O << 
542           data_lexer_to_parser.operator = c;   << 
543         }                                      << 
544       else if (got_what == GOT_NUM)            << 
545         {                                      << 
546           data_lexer_to_parser.type = LEX_IS_N << 
547           data_lexer_to_parser.number *= 10;   << 
548           data_lexer_to_parser.number += (c -  << 
549         }                                      << 
550       else if (got_what == GOT_STR)            << 
551         {                                      << 
552           char to_cat[] = { c, '\0' };         << 
553           data_lexer_to_parser.type = LEX_IS_V << 
554           strzcat(data_lexer_to_parser.var, to << 
555         }                                      << 
556       else if (got_what == GOT_OPENPAR)        << 
557         data_lexer_to_parser.type = LEX_IS_OPE << 
558       else if (got_what == GOT_CLOSEPAR)       << 
559         data_lexer_to_parser.type = LEX_IS_CLO << 
560                                                << 
561       got_what_before = got_what;              << 
562     }                                             321     }
563   while (c != '\0');                           << 
564                                                << 
565   /* Transfer last recognized token to the par << 
566   if ( (got_what_before != GOT_SPACE) )        << 
567     sos_cpu_kstate_switch(& st_lexer, st_parse << 
568                                                << 
569   /* Signal that no more token are available * << 
570   data_lexer_to_parser.type = LEX_END;         << 
571   sos_cpu_kstate_switch(& st_lexer, st_parser) << 
572                                                << 
573   /* Exception: parser asks for a token AFTER  << 
574      one */                                    << 
575   sos_bochs_printf("Error: end of string alrea << 
576   sos_cpu_kstate_switch(& st_lexer, st_main);  << 
577 }                                              << 
578                                                   322 
579                                                !! 323   /* Now update the process ! */
580 /*                                             !! 324   if ( ( SOS_OK != sos_process_chroot(proc_init, init_root, & unused_of) )
581  * The Parser coroutine and associated types/v !! 325        || ( SOS_OK != sos_process_chdir(proc_init, init_cwd, & unused_of) ) )
582  */                                            << 
583 struct syntax_node                             << 
584 {                                              << 
585   enum { YY_IS_BINOP, YY_IS_UNAROP, YY_IS_NUM, << 
586   union                                        << 
587   {                                            << 
588     int  number;                               << 
589     char var[STR_VAR_MAXLEN];                  << 
590     struct                                     << 
591     {                                          << 
592       char op;                                 << 
593       struct syntax_node *parm_left, *parm_rig << 
594     } binop;                                   << 
595     struct                                     << 
596     {                                          << 
597       char op;                                 << 
598       struct syntax_node *parm;                << 
599     } unarop;                                  << 
600   };                                           << 
601 };                                             << 
602                                                << 
603 static void func_parser(struct syntax_node **  << 
604 {                                              << 
605   static struct syntax_node *alloc_node_num(in << 
606   static struct syntax_node *alloc_node_var(co << 
607   static struct syntax_node *alloc_node_binop( << 
608                                                << 
609                                                << 
610   static struct syntax_node *alloc_node_unarop << 
611                                                << 
612   static struct syntax_node * get_expr();      << 
613   static struct syntax_node * get_expr_lr(stru << 
614   static struct syntax_node * get_term();      << 
615   static struct syntax_node * get_term_lr(stru << 
616   static struct syntax_node * get_factor();    << 
617   static struct syntax_node * get_scalar();    << 
618                                                << 
619   /* Create a new node to store a number */    << 
620   static struct syntax_node *alloc_node_num(in << 
621     {                                          << 
622       struct syntax_node *n                    << 
623         = (struct syntax_node*) sos_kmalloc(si << 
624       n->type   = YY_IS_NUM;                   << 
625       n->number = val;                         << 
626       return n;                                << 
627     }                                          << 
628   /* Create a new node to store a variable */  << 
629   static struct syntax_node *alloc_node_var(co << 
630     {                                          << 
631       struct syntax_node *n                    << 
632         = (struct syntax_node*) sos_kmalloc(si << 
633       n->type   = YY_IS_VAR;                   << 
634       strzcpy(n->var, name, STR_VAR_MAXLEN);   << 
635       return n;                                << 
636     }                                          << 
637   /* Create a new node to store a binary opera << 
638   static struct syntax_node *alloc_node_binop( << 
639                                                << 
640                                                << 
641     {                                             326     {
642       struct syntax_node *n                    !! 327       sos_fs_close(init_root);
643         = (struct syntax_node*) sos_kmalloc(si !! 328       sos_fs_close(init_cwd);
644       n->type             = YY_IS_BINOP;       !! 329       sos_process_chroot(proc_init, NULL, & unused_of);
645       n->binop.op         = op;                !! 330       sos_process_chdir(proc_init, NULL, & unused_of);
646       n->binop.parm_left  = parm_left;         !! 331       sos_process_unref(proc_init);
647       n->binop.parm_right = parm_right;        !! 332       return -SOS_ENOENT;
648       return n;                                << 
649     }                                          << 
650   /* Create a new node to store a unary operat << 
651   static struct syntax_node *alloc_node_unarop << 
652                                                << 
653     {                                          << 
654       struct syntax_node *n                    << 
655         = (struct syntax_node*) sos_kmalloc(si << 
656       n->type        = YY_IS_UNAROP;           << 
657       n->unarop.op   = op;                     << 
658       n->unarop.parm = parm;                   << 
659       return n;                                << 
660     }                                             333     }
661                                                   334 
662   /* Raise an exception: transfer control back << 
663      without unrolling the whole recursion */  << 
664   static void parser_exception(const char *str << 
665     {                                          << 
666       sos_bochs_printf("Parser exception: %s\n << 
667       sos_cpu_kstate_switch(& st_parser, st_ma << 
668     }                                          << 
669                                                   335 
670   /* Consume the current terminal "number" tok << 
671      token */                                  << 
672   static int get_number()                      << 
673     {                                          << 
674       int v;                                   << 
675       if (data_lexer_to_parser.type != LEX_IS_ << 
676         parser_exception("Expected number");   << 
677       v = data_lexer_to_parser.number;         << 
678       sos_cpu_kstate_switch(& st_parser, st_le << 
679       return v;                                << 
680     }                                          << 
681   /* Consume the current terminal "variable" t << 
682      token */                                  << 
683   static void get_str(char name[STR_VAR_MAXLEN << 
684     {                                          << 
685       if (data_lexer_to_parser.type != LEX_IS_ << 
686         parser_exception("Expected variable"); << 
687       strzcpy(name, data_lexer_to_parser.var,  << 
688       sos_cpu_kstate_switch(& st_parser, st_le << 
689     }                                          << 
690   /* Consume the current terminal "operator" t << 
691      token */                                  << 
692   static char get_op()                         << 
693     {                                          << 
694       char op;                                 << 
695       if (data_lexer_to_parser.type != LEX_IS_ << 
696         parser_exception("Expected operator"); << 
697       op = data_lexer_to_parser.operator;      << 
698       sos_cpu_kstate_switch(& st_parser, st_le << 
699       return op;                               << 
700     }                                          << 
701   /* Consume the current terminal "parenthese" << 
702      token */                                  << 
703   static void get_par()                        << 
704     {                                          << 
705       if ( (data_lexer_to_parser.type != LEX_I << 
706            && (data_lexer_to_parser.type != LE << 
707         parser_exception("Expected parenthese" << 
708       sos_cpu_kstate_switch(& st_parser, st_le << 
709     }                                          << 
710                                                   336 
711   /* Parse an Expression */                    !! 337   /* Map the 'init' program in user space */
712   static struct syntax_node * get_expr()       !! 338   start_uaddr = sos_binfmt_elf32_map(as_init, "init");
713     {                                          !! 339   if (0 == start_uaddr)
714       struct syntax_node *t = get_term();      << 
715       return get_expr_lr(t);                   << 
716     }                                          << 
717   /* Parse an Expr_lr */                       << 
718   static struct syntax_node * get_expr_lr(stru << 
719     {                                          << 
720       if ( (data_lexer_to_parser.type == LEX_I << 
721            && ( (data_lexer_to_parser.operator << 
722                 || (data_lexer_to_parser.opera << 
723         {                                      << 
724           char op = get_op();                  << 
725           struct syntax_node *term = get_term( << 
726           struct syntax_node *node_op = alloc_ << 
727           return get_expr_lr(node_op);         << 
728         }                                      << 
729       return n;                                << 
730     }                                          << 
731   /* Parse a Term */                           << 
732   static struct syntax_node * get_term()       << 
733     {                                             340     {
734       struct syntax_node *f1 = get_factor();   !! 341       sos_process_unref(proc_init);
735       return get_term_lr(f1);                  !! 342       return -SOS_ENOENT;
736     }                                             343     }
737   /* Parse a Term_lr */                        << 
738   static struct syntax_node * get_term_lr(stru << 
739     {                                          << 
740       if ( (data_lexer_to_parser.type == LEX_I << 
741            && ( (data_lexer_to_parser.operator << 
742                 || (data_lexer_to_parser.opera << 
743         {                                      << 
744           char op = get_op();                  << 
745           struct syntax_node *factor = get_fac << 
746           struct syntax_node *node_op = alloc_ << 
747           return get_term_lr(node_op);         << 
748         }                                      << 
749       return n;                                << 
750     }                                          << 
751   /* Parse a Factor */                         << 
752   static struct syntax_node * get_factor()     << 
753     {                                          << 
754       if ( (data_lexer_to_parser.type == LEX_I << 
755            && ( (data_lexer_to_parser.operator << 
756                 || (data_lexer_to_parser.opera << 
757         { char op = data_lexer_to_parser.opera << 
758         get_op(); return alloc_node_unarop(op, << 
759       else if (data_lexer_to_parser.type == LE << 
760         {                                      << 
761           struct syntax_node *expr;            << 
762           get_par();                           << 
763           expr = get_expr();                   << 
764           if (data_lexer_to_parser.type != LEX << 
765             parser_exception("Mismatched paren << 
766           get_par();                           << 
767           return expr;                         << 
768         }                                      << 
769                                                << 
770       return get_scalar();                     << 
771     }                                          << 
772   /* Parse a Scalar */                         << 
773   static struct syntax_node * get_scalar()     << 
774     {                                          << 
775       if (data_lexer_to_parser.type != LEX_IS_ << 
776         {                                      << 
777           char var[STR_VAR_MAXLEN];            << 
778           get_str(var);                        << 
779           return alloc_node_var(var);          << 
780         }                                      << 
781       return alloc_node_num(get_number());     << 
782     }                                          << 
783                                                << 
784                                                << 
785   /*                                           << 
786    * Body of the function                      << 
787    */                                          << 
788                                                << 
789   /* Get the first token */                    << 
790   sos_cpu_kstate_switch(& st_parser, st_lexer) << 
791                                                << 
792   /* Begin the parsing ! */                    << 
793   *syntax_tree = get_expr();                   << 
794   /* The result is returned in the syntax_tree << 
795 }                                              << 
796                                                << 
797                                                << 
798 /*                                             << 
799  * Setup the parser's pipeline                 << 
800  */                                            << 
801 static struct syntax_node * parse_expression(c << 
802 {                                              << 
803   struct syntax_node *retval = NULL;           << 
804                                                << 
805   /* Build the context of the functions in the << 
806   sos_cpu_kstate_init(& st_reader,             << 
807                       (sos_cpu_kstate_function << 
808                       (sos_ui32_t)expr,        << 
809                       (sos_vaddr_t)stack_reade << 
810                       (sos_cpu_kstate_function << 
811   sos_cpu_kstate_init(& st_lexer,              << 
812                       (sos_cpu_kstate_function << 
813                       0,                       << 
814                       (sos_vaddr_t)stack_lexer << 
815                       (sos_cpu_kstate_function << 
816   sos_cpu_kstate_init(& st_parser,             << 
817                       (sos_cpu_kstate_function << 
818                       (sos_ui32_t) /* syntax t << 
819                       (sos_vaddr_t)deep_stack, << 
820                       (sos_cpu_kstate_function << 
821                                                << 
822   /* Parse the expression */                   << 
823   sos_cpu_kstate_switch(& st_main, st_parser); << 
824   return retval;                               << 
825 }                                              << 
826                                                   344 
827                                                !! 345   /* Allocate the user stack */
828 /*                                             !! 346   ustack = (SOS_PAGING_TOP_USER_ADDRESS - SOS_DEFAULT_USER_STACK_SIZE) + 1;
829  * The Evaluator coroutine and associated type !! 347   retval = sos_dev_zero_map(as_init, &ustack, SOS_DEFAULT_USER_STACK_SIZE,
830  */                                            !! 348                             SOS_VM_MAP_PROT_READ | SOS_VM_MAP_PROT_WRITE,
831 struct func_eval_params                        !! 349                             /* PRIVATE */ 0);
832 {                                              !! 350   if (SOS_OK != retval)
833   const struct syntax_node *e;                 << 
834   const char **var_name;                       << 
835   int *var_val;                                << 
836   int nb_vars;                                 << 
837                                                << 
838   int result;                                  << 
839 };                                             << 
840                                                << 
841 static void func_eval(struct func_eval_params  << 
842 {                                              << 
843   /* The internal (recursive) nested function  << 
844      the syntax tree */                        << 
845   static int rec_eval(const struct syntax_node << 
846                       const char* var_name[],  << 
847     {                                             351     {
848       switch (n->type)                         !! 352       sos_process_unref(proc_init);
849         {                                      !! 353       return -SOS_ENOMEM;
850         case YY_IS_NUM:                        << 
851           return n->number;                    << 
852                                                << 
853         case YY_IS_VAR:                        << 
854           {                                    << 
855             int i;                             << 
856             for (i = 0 ; i < nb_vars ; i++)    << 
857               if (0 == strcmp(var_name[i], n-> << 
858                 return var_val[i];             << 
859                                                << 
860             /* Exception: no variable with tha << 
861             sos_bochs_printf("ERROR: unknown v << 
862             sos_cpu_kstate_switch(& st_eval, s << 
863           }                                    << 
864                                                << 
865         case YY_IS_BINOP:                      << 
866           {                                    << 
867             int left = rec_eval(n->binop.parm_ << 
868                                 var_name, var_ << 
869             int right = rec_eval(n->binop.parm << 
870                                  var_name, var << 
871             switch (n->binop.op)               << 
872               {                                << 
873               case '+': return left + right;   << 
874               case '-': return left - right;   << 
875               case '*': return left * right;   << 
876               case '/': return left / right;   << 
877               default:                         << 
878                 /* Exception: no such operator << 
879                 sos_bochs_printf("ERROR: unkno << 
880                 sos_cpu_kstate_switch(& st_eva << 
881               }                                << 
882           }                                    << 
883                                                << 
884         case YY_IS_UNAROP:                     << 
885           {                                    << 
886             int arg = rec_eval(n->unarop.parm, << 
887             switch (n->unarop.op)              << 
888               {                                << 
889               case '-': return -arg;           << 
890               case '+': return arg;            << 
891               default:                         << 
892                 /* Exception: no such operator << 
893                 sos_bochs_printf("ERROR: unkno << 
894                 sos_cpu_kstate_switch(& st_eva << 
895               }                                << 
896           }                                    << 
897         }                                      << 
898                                                << 
899       /* Exception: no such syntax node (INTER << 
900       sos_bochs_printf("ERROR: invalid node ty << 
901       sos_cpu_kstate_switch(& st_eval, st_main << 
902       return -1; /* let's make gcc happy */    << 
903     }                                             354     }
904                                                   355 
905                                                !! 356   /* Now create the user thread */
906   /*                                           !! 357   new_thr = sos_create_user_thread(NULL,
907    * Function BODY                             !! 358                                    proc_init,
908    */                                          !! 359                                    start_uaddr,
909   /* Update p.result returned back to calling  !! 360                                    0, 0,
910   parms->result                                !! 361                                    ustack + SOS_DEFAULT_USER_STACK_SIZE - 4,
911     = rec_eval(parms->e, parms->var_name, parm !! 362                                    SOS_SCHED_PRIO_TS_LOWEST);
912 }                                              !! 363   if (! new_thr)
913                                                << 
914 /*                                             << 
915  * Change the stack for something larger in or << 
916  * recursive function above in a safe way      << 
917  */                                            << 
918 static int eval_expression(const struct syntax << 
919                            const char* var_nam << 
920 {                                              << 
921   struct func_eval_params p                    << 
922     = (struct func_eval_params){ .e=e,         << 
923                                  .var_name=var << 
924                                  .var_val=var_ << 
925                                  .nb_vars=nb_v << 
926                                  .result = 0 } << 
927                                                << 
928   sos_cpu_kstate_init(& st_eval,               << 
929                       (sos_cpu_kstate_function << 
930                       (sos_ui32_t)/* p.result  << 
931                       (sos_vaddr_t)deep_stack, << 
932                       (sos_cpu_kstate_function << 
933                                                << 
934   /* Go ! */                                   << 
935   sos_cpu_kstate_switch(& st_main, st_eval);   << 
936   return p.result;                             << 
937 }                                              << 
938                                                << 
939                                                << 
940 /*                                             << 
941  * Function to free the syntax tree            << 
942  */                                            << 
943 static void func_free(struct syntax_node *n)   << 
944 {                                              << 
945   switch (n->type)                             << 
946     {                                             364     {
947     case YY_IS_NUM:                            !! 365       sos_process_unref(proc_init);
948     case YY_IS_VAR:                            !! 366       return -SOS_ENOMEM;
949       break;                                   << 
950                                                << 
951     case YY_IS_BINOP:                          << 
952       func_free(n->binop.parm_left);           << 
953       func_free(n->binop.parm_right);          << 
954       break;                                   << 
955                                                << 
956     case YY_IS_UNAROP:                         << 
957       func_free(n->unarop.parm);               << 
958       break;                                   << 
959     }                                             367     }
960                                                << 
961   sos_kfree((sos_vaddr_t)n);                   << 
962 }                                              << 
963                                                   368 
964 /*                                             !! 369   sos_process_unref(proc_init);
965  * Change the stack for something larger in or !! 370   return SOS_OK;
966  * recursive function above in a safe way      << 
967  */                                            << 
968 static void free_syntax_tree(struct syntax_nod << 
969 {                                              << 
970   sos_cpu_kstate_init(& st_free,               << 
971                       (sos_cpu_kstate_function << 
972                       (sos_ui32_t)tree,        << 
973                       (sos_vaddr_t)deep_stack, << 
974                       (sos_cpu_kstate_function << 
975                                                << 
976   /* Go ! */                                   << 
977   sos_cpu_kstate_switch(& st_main, st_free);   << 
978 }                                                 371 }
979                                                   372 
980                                                   373 
981 /* ===========================================    374 /* ======================================================================
982  * The C entry point of our operating system      375  * The C entry point of our operating system
983  */                                               376  */
984 void sos_main(unsigned long magic, unsigned lo !! 377 void sos_main(unsigned long magic, unsigned long arg)
985 {                                                 378 {
986   unsigned i;                                     379   unsigned i;
987   sos_paddr_t sos_kernel_core_base_paddr, sos_    380   sos_paddr_t sos_kernel_core_base_paddr, sos_kernel_core_top_paddr;
988   struct syntax_node *syntax_tree;             !! 381   struct sos_time tick_resolution;
                                                   >> 382   struct sos_fs_manager_instance * rootfs;
989                                                   383 
990   /* Grub sends us a structure, called multibo !! 384   /* Size of RAM above 1MB. Might be undefined ! */
991      precious informations about the system, s !! 385   unsigned long int upper_mem = 0;
992      documentation for more information. */    << 
993   multiboot_info_t *mbi;                       << 
994   mbi = (multiboot_info_t *) addr;             << 
995                                                   386 
996   /* Setup bochs and console, and clear the co    387   /* Setup bochs and console, and clear the console */
997   sos_bochs_setup();                           !! 388   sos_bochs_subsystem_setup();
998                                                   389 
999   sos_x86_videomem_setup();                       390   sos_x86_videomem_setup();
1000   sos_x86_videomem_cls(SOS_X86_VIDEO_BG_BLUE)    391   sos_x86_videomem_cls(SOS_X86_VIDEO_BG_BLUE);
1001                                                  392 
1002   /* Greetings from SOS */                       393   /* Greetings from SOS */
1003   if (magic == MULTIBOOT_BOOTLOADER_MAGIC)       394   if (magic == MULTIBOOT_BOOTLOADER_MAGIC)
1004     /* Loaded with Grub */                    !! 395     {
1005     sos_x86_videomem_printf(1, 0,             !! 396       /* Grub sends us a structure, called multiboot_info_t with a lot of
1006                             SOS_X86_VIDEO_FG_ !! 397          precious informations about the system, see the multiboot
1007                             "Welcome From GRU !! 398          documentation for more information. */
1008                             "SOS", ',',       !! 399       multiboot_info_t *mbi = (multiboot_info_t *) arg;
1009                             (unsigned)(mbi->m !! 400 
1010                             (unsigned)mbi->me !! 401       /* Multiboot says: "The value returned for upper memory is
                                                   >> 402          maximally the address of the first upper memory hole minus 1
                                                   >> 403          megabyte.". It also adds: "It is not guaranteed to be this
                                                   >> 404          value." aka "YMMV" ;) */
                                                   >> 405       upper_mem = mbi->mem_upper;
                                                   >> 406       sos_x86_videomem_printf(1, 0,
                                                   >> 407                               SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
                                                   >> 408                               "Welcome From GRUB to %s%c RAM is %dMB (upper mem = 0x%x kB)",
                                                   >> 409                               "SOS article 9", ',',
                                                   >> 410                               (unsigned)(upper_mem >> 10) + 1,
                                                   >> 411                               (unsigned)upper_mem);
                                                   >> 412     }
                                                   >> 413   else if (magic == 0x42244224)
                                                   >> 414     {
                                                   >> 415       /* Loaded with SOS bootsect */
                                                   >> 416       upper_mem = arg;
                                                   >> 417       sos_x86_videomem_printf(1, 0,
                                                   >> 418                               SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
                                                   >> 419                               "Welcome to %s%c RAM is %dMB (upper mem = 0x%x kB)",
                                                   >> 420                               "SOS article 9", ',',
                                                   >> 421                               (unsigned)(upper_mem >> 10) + 1,
                                                   >> 422                               (unsigned)upper_mem);
                                                   >> 423     }
1011   else                                           424   else
1012     /* Not loaded with grub */                !! 425     /* Not loaded with grub, not from an enhanced bootsect */
1013     sos_x86_videomem_printf(1, 0,                426     sos_x86_videomem_printf(1, 0,
1014                             SOS_X86_VIDEO_FG_    427                             SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
1015                             "Welcome to SOS") !! 428                             "Welcome to SOS article 9");
1016                                                  429 
1017   sos_bochs_putstring("Message in a bochs\n") !! 430   sos_bochs_putstring("Message in a bochs: This is SOS article 9.\n");
1018                                                  431 
1019   /* Setup CPU segmentation and IRQ subsystem    432   /* Setup CPU segmentation and IRQ subsystem */
1020   sos_gdt_subsystem_setup();                     433   sos_gdt_subsystem_setup();
1021   sos_idt_subsystem_setup();                     434   sos_idt_subsystem_setup();
1022                                                  435 
1023   /* Setup SOS IRQs and exceptions subsystem     436   /* Setup SOS IRQs and exceptions subsystem */
1024   sos_exception_subsystem_setup();               437   sos_exception_subsystem_setup();
1025   sos_irq_subsystem_setup();                     438   sos_irq_subsystem_setup();
1026                                                  439 
1027   /* Configure the timer so as to raise the I    440   /* Configure the timer so as to raise the IRQ0 at a 100Hz rate */
1028   sos_i8254_set_frequency(100);                  441   sos_i8254_set_frequency(100);
1029                                                  442 
1030   /* We need a multiboot-compliant boot loade !! 443   /* Setup the kernel time subsystem to get prepared to take the timer
1031   if (magic != MULTIBOOT_BOOTLOADER_MAGIC)    !! 444      ticks into account */
                                                   >> 445   tick_resolution = (struct sos_time) { .sec=0, .nanosec=10000000UL };
                                                   >> 446   sos_time_subsysem_setup(& tick_resolution);
                                                   >> 447 
                                                   >> 448   /* We need to know the RAM size */
                                                   >> 449   if (upper_mem == 0)
1032     {                                            450     {
1033       sos_x86_videomem_putstring(20, 0,          451       sos_x86_videomem_putstring(20, 0,
1034                                  SOS_X86_VIDE    452                                  SOS_X86_VIDEO_FG_LTRED
1035                                    | SOS_X86_    453                                    | SOS_X86_VIDEO_BG_BLUE
1036                                    | SOS_X86_    454                                    | SOS_X86_VIDEO_FG_BLINKING,
1037                                  "I'm not loa !! 455                                  "I don't know RAM size ! Load me with Grub...");
1038       /* STOP ! */                               456       /* STOP ! */
1039       for (;;)                                   457       for (;;)
1040         continue;                                458         continue;
1041     }                                            459     }
1042                                                  460 
1043   /*                                             461   /*
1044    * Some interrupt handlers                     462    * Some interrupt handlers
1045    */                                            463    */
1046                                                  464 
1047   /* Binding some HW interrupts and exception    465   /* Binding some HW interrupts and exceptions to software routines */
1048   sos_irq_set_routine(SOS_IRQ_TIMER,             466   sos_irq_set_routine(SOS_IRQ_TIMER,
1049                       clk_it);                   467                       clk_it);
1050                                                  468 
1051   /*                                             469   /*
1052    * Setup physical memory management            470    * Setup physical memory management
1053    */                                            471    */
1054                                                  472 
1055   /* Multiboot says: "The value returned for  !! 473   SOS_ASSERT_FATAL(SOS_OK
1056      the address of the first upper memory ho !! 474                    == sos_physmem_subsystem_setup((upper_mem<<10) + (1<<20),
1057      also adds: "It is not guaranteed to be t !! 475                                                   &sos_kernel_core_base_paddr,
1058   sos_physmem_subsystem_setup((mbi->mem_upper !! 476                                                   &sos_kernel_core_top_paddr));
1059                               & sos_kernel_co << 
1060                               & sos_kernel_co << 
1061                                                  477   
1062   /*                                             478   /*
1063    * Switch to paged-memory mode                 479    * Switch to paged-memory mode
1064    */                                            480    */
1065                                                  481 
1066   /* Disabling interrupts should seem more co    482   /* Disabling interrupts should seem more correct, but it's not really
1067      necessary at this stage */                  483      necessary at this stage */
1068   SOS_ASSERT_FATAL(SOS_OK ==                     484   SOS_ASSERT_FATAL(SOS_OK ==
1069                    sos_paging_subsystem_setup    485                    sos_paging_subsystem_setup(sos_kernel_core_base_paddr,
1070                                                  486                                               sos_kernel_core_top_paddr));
1071                                                  487   
1072   /* Bind the page fault exception */            488   /* Bind the page fault exception */
1073   sos_exception_set_routine(SOS_EXCEPT_PAGE_F    489   sos_exception_set_routine(SOS_EXCEPT_PAGE_FAULT,
1074                             pgflt_ex);           490                             pgflt_ex);
1075                                                  491 
1076   /*                                             492   /*
1077    * Setup kernel virtual memory allocator       493    * Setup kernel virtual memory allocator
1078    */                                         !! 494    */
1079                                                  495 
1080   if (sos_kmem_vmm_subsystem_setup(sos_kernel    496   if (sos_kmem_vmm_subsystem_setup(sos_kernel_core_base_paddr,
1081                                    sos_kernel    497                                    sos_kernel_core_top_paddr,
1082                                    bootstrap_    498                                    bootstrap_stack_bottom,
1083                                    bootstrap_    499                                    bootstrap_stack_bottom
1084                                    + bootstra    500                                    + bootstrap_stack_size))
1085     sos_bochs_printf("Could not setup the Ker    501     sos_bochs_printf("Could not setup the Kernel virtual space allocator\n");
1086                                                  502 
1087   if (sos_kmalloc_subsystem_setup())             503   if (sos_kmalloc_subsystem_setup())
1088     sos_bochs_printf("Could not setup the Kma    504     sos_bochs_printf("Could not setup the Kmalloc subsystem\n");
1089                                                  505 
1090   /*                                             506   /*
1091    * Enabling the HW interrupts here, this wi !! 507    * Initialize the MMU context subsystem
1092    * interrupt call our clk_it handler        << 
1093    */                                            508    */
1094   asm volatile ("sti\n");                     !! 509   sos_mm_context_subsystem_setup();
1095                                                  510 
1096   /*                                             511   /*
1097    * Print hello world using coroutines       !! 512    * Initialize the CPU context subsystem
1098    */                                            513    */
1099   print_hello_world();                        !! 514   sos_cpu_context_subsystem_setup();
1100                                               << 
1101                                                  515 
1102   /*                                             516   /*
1103    * Run coroutine tests                      !! 517    * Bind the syscall handler to its software interrupt handler
1104    */                                            518    */
1105   sos_x86_videomem_printf(4, 0,               !! 519   sos_swintr_subsystem_setup();
1106                           SOS_X86_VIDEO_BG_BL << 
1107                           "Coroutine test");  << 
1108   sos_x86_videomem_printf(5, 0,               << 
1109                           SOS_X86_VIDEO_BG_BL << 
1110                           "Parsing...");      << 
1111   syntax_tree = parse_expression(" -  ( (69/  << 
1112                                                  520 
1113   if (syntax_tree != NULL)                    << 
1114     {                                         << 
1115       sos_x86_videomem_printf(6, 0,           << 
1116                               SOS_X86_VIDEO_B << 
1117                               "Evaluating..." << 
1118       sos_x86_videomem_printf(7, 0,           << 
1119                               SOS_X86_VIDEO_B << 
1120                               "Result=%d (if  << 
1121                               eval_expression << 
1122                                               << 
1123                                               << 
1124                                               << 
1125       free_syntax_tree(syntax_tree);          << 
1126       sos_x86_videomem_printf(8, 0,           << 
1127                               SOS_X86_VIDEO_B << 
1128                               "Done (un-alloc << 
1129     }                                         << 
1130   else                                        << 
1131     {                                         << 
1132       sos_x86_videomem_printf(6, 0,           << 
1133                               SOS_X86_VIDEO_B << 
1134                               "Error in parsi << 
1135     }                                         << 
1136                                                  521 
1137   /*                                             522   /*
1138    * Run some demand-paging tests             !! 523    * Initialize the Kernel thread and scheduler subsystems
1139    */                                            524    */
1140   test_demand_paging(234567, 500);            !! 525   
                                                   >> 526   /* Initialize kernel thread subsystem */
                                                   >> 527   sos_thread_subsystem_setup(bootstrap_stack_bottom,
                                                   >> 528                              bootstrap_stack_size);
                                                   >> 529 
                                                   >> 530   /* Initialize the scheduler */
                                                   >> 531   sos_sched_subsystem_setup();
                                                   >> 532 
                                                   >> 533   /* Declare the IDLE thread */
                                                   >> 534   SOS_ASSERT_FATAL(sos_create_kernel_thread("idle", idle_thread, NULL,
                                                   >> 535                                             SOS_SCHED_PRIO_TS_LOWEST) != NULL);
                                                   >> 536 
                                                   >> 537   /* Prepare the stats subsystem */
                                                   >> 538   sos_load_subsystem_setup();
                                                   >> 539 
                                                   >> 540   /* Declare a thread that prints some stats */
                                                   >> 541   SOS_ASSERT_FATAL(sos_create_kernel_thread("stat_thread", stat_thread,
                                                   >> 542                                             NULL,
                                                   >> 543                                             SOS_SCHED_PRIO_TS_LOWEST) != NULL);
1141                                                  544 
1142                                                  545 
1143   /*                                             546   /*
1144    * Create an un-resolved page fault, which  !! 547    * Initialise user address space management subsystem
1145    * handler print the backtrace.             << 
1146    */                                            548    */
1147   test_backtrace(6, 0xdeadbeef, bootstrap_sta !! 549   sos_umem_vmm_subsystem_setup();
                                                   >> 550   sos_dev_zero_subsystem_setup();
                                                   >> 551   sos_dev_mem_chardev_setup();
1148                                                  552 
1149   /*                                             553   /*
1150    * System should be halted BEFORE here !    !! 554    * Initialize process stuff
1151    */                                            555    */
                                                   >> 556   sos_process_subsystem_setup();
1152                                                  557 
1153                                                  558 
1154   /* An operatig system never ends */         !! 559   /* Enabling the HW interrupts here, this will make the timer HW
1155   for (;;)                                    !! 560      interrupt call the scheduler */
1156     {                                         !! 561   asm volatile ("sti\n");
1157       /* Remove this instruction if you get a << 
1158          exception (old 80386 CPU) */         << 
1159       asm("hlt\n");                           << 
1160                                                  562 
1161       continue;                               !! 563 
1162     }                                         !! 564   SOS_ASSERT_FATAL(SOS_OK == sos_fs_virtfs_subsystem_setup());
                                                   >> 565   SOS_ASSERT_FATAL(SOS_OK == sos_fs_subsystem_setup(NULL,
                                                   >> 566                                                     "virtfs",
                                                   >> 567                                                     NULL,
                                                   >> 568                                                     & rootfs));
                                                   >> 569 
                                                   >> 570 
                                                   >> 571   tty_subsystem_setup();
                                                   >> 572   sos_ttyS0_subsystem_setup();
                                                   >> 573   sos_console_subsystem_setup();
                                                   >> 574 
                                                   >> 575 
                                                   >> 576 
                                                   >> 577 
                                                   >> 578   /* Start the 'init' process, which in turns launches the other
                                                   >> 579      programs */
                                                   >> 580   start_init(rootfs);
                                                   >> 581   /*
                                                   >> 582    * We can safely exit from this function now, for there is already
                                                   >> 583    * an idle Kernel thread ready to make the CPU busy working...
                                                   >> 584    *
                                                   >> 585    * However, we must EXPLICITELY call sos_thread_exit() because a
                                                   >> 586    * simple "return" will return nowhere ! Actually this first thread
                                                   >> 587    * was initialized by the Grub bootstrap stage, at a time when the
                                                   >> 588    * word "thread" did not exist. This means that the stack was not
                                                   >> 589    * setup in order for a return here to call sos_thread_exit()
                                                   >> 590    * automagically. Hence we must call it manually. This is the ONLY
                                                   >> 591    * kernel thread where we must do this manually.
                                                   >> 592    */
                                                   >> 593   sos_bochs_printf("Bye from primary thread !\n");
                                                   >> 594   sos_thread_exit();
                                                   >> 595   SOS_FATAL_ERROR("No trespassing !");
1163 }                                                596 }
                                                      

source navigation ] diff markup ] identifier search ] general search ]