Diff markup
001 001
002 002
003 003
004 004
005 005
006 006
007 007
008 008
009 009
010 010
011 011
012 012
013 013
014 014
015 015
016 016
017 017
018 018
019 019
020 020
021 #include <bootstrap/multiboot.h> 021 #include <bootstrap/multiboot.h>
022 #include <hwcore/idt.h> 022 #include <hwcore/idt.h>
023 #include <hwcore/gdt.h> 023 #include <hwcore/gdt.h>
024 #include <hwcore/irq.h> 024 #include <hwcore/irq.h>
025 #include <hwcore/exception.h> 025 #include <hwcore/exception.h>
026 #include <hwcore/i8254.h> 026 #include <hwcore/i8254.h>
027 #include <sos/list.h> 027 #include <sos/list.h>
028 #include <sos/physmem.h> 028 #include <sos/physmem.h>
029 #include <hwcore/paging.h> 029 #include <hwcore/paging.h>
030 #include <sos/kmem_vmm.h> 030 #include <sos/kmem_vmm.h>
031 #include <sos/kmalloc.h> 031 #include <sos/kmalloc.h>
032 #include <sos/klibc.h> 032 #include <sos/klibc.h>
033 #include <sos/assert.h> 033 #include <sos/assert.h>
034 #include <drivers/x86_videomem.h> 034 #include <drivers/x86_videomem.h>
035 #include <drivers/bochs.h> 035 #include <drivers/bochs.h>
036 036
037 037
038 038
039 039
040 void display_bits(unsigned char row, unsigned !! 040 static void display_bits(unsigned char row, unsigned char col,
041 unsigned char attribute, !! 041 unsigned char attribute,
042 sos_ui32_t integer) !! 042 sos_ui32_t integer)
043 { 043 {
044 int i; 044 int i;
045 045
046 for (i = 31 ; i >= 0 ; i--) 046 for (i = 31 ; i >= 0 ; i--)
047 { 047 {
048 048
049 int bit_i = (integer & (1 << i)); 049 int bit_i = (integer & (1 << i));
050 050
051 unsigned char ascii_code = bit_i?219:177 051 unsigned char ascii_code = bit_i?219:177;
052 sos_x86_videomem_putchar(row, col++, 052 sos_x86_videomem_putchar(row, col++,
053 attribute, 053 attribute,
054 ascii_code); 054 ascii_code);
055 } 055 }
056 } 056 }
057 057
058 058
059 059
060 static void clk_it(int intid, !! 060 static void clk_it(int intid)
061 const struct sos_cpu_kstate <<
062 { 061 {
063 static sos_ui32_t clock_count = 0; 062 static sos_ui32_t clock_count = 0;
064 063
065 display_bits(0, 48, 064 display_bits(0, 48,
066 SOS_X86_VIDEO_FG_LTGREEN | SOS_ 065 SOS_X86_VIDEO_FG_LTGREEN | SOS_X86_VIDEO_BG_BLUE,
067 clock_count); 066 clock_count);
068 clock_count++; 067 clock_count++;
069 } <<
070 <<
071 <<
072 <<
073 <<
074 <<
075 <<
076 <<
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 068
090 <<
091 <<
092 <<
093 <<
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 <<
100 <<
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 <<
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 } 069 }
132 !! 070 struct digit
133 <<
134 <<
135 static void pgflt_ex(int intid, const struct s <<
136 { 071 {
137 static sos_ui32_t demand_paging_count = 0; !! 072 struct digit *prev, *next;
138 sos_vaddr_t faulting_vaddr = sos_cpu_kstate_ !! 073 char value;
139 sos_paddr_t ppage_paddr; !! 074 };
140 <<
141 <<
142 if (! sos_kmem_vmm_is_valid_vaddr(faulting_v <<
143 { <<
144 <<
145 <<
146 dump_backtrace(ctxt, <<
147 bootstrap_stack_bottom, <<
148 bootstrap_stack_size, <<
149 TRUE, TRUE); <<
150 sos_display_fatal_error("Unresolved page <<
151 (unsigned)faulti <<
152 (unsigned)sos_cp <<
153 SOS_ASSERT_FATAL(! "Got page fault (note <<
154 } <<
155 <<
156 <<
157 <<
158 <<
159 <<
160 <<
161 <<
162 demand_paging_count ++; <<
163 display_bits(0, 0, <<
164 SOS_X86_VIDEO_FG_LTRED | SOS_X8 <<
165 demand_paging_count); <<
166 <<
167 <<
168 ppage_paddr = sos_physmem_ref_physpage_new(F <<
169 if (! ppage_paddr) <<
170 SOS_ASSERT_FATAL(! "TODO: implement swap. <<
171 SOS_ASSERT_FATAL(SOS_OK == sos_paging_map(pp <<
172 SO <<
173 FA <<
174 SO <<
175 | <<
176 | <<
177 sos_physmem_unref_physpage(ppage_paddr); <<
178 <<
179 <<
180 } <<
181 <<
182 075
>> 076
>> 077
>> 078
>> 079 typedef struct digit * big_number_t;
183 080
184 <<
185 <<
186 <<
187 <<
188 <<
189 <<
190 <<
191 <<
192 struct sos_cpu_kstate *ctxt_hello1; <<
193 struct sos_cpu_kstate *ctxt_hello2; <<
194 struct sos_cpu_kstate *ctxt_main; <<
195 sos_vaddr_t hello1_stack, hello2_stack; <<
196 081
197 static void reclaim_stack(sos_vaddr_t stack_va !! 082
>> 083 void bn_push_lsd(big_number_t * bn, char value)
198 { 084 {
199 sos_kfree(stack_vaddr); !! 085 struct digit *d;
>> 086 d = (struct digit*) sos_kmalloc(sizeof(struct digit), 0);
>> 087 SOS_ASSERT_FATAL(d != NULL);
>> 088 d->value = value;
>> 089 list_add_tail(*bn, d);
200 } 090 }
201 091
202 092
203 static void exit_hello12(sos_vaddr_t stack_vad !! 093
>> 094 void bn_push_msd(big_number_t * bn, char value)
204 { 095 {
205 sos_cpu_kstate_exit_to(ctxt_main, !! 096 struct digit *d;
206 (sos_cpu_kstate_funct !! 097 d = (struct digit*) sos_kmalloc(sizeof(struct digit), 0);
207 stack_vaddr); !! 098 SOS_ASSERT_FATAL(d != NULL);
>> 099 d->value = value;
>> 100 list_add_head(*bn, d);
208 } 101 }
209 102
210 103
211 static void hello1 (char *str) !! 104
>> 105 big_number_t bn_new(unsigned long int i)
212 { 106 {
213 for ( ; *str != '\n' ; str++) !! 107 big_number_t retval;
>> 108
>> 109 list_init(retval);
>> 110 do
214 { 111 {
215 sos_bochs_printf("hello1: %c\n", *str); !! 112 bn_push_msd(&retval, i%10);
216 sos_cpu_kstate_switch(& ctxt_hello1, ctx !! 113 i /= 10;
217 } 114 }
>> 115 while (i != 0);
218 116
219 !! 117 return retval;
220 <<
221 <<
222 <<
223 <<
224 } 118 }
225 119
226 120
227 static void hello2 (char *str) !! 121
>> 122 big_number_t bn_copy(const big_number_t bn)
228 { 123 {
229 for ( ; *str != '\n' ; str++) !! 124 big_number_t retval;
>> 125 int nb_elts;
>> 126 struct digit *d;
>> 127
>> 128 list_init(retval);
>> 129 list_foreach(bn, d, nb_elts)
230 { 130 {
231 sos_bochs_printf("hello2: %c\n", *str); !! 131 bn_push_lsd(&retval, d->value);
232 sos_cpu_kstate_switch(& ctxt_hello2, ctx <<
233 } 132 }
234 133
235 !! 134 return retval;
236 <<
237 <<
238 <<
239 <<
240 } <<
241 <<
242 <<
243 void print_hello_world () <<
244 { <<
245 #define DEMO_STACK_SIZE 1024 <<
246 <<
247 hello1_stack = sos_kmalloc(DEMO_STACK_SIZE, <<
248 hello2_stack = sos_kmalloc(DEMO_STACK_SIZE, <<
249 <<
250 <<
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 <<
265 sos_bochs_printf("Printing Hello World\\n... <<
266 sos_cpu_kstate_switch(& ctxt_main, ctxt_hell <<
267 <<
268 <<
269 sos_bochs_printf("Back in main !\n"); <<
270 } 135 }
271 136
272 137
273 !! 138
274 !! 139 void bn_del(big_number_t * bn)
275 <<
276 <<
277 <<
278 static void test_demand_paging(int nb_alloc_vp <<
279 { 140 {
280 int i; !! 141 struct digit *d;
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 <<
289 base_vaddr = sos_kmem_vmm_alloc(nb_alloc_vpa <<
290 142
291 SOS_ASSERT_FATAL(base_vaddr != (sos_vaddr_t) !! 143 list_collapse(*bn, d)
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 <<
298 <<
299 for (i = 0 ; (i < nb_alloc_ppages) && (i < n <<
300 { 144 {
301 !! 145 sos_kfree((sos_vaddr_t)d);
302 sos_ui32_t *value, j; <<
303 sos_vaddr_t vaddr = base_vaddr; <<
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 <<
313 value = (sos_ui32_t*)vaddr; <<
314 *value = i; <<
315 <<
316 <<
317 sos_x86_videomem_printf(13, 0, <<
318 SOS_X86_VIDEO_BG <<
319 "Value read at a <<
320 vaddr, (unsigned <<
321 } 146 }
322 <<
323 SOS_ASSERT_FATAL(SOS_OK == sos_kmem_vmm_free <<
324 <<
325 sos_x86_videomem_printf(14, 0, <<
326 SOS_X86_VIDEO_BG_BLU <<
327 "Done (area un-alloc <<
328 } 147 }
329 148
330 149
331 !! 150
332 !! 151 void bn_shift(big_number_t *bn, int shift)
333 <<
334 <<
335 <<
336 <<
337 static void test_backtrace(int i, int magic, s <<
338 sos_size_t stack_s <<
339 { 152 {
340 if (i <= 0) !! 153 for ( ; shift > 0 ; shift --)
341 { 154 {
342 !! 155 bn_push_lsd(bn, 0);
343 <<
344 *((char*)0x42) = 12; <<
345 <<
346 <<
347 <<
348 } 156 }
349 else <<
350 test_backtrace(i-1, magic, stack_bottom, s <<
351 } 157 }
352 158
353 159
354 !! 160
355 !! 161 void bn_print_bochs(const big_number_t bn)
356 <<
357 <<
358 <<
359 <<
360 <<
361 <<
362 <<
363 <<
364 <<
365 <<
366 <<
367 <<
368 <<
369 <<
370 <<
371 <<
372 <<
373 <<
374 <<
375 <<
376 <<
377 <<
378 <<
379 <<
380 <<
381 <<
382 <<
383 <<
384 <<
385 <<
386 <<
387 <<
388 <<
389 <<
390 <<
391 <<
392 <<
393 <<
394 <<
395 <<
396 <<
397 <<
398 <<
399 <<
400 <<
401 <<
402 <<
403 <<
404 <<
405 <<
406 <<
407 <<
408 <<
409 <<
410 <<
411 <<
412 <<
413 <<
414 <<
415 <<
416 <<
417 <<
418 <<
419 <<
420 <<
421 <<
422 <<
423 <<
424 <<
425 <<
426 <<
427 <<
428 <<
429 <<
430 <<
431 <<
432 <<
433 <<
434 <<
435 static char stack_reader[1024]; <<
436 static char stack_lexer[1024]; <<
437 static char deep_stack[65536]; <<
438 <<
439 <<
440 static struct sos_cpu_kstate *st_reader, *st_l <<
441 *st_eval, *st_free, *st_main; <<
442 <<
443 <<
444 <<
445 <<
446 <<
447 <<
448 static void reclaim(int unused) <<
449 { 162 {
450 } !! 163 int nb_elts;
451 static void func_exit(sos_ui32_t unused) !! 164 const struct digit *d;
452 { <<
453 sos_cpu_kstate_exit_to(st_main, (sos_cpu_kst <<
454 } <<
455 <<
456 165
457 !! 166 if (list_is_empty(bn))
458 !! 167 sos_bochs_printf("0");
459 !! 168 else
460 !! 169 list_foreach(bn, d, nb_elts)
461 !! 170 sos_bochs_printf("%d", d->value);
462 static char data_reader_to_lexer; <<
463 <<
464 static void func_reader(const char *str) <<
465 { <<
466 for ( ; str && (*str != '\0') ; str++) <<
467 { <<
468 data_reader_to_lexer = *str; <<
469 sos_cpu_kstate_switch(& st_reader, st_le <<
470 } <<
471 <<
472 data_reader_to_lexer = '\0'; <<
473 sos_cpu_kstate_switch(& st_reader, st_lexer) <<
474 } 171 }
475 172
476 !! 173
477 !! 174 void bn_print_console(unsigned char row, unsigned char col,
478 !! 175 unsigned char attribute,
479 !! 176 const big_number_t bn,
480 !! 177 int nb_decimals)
481 <<
482 <<
483 #define STR_VAR_MAXLEN 16 <<
484 static struct lex_elem <<
485 { 178 {
486 enum { LEX_IS_NUMBER, LEX_IS_OPER, LEX_IS_VA !! 179 if (list_is_empty(bn))
487 LEX_IS_OPENPAR, LEX_IS_CLOSEPAR, LEX_ !! 180 sos_x86_videomem_printf(row, col, attribute, "0");
488 union { !! 181 else
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 { 182 {
505 !! 183 int nb_elts;
506 sos_cpu_kstate_switch(& st_lexer, st_rea !! 184 const struct digit *d;
507 c = data_reader_to_lexer; !! 185 unsigned char x = col;
508 <<
509 <<
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 <<
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 <<
531 <<
532 if ( (got_what_before != GOT_SPACE) <<
533 sos_cpu_kstate_switch(& st_lexer, <<
534 186
535 data_lexer_to_parser.number = 0; !! 187 list_foreach(bn, d, nb_elts)
536 } <<
537 <<
538 <<
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 { 188 {
552 char to_cat[] = { c, '\0' }; !! 189 if (nb_elts == 0)
553 data_lexer_to_parser.type = LEX_IS_V !! 190 {
554 strzcat(data_lexer_to_parser.var, to !! 191 sos_x86_videomem_printf(row, x, attribute, "%d.", d->value);
>> 192 x += 2;
>> 193 }
>> 194 else if (nb_elts < nb_decimals)
>> 195 {
>> 196 sos_x86_videomem_printf(row, x, attribute, "%d", d->value);
>> 197 x ++;
>> 198 }
555 } 199 }
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 200
561 got_what_before = got_what; !! 201 sos_x86_videomem_printf(row, x, attribute, " . 10^{%d} ", nb_elts-1);
562 } 202 }
563 while (c != '\0'); <<
564 <<
565 <<
566 if ( (got_what_before != GOT_SPACE) ) <<
567 sos_cpu_kstate_switch(& st_lexer, st_parse <<
568 <<
569 <<
570 data_lexer_to_parser.type = LEX_END; <<
571 sos_cpu_kstate_switch(& st_lexer, st_parser) <<
572 <<
573 <<
574 <<
575 sos_bochs_printf("Error: end of string alrea <<
576 sos_cpu_kstate_switch(& st_lexer, st_main); <<
577 } 203 }
578 204
579 205
580 !! 206
581 !! 207 big_number_t bn_add (const big_number_t bn1, const big_number_t bn2)
582 <<
583 struct syntax_node <<
584 { 208 {
585 enum { YY_IS_BINOP, YY_IS_UNAROP, YY_IS_NUM, !! 209 big_number_t retval;
586 union !! 210 const struct digit *d1, *d2;
587 { !! 211 sos_bool_t bn1_end = FALSE, bn2_end = FALSE;
588 int number; !! 212 char carry = 0;
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 213
603 static void func_parser(struct syntax_node ** !! 214 list_init(retval);
604 { !! 215 d1 = list_get_tail(bn1);
605 static struct syntax_node *alloc_node_num(in !! 216 bn1_end = list_is_empty(bn1);
606 static struct syntax_node *alloc_node_var(co !! 217 d2 = list_get_tail(bn2);
607 static struct syntax_node *alloc_node_binop( !! 218 bn2_end = list_is_empty(bn2);
608 !! 219 do
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 <<
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 <<
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 <<
638 static struct syntax_node *alloc_node_binop( <<
639 <<
640 <<
641 { <<
642 struct syntax_node *n <<
643 = (struct syntax_node*) sos_kmalloc(si <<
644 n->type = YY_IS_BINOP; <<
645 n->binop.op = op; <<
646 n->binop.parm_left = parm_left; <<
647 n->binop.parm_right = parm_right; <<
648 return n; <<
649 } <<
650 <<
651 static struct syntax_node *alloc_node_unarop <<
652 <<
653 { 220 {
654 struct syntax_node *n !! 221 if (! bn1_end)
655 = (struct syntax_node*) sos_kmalloc(si !! 222 carry += d1->value;
656 n->type = YY_IS_UNAROP; !! 223 if (! bn2_end)
657 n->unarop.op = op; !! 224 carry += d2->value;
658 n->unarop.parm = parm; !! 225
659 return n; !! 226 bn_push_msd(&retval, carry % 10);
>> 227 carry /= 10;
>> 228
>> 229 if (! bn1_end)
>> 230 d1 = d1->prev;
>> 231 if (! bn2_end)
>> 232 d2 = d2->prev;
>> 233 if (d1 == list_get_tail(bn1))
>> 234 bn1_end = TRUE;
>> 235 if (d2 == list_get_tail(bn2))
>> 236 bn2_end = TRUE;
660 } 237 }
>> 238 while (!bn1_end || !bn2_end);
661 239
662 !! 240 if (carry > 0)
663 <<
664 static void parser_exception(const char *str <<
665 { 241 {
666 sos_bochs_printf("Parser exception: %s\n !! 242 bn_push_msd(&retval, carry);
667 sos_cpu_kstate_switch(& st_parser, st_ma <<
668 } 243 }
669 244
670 !! 245 return retval;
671 !! 246 }
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 <<
682 <<
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 <<
691 <<
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 <<
702 <<
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 247
711 !! 248
712 static struct syntax_node * get_expr() !! 249
713 { !! 250 big_number_t bn_muli (const big_number_t bn, char digit)
714 struct syntax_node *t = get_term(); !! 251 {
715 return get_expr_lr(t); !! 252 big_number_t retval;
716 } !! 253 int nb_elts;
717 !! 254 char carry = 0;
718 static struct syntax_node * get_expr_lr(stru !! 255 const struct digit *d;
719 { !! 256
720 if ( (data_lexer_to_parser.type == LEX_I !! 257 list_init(retval);
721 && ( (data_lexer_to_parser.operator !! 258 list_foreach_backward(bn, d, nb_elts)
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 <<
732 static struct syntax_node * get_term() <<
733 { <<
734 struct syntax_node *f1 = get_factor(); <<
735 return get_term_lr(f1); <<
736 } <<
737 <<
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 <<
752 static struct syntax_node * get_factor() <<
753 { 259 {
754 if ( (data_lexer_to_parser.type == LEX_I !! 260 carry += d->value * digit;
755 && ( (data_lexer_to_parser.operator !! 261 bn_push_msd(&retval, carry % 10);
756 || (data_lexer_to_parser.opera !! 262 carry /= 10;
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 } 263 }
772 !! 264
773 static struct syntax_node * get_scalar() !! 265 if (carry > 0)
774 { 266 {
775 if (data_lexer_to_parser.type != LEX_IS_ !! 267 bn_push_msd(&retval, carry);
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 } 268 }
783 269
784 !! 270 return retval;
785 <<
786 <<
787 <<
788 <<
789 <<
790 sos_cpu_kstate_switch(& st_parser, st_lexer) <<
791 <<
792 <<
793 *syntax_tree = get_expr(); <<
794 <<
795 } 271 }
796 272
797 273
798 !! 274
799 !! 275 big_number_t bn_mult(const big_number_t bn1, const big_number_t bn2)
800 <<
801 static struct syntax_node * parse_expression(c <<
802 { 276 {
803 struct syntax_node *retval = NULL; !! 277 int shift = 0;
804 !! 278 big_number_t retval;
805 !! 279 int nb_elts;
806 sos_cpu_kstate_init(& st_reader, !! 280 struct digit *d;
807 (sos_cpu_kstate_function !! 281
808 (sos_ui32_t)expr, !! 282 list_init(retval);
809 (sos_vaddr_t)stack_reade !! 283 list_foreach_backward(bn2, d, nb_elts)
810 (sos_cpu_kstate_function !! 284 {
811 sos_cpu_kstate_init(& st_lexer, !! 285 big_number_t retmult = bn_muli(bn1, d->value);
812 (sos_cpu_kstate_function !! 286 big_number_t old_retval = retval;
813 0, !! 287 bn_shift(& retmult, shift);
814 (sos_vaddr_t)stack_lexer !! 288 retval = bn_add(old_retval, retmult);
815 (sos_cpu_kstate_function !! 289 bn_del(& retmult);
816 sos_cpu_kstate_init(& st_parser, !! 290 bn_del(& old_retval);
817 (sos_cpu_kstate_function !! 291 shift ++;
818 (sos_ui32_t) !! 292 }
819 (sos_vaddr_t)deep_stack, <<
820 (sos_cpu_kstate_function <<
821 293
822 <<
823 sos_cpu_kstate_switch(& st_main, st_parser); <<
824 return retval; 294 return retval;
825 } 295 }
826 296
827 297
828 !! 298
829 !! 299 big_number_t bn_fact(unsigned long int v)
830 <<
831 struct func_eval_params <<
832 { 300 {
833 const struct syntax_node *e; !! 301 unsigned long int i;
834 const char **var_name; !! 302 big_number_t retval = bn_new(1);
835 int *var_val; !! 303 for (i = 1 ; i <= v ; i++)
836 int nb_vars; !! 304 {
837 !! 305 big_number_t I = bn_new(i);
838 int result; !! 306 big_number_t tmp = bn_mult(retval, I);
839 }; !! 307 sos_x86_videomem_printf(4, 0,
840 !! 308 SOS_X86_VIDEO_BG_BLUE | SOS_X86_VIDEO_FG_LTGREEN,
841 static void func_eval(struct func_eval_params !! 309 "%d! = ", (int)i);
842 { !! 310 bn_print_console(4, 8, SOS_X86_VIDEO_BG_BLUE | SOS_X86_VIDEO_FG_WHITE,
843 !! 311 tmp, 55);
844 !! 312 bn_del(& I);
845 static int rec_eval(const struct syntax_node !! 313 bn_del(& retval);
846 const char* var_name[], !! 314 retval = tmp;
847 { <<
848 switch (n->type) <<
849 { <<
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 <<
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 <<
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 <<
893 sos_bochs_printf("ERROR: unkno <<
894 sos_cpu_kstate_switch(& st_eva <<
895 } <<
896 } <<
897 } <<
898 <<
899 <<
900 sos_bochs_printf("ERROR: invalid node ty <<
901 sos_cpu_kstate_switch(& st_eval, st_main <<
902 return -1; <<
903 } 315 }
904 316
905 !! 317 return retval;
906 <<
907 <<
908 <<
909 <<
910 parms->result <<
911 = rec_eval(parms->e, parms->var_name, parm <<
912 } <<
913 <<
914 <<
915 <<
916 <<
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) <<
931 (sos_vaddr_t)deep_stack, <<
932 (sos_cpu_kstate_function <<
933 <<
934 <<
935 sos_cpu_kstate_switch(& st_main, st_eval); <<
936 return p.result; <<
937 } 318 }
938 319
939 320
940 !! 321 void bn_test()
941 <<
942 <<
943 static void func_free(struct syntax_node *n) <<
944 { 322 {
945 switch (n->type) !! 323 big_number_t bn = bn_fact(1000);
946 { !! 324 sos_bochs_printf("1000! = ");
947 case YY_IS_NUM: !! 325 bn_print_bochs(bn);
948 case YY_IS_VAR: !! 326 sos_bochs_printf("\n");
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 } <<
960 327
961 sos_kfree((sos_vaddr_t)n); <<
962 } 328 }
963 329
964 <<
965 <<
966 <<
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 330
976 !! 331
977 sos_cpu_kstate_switch(& st_main, st_free); <<
978 } <<
979 <<
980 <<
981 <<
982 <<
983 <<
984 void sos_main(unsigned long magic, unsigned lo 332 void sos_main(unsigned long magic, unsigned long addr)
985 { 333 {
986 unsigned i; 334 unsigned i;
987 sos_paddr_t sos_kernel_core_base_paddr, sos_ 335 sos_paddr_t sos_kernel_core_base_paddr, sos_kernel_core_top_paddr;
988 struct syntax_node *syntax_tree; <<
989 336
990 337
991 338
992 339
993 multiboot_info_t *mbi; 340 multiboot_info_t *mbi;
994 mbi = (multiboot_info_t *) addr; 341 mbi = (multiboot_info_t *) addr;
995 342
996 343
997 sos_bochs_setup(); 344 sos_bochs_setup();
998 345
999 sos_x86_videomem_setup(); 346 sos_x86_videomem_setup();
1000 sos_x86_videomem_cls(SOS_X86_VIDEO_BG_BLUE) 347 sos_x86_videomem_cls(SOS_X86_VIDEO_BG_BLUE);
1001 348
1002 349
1003 if (magic == MULTIBOOT_BOOTLOADER_MAGIC) 350 if (magic == MULTIBOOT_BOOTLOADER_MAGIC)
1004 351
1005 sos_x86_videomem_printf(1, 0, 352 sos_x86_videomem_printf(1, 0,
1006 SOS_X86_VIDEO_FG_ 353 SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
1007 "Welcome From GRU 354 "Welcome From GRUB to %s%c RAM is %dMB (upper mem = 0x%x kB)",
1008 "SOS", ',', 355 "SOS", ',',
1009 (unsigned)(mbi->m 356 (unsigned)(mbi->mem_upper >> 10) + 1,
1010 (unsigned)mbi->me 357 (unsigned)mbi->mem_upper);
1011 else 358 else
1012 359
1013 sos_x86_videomem_printf(1, 0, 360 sos_x86_videomem_printf(1, 0,
1014 SOS_X86_VIDEO_FG_ 361 SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
1015 "Welcome to SOS") 362 "Welcome to SOS");
1016 363
1017 sos_bochs_putstring("Message in a bochs\n") 364 sos_bochs_putstring("Message in a bochs\n");
1018 365
1019 366
1020 sos_gdt_subsystem_setup(); !! 367 sos_gdt_setup();
1021 sos_idt_subsystem_setup(); !! 368 sos_idt_setup();
1022 369
1023 370
1024 sos_exception_subsystem_setup(); !! 371 sos_exceptions_setup();
1025 sos_irq_subsystem_setup(); !! 372 sos_irq_setup();
1026 373
1027 374
1028 sos_i8254_set_frequency(100); 375 sos_i8254_set_frequency(100);
1029 376
>> 377
1030 378
1031 if (magic != MULTIBOOT_BOOTLOADER_MAGIC) 379 if (magic != MULTIBOOT_BOOTLOADER_MAGIC)
1032 { 380 {
1033 sos_x86_videomem_putstring(20, 0, 381 sos_x86_videomem_putstring(20, 0,
1034 SOS_X86_VIDE 382 SOS_X86_VIDEO_FG_LTRED
1035 | SOS_X86_ 383 | SOS_X86_VIDEO_BG_BLUE
1036 | SOS_X86_ 384 | SOS_X86_VIDEO_FG_BLINKING,
1037 "I'm not loa 385 "I'm not loaded with Grub !");
1038 386
1039 for (;;) 387 for (;;)
1040 continue; 388 continue;
1041 } 389 }
1042 390
1043 <<
1044 <<
1045 <<
1046 <<
1047 391
1048 sos_irq_set_routine(SOS_IRQ_TIMER, 392 sos_irq_set_routine(SOS_IRQ_TIMER,
1049 clk_it); !! 393 clk_it);
1050 !! 394
1051 !! 395
1052 !! 396 asm volatile ("sti\n");
1053 <<
1054 <<
1055 397
1056 398
1057 399
1058 sos_physmem_subsystem_setup((mbi->mem_upper !! 400 sos_physmem_setup((mbi->mem_upper<<10) + (1<<20),
1059 & sos_kernel_co !! 401 & sos_kernel_core_base_paddr,
1060 & sos_kernel_co !! 402 & sos_kernel_core_top_paddr);
1061 403
1062 404
1063 405
1064 406
1065 407
1066 408
1067 409
1068 SOS_ASSERT_FATAL(SOS_OK == !! 410 if (sos_paging_setup(sos_kernel_core_base_paddr,
1069 sos_paging_subsystem_setup !! 411 sos_kernel_core_top_paddr))
1070 !! 412 sos_bochs_printf("Could not setup paged memory mode\n");
1071 !! 413 sos_x86_videomem_printf(2, 0,
1072 !! 414 SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
1073 sos_exception_set_routine(SOS_EXCEPT_PAGE_F !! 415 "Paged-memory mode is activated");
1074 pgflt_ex); <<
1075 416
1076 <<
1077 <<
1078 <<
1079 417
1080 if (sos_kmem_vmm_subsystem_setup(sos_kernel !! 418 if (sos_kmem_vmm_setup(sos_kernel_core_base_paddr,
1081 sos_kernel !! 419 sos_kernel_core_top_paddr))
1082 bootstrap_ <<
1083 bootstrap_ <<
1084 + bootstra <<
1085 sos_bochs_printf("Could not setup the Ker 420 sos_bochs_printf("Could not setup the Kernel virtual space allocator\n");
1086 421
1087 if (sos_kmalloc_subsystem_setup()) !! 422 if (sos_kmalloc_setup())
1088 sos_bochs_printf("Could not setup the Kma 423 sos_bochs_printf("Could not setup the Kmalloc subsystem\n");
1089 424
1090 !! 425
1091 !! 426 bn_test();
1092 <<
1093 <<
1094 asm volatile ("sti\n"); <<
1095 <<
1096 <<
1097 <<
1098 <<
1099 print_hello_world(); <<
1100 <<
1101 <<
1102 <<
1103 <<
1104 <<
1105 sos_x86_videomem_printf(4, 0, <<
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 <<
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 <<
1137 <<
1138 <<
1139 <<
1140 test_demand_paging(234567, 500); <<
1141 <<
1142 <<
1143 <<
1144 <<
1145 <<
1146 <<
1147 test_backtrace(6, 0xdeadbeef, bootstrap_sta <<
1148 <<
1149 <<
1150 <<
1151 <<
1152 <<
1153 427
1154 428
1155 for (;;) 429 for (;;)
1156 { !! 430 continue;
1157 <<
1158 <<
1159 asm("hlt\n"); <<
1160 431
1161 continue; !! 432 return;
1162 } <<
1163 } 433 }