From fa0a8f6b4b619549caa8690eec817d711e71ad89 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 17 Jan 2019 14:09:08 +0100 Subject: [PATCH 1/5] Improvements with nasm preproc --- boot/loader/io/ata.asm | 11 +++++++++-- boot/loader/io/lmterm.asm | 7 +++---- boot/loader/loader.asm | 38 ++++++++++++++++++++++---------------- build/bin/disk.img | Bin 18944000 -> 18944000 bytes build/bin/loader.bin | Bin 1992 -> 2043 bytes build/obj/boot/loader.bin | Bin 1992 -> 2043 bytes 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/boot/loader/io/ata.asm b/boot/loader/io/ata.asm index 9f6aaed..f075b7d 100644 --- a/boot/loader/io/ata.asm +++ b/boot/loader/io/ata.asm @@ -34,7 +34,7 @@ ;; GLOBAL DATA Bootdrv db 0 -end db "[End of Sector]", 0x0A, 0x0D, 0x0 +ended db "[End of Sector]", 0x0A, 0x0A, 0x0D, 0x0 buffer: times 513 db "_" ;; TEXT @@ -124,12 +124,19 @@ still_going: mov dx, 0x1f0 ; Data port - data comes in and out of here. rep insw pop rdi +%ifdef DEBUG mov bl, 0x0F mov esi, buffer call dump mov bl, 0x0A - mov esi, end + mov esi, ended call write + add qword [NextTRAM], 120 ; Cursor moving : 1120 = 80 * 2 * 7 lignes +%else + mov bl, 0x0A + mov esi, Pass + call write +%endif pop rdx pop rcx pop rbx diff --git a/boot/loader/io/lmterm.asm b/boot/loader/io/lmterm.asm index 3fa0ccb..427ae89 100644 --- a/boot/loader/io/lmterm.asm +++ b/boot/loader/io/lmterm.asm @@ -93,9 +93,9 @@ write: dump: ;-----------------------------------------------------------------------; -; x64/LM Dump Printing Functions ; +; x64/LM Dump 512 bytes of a buffer ; ; bl : color code ; -; esi : string address ; +; esi : buffer address ; ;-----------------------------------------------------------------------; mov edi, [NextTRAM] ; TRAM ADDRESS push rsi @@ -107,10 +107,9 @@ dump: stosb ; text subpixel mov al, bl stosb ; color subpixel - add qword [NextTRAM], 0x2 ; Cursor moving - add qword [VGA_X], 0x2 ; coord + 2 because 2 subpixels loop .pLoop pop rcx pop rdi pop rsi + add qword [NextTRAM], 1000 ; Cursor moving : 1120 = 80 * 2 * 7 lignes ret diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index a2bb849..2cfc899 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -8,6 +8,8 @@ ; (x86_64 architecture only) ; ;=----------------------------------------------------------------------------=; +%define DEBUG + [BITS 16] [ORG 0x1000] @@ -161,18 +163,23 @@ main32: [BITS 64] ;; DATA -Init db "Booting OS/K !", 0x0D, 0x0A, 0x0D, 0x0A, 0x09, " Checking CPUID...",0 -CPUIDD db 0x09, " Checking CPUID...", 0 -EnA20 db 0x09, " Enabling A20 line...", 0 -ReadAttempt db 0x09, " Attempt to read a sector with ATA commands...", 0x0A, 0x0D, 0x0A, 0x0D,0 -txt db 0x09, " Switching to Long Mode... ", 0 -Reinit db "Booting OS/K !", 0x0D, 0x0A, 0x0D, 0x0A, 0 -Pass db " OK", 0x0A, 0x0D, 0 -Fail db " FAIL!", 0x0A, 0x0D, 0 -msg db "The system is now in x64 mode. Is this not beautiful ?", 0x0A, 0x0D, 0 -FileNotFound db "Second Stage Error : The Kernel was not found.", 0x0A, 0x0D, 0 -DiskError db "Second Stage Error : The Disk has crashed.", 0x0A, 0x0D, 0 -filename db "KERNEL BIN" + Init db "Booting OS/K !", 0x0D, 0x0A, 0x0D, 0x0A, 0x09, " Checking CPUID...",0 + CPUIDD db 0x09, " Checking CPUID...", 0 + EnA20 db 0x09, " Enabling A20 line...", 0 +%ifdef DEBUG + ReadAttempt db 0x09, " Attempt to read a sector with ATA commands...", 0x0A, 0x0D, 0x0A, 0x0D, 0 +%else + ReadAttempt db 0x09, " Attempt to read a sector with ATA commands...", 0 +%endif + txt db 0x09, " Switching to Long Mode... ", 0 + EndOfLoader db "End of loader.bin. System will halt !", 0x0A, 0x0D, 0 + Reinit db "Booting OS/K !", 0x0D, 0x0A, 0x0D, 0x0A, 0 + Pass db " OK", 0x0A, 0x0D, 0 + Fail db " FAIL!", 0x0A, 0x0D, 0 + msg db "The system is now in x64 mode. Is this not beautiful ?", 0x0A, 0x0D, 0 + FileNotFound db "Second Stage Error : The Kernel was not found.", 0x0A, 0x0D, 0 + DiskError db "Second Stage Error : The Disk has crashed.", 0x0A, 0x0D, 0 + filename db "KERNEL BIN" %include "boot/loader/io/lmmem.asm" %include "boot/loader/io/lmterm.asm" @@ -229,10 +236,9 @@ main64: mov bh, 1 call ata_read - call bitemporize ; Temporized because the ATA drive must be ready + mov bl, 0x0D + mov esi, EndOfLoader + call write jmp Die - ; times 1024 nop - ; XXX ; - ; It seems impossible to have an executable > 2.0 kB... diff --git a/build/bin/disk.img b/build/bin/disk.img index 9c358523e8bbaca4b9d9434333ea44f8a2df44e9..20e2f58b253e90add0569dae03006333c61d2a54 100644 GIT binary patch delta 21790 zcmeI33tUuH9>?z;-h;?vP{BlzBC*ny9$Jc8_989c;e!Os8~ZjhR^4F=FGW&hT;DH z=bZofpK}+kd^ltwShr4=+Z4Yc=Lec24E>5zf*%>OBsk@Gy{OwoldJDa9-KT_lI(cx zD>Vd3(%zX;H|Yjxq;!)ME-f7;rJ08nr9`+4=@(#FyrO8{EhVNm<(~uO_st>pHI|`@ z!D!xKzahFNN-1$9W)*5rvp`^f_rm2peUzo2w)F$X#lOz=cl$!52 z6dSq*bxEoW`Y>RPdEuc)ET2XCg$1OylcX3+M1)@#%cd{oZszQ&Azf~qH90W3_s-3oGBbYu^z7{TJ11o4#&N2zyxN76kNCzoYv#&*5-ntd|Uu^vc?G;cMx3AHVM;z@B|%yN_%KlI@tx zjLqo-rc9eYYn(AmN{18Bt5hPdkR7t+*mH79j2*`GC1^fclcB4jby^WvgXViOEQv$s3B#I@c7BYpS@d)h`Z*D*GT$ zvfo4MphuEAPpV3>>O%OQ^h+lYz8$XHQdsyZ(cdreR)6;?FDBs@Q{I-2i910VO9MO& z_tn(z$7qK8D3ZSy%fDa9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)RD@Dtry`7s sa4I6Ga8Ti-LZl*+irQ2}Q4vi=3>C3d)S;p-74@j7&x-MQU=`he0Db?sAOHXW diff --git a/build/bin/loader.bin b/build/bin/loader.bin index 94295b38065e9f2ccacaccf7c1b8e8f8d67199de..319d77224306d3f677ecbcf3c3f3620a36b9df38 100644 GIT binary patch delta 564 zcmX@X|C^t)=S1I)z5^X%yC-rkXSpD}=*Gk?ULxOwUTps#7q%~0=*9Z~aWAg_58L-` zvJj&N+YRBg|7I5^J1~l}-w|eD=x4n(IfhY^dpVHJ!0?*=80+%M4UBDkH-P*shX1A) zfb^^Q$s$b4)m`&a6!Oy)a`F>XQj7GGGV}Blf-8$lQgao`GjnnjG7@u26co95C+}i9 zDFZT&u|#k)&%QrG3=A()fM&n!{QLj^W-g#J<}+I}-kW@zSqVt~W0tDF2UN`D(afUy z4a9hTJ3j8PM>mV=J0Q=4kwq0G4Wk*4Gai4vD*z-7QtY7#k=DHKaU3E7@53sOKUdv`>cwPi#CIds-t`Cf#({kfC^X~%*y~u}nXAg+|;m`m7AP<0?S}~cQ zUA-RU_^>Vf#F38P|wSafB*mA%mp<5II}h5w#m#aO7+`- z%EH3e*f5kR*l?A|+whiHuIjk4%A=V@6(q$3Vw?jpUf+(7JM7WTqIwL-^I(KX!)V6i zjK^Q^3IIui6nkhwq&2U59EXU2oE-6b#_MU_HehqZ!UK0QFflL~tif*Eqvw%8>lmV4 z^HLP@(-eYJlS}f8V!3!3HeY1<&p7!Un~laA5s;w_3~9SQFn&(Ujo-|_4>EVIl7-kbsv!0!|;KV;YBx4W;6G`-Jq~p3}W-|vj?%KgV?|z0_ L$bH%J^Z$PU%J|#x diff --git a/build/obj/boot/loader.bin b/build/obj/boot/loader.bin index 94295b38065e9f2ccacaccf7c1b8e8f8d67199de..319d77224306d3f677ecbcf3c3f3620a36b9df38 100644 GIT binary patch delta 564 zcmX@X|C^t)=S1I)z5^X%yC-rkXSpD}=*Gk?ULxOwUTps#7q%~0=*9Z~aWAg_58L-` zvJj&N+YRBg|7I5^J1~l}-w|eD=x4n(IfhY^dpVHJ!0?*=80+%M4UBDkH-P*shX1A) zfb^^Q$s$b4)m`&a6!Oy)a`F>XQj7GGGV}Blf-8$lQgao`GjnnjG7@u26co95C+}i9 zDFZT&u|#k)&%QrG3=A()fM&n!{QLj^W-g#J<}+I}-kW@zSqVt~W0tDF2UN`D(afUy z4a9hTJ3j8PM>mV=J0Q=4kwq0G4Wk*4Gai4vD*z-7QtY7#k=DHKaU3E7@53sOKUdv`>cwPi#CIds-t`Cf#({kfC^X~%*y~u}nXAg+|;m`m7AP<0?S}~cQ zUA-RU_^>Vf#F38P|wSafB*mA%mp<5II}h5w#m#aO7+`- z%EH3e*f5kR*l?A|+whiHuIjk4%A=V@6(q$3Vw?jpUf+(7JM7WTqIwL-^I(KX!)V6i zjK^Q^3IIui6nkhwq&2U59EXU2oE-6b#_MU_HehqZ!UK0QFflL~tif*Eqvw%8>lmV4 z^HLP@(-eYJlS}f8V!3!3HeY1<&p7!Un~laA5s;w_3~9SQFn&(Ujo-|_4>EVIl7-kbsv!0!|;KV;YBx4W;6G`-Jq~p3}W-|vj?%KgV?|z0_ L$bH%J^Z$PU%J|#x From b102824b15766757c845a523117d7d4a23b404ce Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 17 Jan 2019 14:40:39 +0100 Subject: [PATCH 2/5] BUG --- boot/loader/loader.asm | 1 + build/bin/disk.img | Bin 18944000 -> 18944000 bytes build/bin/loader.bin | Bin 2043 -> 2049 bytes build/obj/boot/loader.bin | Bin 2043 -> 2049 bytes 4 files changed, 1 insertion(+) diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index 2cfc899..dff8458 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -242,3 +242,4 @@ main64: jmp Die +times 6 db 0 diff --git a/build/bin/disk.img b/build/bin/disk.img index 20e2f58b253e90add0569dae03006333c61d2a54..8de11c32b3e39c3dedd2495de5d186a1ac916354 100644 GI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-@rk|~k5FTQsIjy})1CSoeO1!CNAwy_Jz$hxEHq6Nnw?Ie>3xOP_AfN4f1#2j zx7WP-zODsay5_f*D~X8(;noC+0XnYE)z4heUkU5G!so1Vyxo6Ixss5~Jsa5>YH+o~ zB57M(`uXs=Q^AlSh3Xnk#9?uAe%i&uX>W?F z&TeU3pzN0K5I?MoNX=#QoD_qe0 z{r&p4Ts+?p{pA#uucH93v>BslHmU<`s)&K9Lztv&d5P8Y1 z$2QnmsSowN{(C-6TkYE(wD*3RV*4*zQQ;_m?FmISjVQd%pLJ5%kzJxS+jN;#x-%>m za>%Bi3bT;++w@Puy`L+34~9$i+roNTzgMl3|1q+++Jk2Csy%Jfo5JU|C!5UTCU-Kv zMPhsh*wh|ytFgv0$%F_*!i*?b5RDiNLM-Ai7*^O|hXYQypdubakbp!C#bvl0!;pmG z7=e*UMhd=xR9t~?VieMlj&C6YnaILuxZ%MVjKw%yiENC=1biDgn21T3j48MZSK}Jw zVk+`54cB5i@-YK5aUH&cStvjuif}z<u@Kku^xBf zZq#4{evXaUgw5E3TGZhes7C{QxCdKtFZ{R<_v4rN6&}EYXv9N!7>{5ZevRMYQEbO! zXu@ytIDUs6_&uJ$lh}!;@CP(w7ygJpp#@Ll89a;Kcn;4)Ln{J!0WYErFX7Ml3)-;< zFJmwMidWEqzv1uLhgZ>w*YG<2fj96by09N_;cXniJ2;3#co*;CeH_LSe1H$}5p;Zv zAiD7hj^Y^pi4czC1Ww` Date: Thu, 17 Jan 2019 15:05:03 +0100 Subject: [PATCH 3/5] BUG + stuff --- boot/loader/loader.asm | 14 +++++++------- build/bin/disk.img | Bin 18944000 -> 18944000 bytes build/bin/loader.bin | Bin 2049 -> 2063 bytes build/obj/boot/loader.bin | Bin 2049 -> 2063 bytes 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index dff8458..c67f038 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -94,13 +94,13 @@ main32: ;; VERIFY A20 pushad - mov edi,0x112345 ;odd megabyte address. - mov esi,0x012345 ;even megabyte address. - mov [esi],esi ;making sure that both addresses contain diffrent values. - mov [edi],edi ;(if A20 line is cleared the two pointers would point to the address 0x012345 that would contain 0x112345 (edi)) - cmpsd ;compare addresses to see if the're equivalent. + mov edi,0x112345 ;odd megabyte address. + mov esi,0x012345 ;even megabyte address. + mov [esi],esi ;making sure that both addresses contain diffrent values. + mov [edi],edi ;(if A20 line is cleared the two pointers would point to the address 0x012345 that would contain 0x112345 (edi)) + cmpsd ;compare addresses to see if the're equivalent. popad - jne .A20_on ;if not equivalent , A20 line is set. + jne .A20_on ;if not equivalent , A20 line is set. mov WORD [A20_OK], 0 jmp .A20_end .A20_on: @@ -242,4 +242,4 @@ main64: jmp Die -times 6 db 0 +times 20 db 0 diff --git a/build/bin/disk.img b/build/bin/disk.img index 8de11c32b3e39c3dedd2495de5d186a1ac916354..1dc81cd507c6b735120973f2a014c138ac11c135 100644 GIT binary patch delta 4822 zcmZoz(8AdO#2cn@Ocr3$VM=1yKGBBR#(xsWG*tcyT_pykGDAN@KL!SAAf3p-&%wa( zG?zh_!IZ(7A&P;YVXEb1{}pxuDT+{KAR4HwdG3n#xhojA&t1WkJbl{_CPuE!8#w;6 z(w-II5432M7!8BbbTFC?MvH>cl5n&n94!e)OTy8TaI_>GEeS_U!qJj&v?LrY2}eu9 KVOJ8?%K-o;*rDJ6 delta 5310 zcmZoz(8AdO#2cn@F!?ZSpZJ9-pLuh^$_Z|p1(-5fCvi-}Cb~jbC8egw(9h71fuRaW zdowU{FfcsLWzc1C2Eq^qeuk;$3}KT6SK9K!)PZQAy5_kn+UKrd+&*^&Q}T3(MPS=T z3AfSE0H(UpbTAqQpweeF9gK#-XgV0p2BSs6Xh}F)5{{OHqb1>JNjO>(j+TU@CE;jE PI9d{pmV}^^(7_P^>^;)y diff --git a/build/bin/loader.bin b/build/bin/loader.bin index 5dc46811a611e4f7cc01ec9869be362fa05e1dc4..6ca87c7c969db4b99dc492b1f72a547ae343898e 100644 GIT binary patch delta 11 ScmZn^=oi?)$T9gjGampI&;xY< delta 7 OcmeAdXcXAM$N>NeF9GTR diff --git a/build/obj/boot/loader.bin b/build/obj/boot/loader.bin index 5dc46811a611e4f7cc01ec9869be362fa05e1dc4..6ca87c7c969db4b99dc492b1f72a547ae343898e 100644 GIT binary patch delta 11 ScmZn^=oi?)$T9gjGampI&;xY< delta 7 OcmeAdXcXAM$N>NeF9GTR From 98c2dd8502f4ad5246cf8e78d85f13e19bf37069 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Sat, 19 Jan 2019 22:36:38 +0100 Subject: [PATCH 4/5] CRT library stuff --- boot/mbr/mbr.asm | 1 + boot/mbr/mbr.inc | 2 +- kaleid/common/itoa.c | 68 ++++++++++++---- kaleid/common/memory.c | 18 ++--- kaleid/common/string.c | 64 +++++++++++++-- kaleid/include/common/kalcrt.h | 4 +- kaleid/include/common/kaldefs.h | 18 +++-- kaleid/include/common/kalerror.h | 2 +- kaleid/include/common/kallist.h | 2 +- kaleid/include/common/kalmask.h | 6 +- kaleid/include/kernel/kernbase.h | 12 ++- kaleid/include/kernel/kernlocks.h | 10 ++- kaleid/include/kernel/kernsched.h | 19 ++++- kaleid/kernel/ke/panic.c | 5 +- kaleid/kernel/proc/sched.c | 127 +++++++++++++++++------------- 15 files changed, 245 insertions(+), 113 deletions(-) diff --git a/boot/mbr/mbr.asm b/boot/mbr/mbr.asm index 42f0dbd..f94c713 100644 --- a/boot/mbr/mbr.asm +++ b/boot/mbr/mbr.asm @@ -123,6 +123,7 @@ load_root: mov di, BUFFER_OFF ; Set es:di to the disk buffer mov cx, word [rootDirEntries] ; Search through all of the root dir entries xor ax, ax ; Clear ax for the file entry offset + search_root: xchg cx, dx ; Save cx because it's a loop counter mov si, filename ; Load the filename diff --git a/boot/mbr/mbr.inc b/boot/mbr/mbr.inc index dc11db1..a5f396d 100644 --- a/boot/mbr/mbr.inc +++ b/boot/mbr/mbr.inc @@ -101,7 +101,7 @@ read_sectors: shl ah, 1 or cl, ah ; Now cx is set with respective track and sector numbers mov dl, byte [Bootdrv] ; Set correct Bootdrv for int 13h - mov di, 21 ; Try five times to read the sector because i love 21 + mov di, 21 ; Try five times to read the sector because I love 21 .attempt_read: mov ax, 0x0201 ; Read Sectors func of int 13h, read one sector int 0x13 ; Call int 13h (BIOS disk I/O) diff --git a/kaleid/common/itoa.c b/kaleid/common/itoa.c index e24a41a..a5e3383 100644 --- a/kaleid/common/itoa.c +++ b/kaleid/common/itoa.c @@ -10,59 +10,89 @@ #include // -// Digits table for bases <=36 +// Digits table for bases <=36 (unused) // static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + // // Integer to string in any base between 2 and 36 (included) // + #if defined(_NEED_ITOA) + char *itoa(int i, char *str, int base) +#define _IL_MIN INT_MIN +#define _IL_MIN_STRING "-2147483648" + #elif defined(_NEED_LTOA) + char *ltoa(long i, char *str, int base) +#define _IL_MIN LONG_MIN +#define _IL_MIN_STRING "-9223372036854775808" + #elif defined(_NEED_UTOA) + char *utoa(uint i, char *str, int base) + #elif defined(_NEED_ULTOA) + char *ultoa(ulong i, char *str, int base) + #else #error "What am I supposed to declare?" #endif + { -#if defined(_NEED_ITOA) || defined(_NEED_LTOA) - int neg = 0; -#endif - char *orig = str; - // - // Only handle base 2 -> 36 - // - if (base < 2 || base > 36) - return NULL; - #if defined(_NEED_ITOA) || defined(_NEED_LTOA) // // Deal with negatives // - if (i < 0) { - neg = 1; - i = -i; + int neg = 0; + if (i < 0 && base == 10) { + // + // Handle INT_MIN and LONG_MIN... + // + if (__builtin_expect(i == _IL_MIN, 0)) { + strcpy(orig, _IL_MIN_STRING); + goto leave; + } + + else { + neg = 1; + i = -i; + } } #endif + // + // Only handle base 2 -> 36 + // + if (base < 2 || base > 36) { + __set_errno(EINVAL); + *orig = '\0; + goto leave; + } + // // Deal with zero separately // if (i == 0) { *str++ = '0'; + *str = '\0'; + goto leave; } // // Compute digits... in reverse order // while (i > 0) { - *str++ = digits[i % base]; + rem = i % base; + *str++ = (rem > 9) + ? (rem - 10) + 'a' + : rem + '0'; i /= base; } @@ -75,7 +105,13 @@ char *ultoa(ulong i, char *str, int base) // // Reverse the string // - return strrev2(orig); + orig = strrev2(orig); + + // + // End of conversion + // +leave: + return orig; } diff --git a/kaleid/common/memory.c b/kaleid/common/memory.c index 3497a38..20a9dd2 100644 --- a/kaleid/common/memory.c +++ b/kaleid/common/memory.c @@ -58,7 +58,7 @@ void *memset(void *ptr, int val, size_t bytes) } // -// Set "words"-many words starting from ptr to val +// Set "words"-many words starting from ptr to val // void *memsetw(void *ptr, int val, size_t words) { @@ -99,8 +99,8 @@ void *memsetw(void *ptr, int val, size_t words) } // -// Set "dwords"-many dwords starting from ptr to val -// XXX unimplemented +// Set "dwords"-many dwords starting from ptr to val +// XXX unimplemented // void *memsetd(void *ptr, int val, size_t dwords) { @@ -111,7 +111,7 @@ void *memsetd(void *ptr, int val, size_t dwords) } // -// Set "qwords"-many qwords starting from ptr to val +// Set "qwords"-many qwords starting from ptr to val // void *memsetq(void *ptr, long val, size_t qwords) { @@ -130,7 +130,7 @@ void *memsetq(void *ptr, long val, size_t qwords) //------------------------------------------// // -// Set "bytes"-many bytes starting from ptr to 0 +// Set "bytes"-many bytes starting from ptr to 0 // void *memzero(void *ptr, size_t bytes) { @@ -138,8 +138,8 @@ void *memzero(void *ptr, size_t bytes) } // -// Copy "bytes"-many bytes of src to dst -// Does not deal with overlapping blocks (memmove's job) +// Copy "bytes"-many bytes of src to dst +// Does not deal with overlapping blocks (memmove's job) // void *memcpy(void *restrict dst, const void *restrict src, size_t bytes) { @@ -203,7 +203,7 @@ void *memcpy(void *restrict dst, const void *restrict src, size_t bytes) } // -// Move memory from src to dest, even if they overlap +// Move memory from src to dest, even if they overlap // void *memmove(void *dst, const void *src, size_t bytes) { @@ -228,7 +228,7 @@ void *memmove(void *dst, const void *src, size_t bytes) } // -// Compare memory areas +// Compare memory areas // int memcmp(const void *ptr1, const void *ptr2, size_t bytes) { diff --git a/kaleid/common/string.c b/kaleid/common/string.c index fafaf89..d7c391d 100644 --- a/kaleid/common/string.c +++ b/kaleid/common/string.c @@ -235,7 +235,7 @@ char *strncpy(char *restrict dest, const char *restrict src, size_t n) // Returns TRUE if dest would have been null-terminated // by ordinary strncpy(), and FALSE otherwise // -int xstrncpy(char *restrict dest, const char *restrict src, size_t n) +int strnzcpy(char *restrict dest, const char *restrict src, size_t n) { size_t it; @@ -243,9 +243,7 @@ int xstrncpy(char *restrict dest, const char *restrict src, size_t n) dest[it] = src[it]; } - // // Was the copy complete? - // if (it == n) { if (dest[n] == 0) { return TRUE; @@ -261,11 +259,63 @@ int xstrncpy(char *restrict dest, const char *restrict src, size_t n) } // -// XXX strcat family +// Appends a copy of src at the end of dest // -char *strcat (char *restrict, const char *restrict); -char *strncat (char *restrict, const char *restrict, size_t); -int *xstrncat(char *restrict, const char *restrict, size_t); +char *strcat(char *restrict dest, const char *restrict src) +{ + char *base = dest; + while (*dest) dest++; + while ((*dest++ = *src++)); + return base; +} + +// +// Appends a copy of at most n bytes of src at the end of dest +// +char *strncat(char *restrict dest, const char *restrict src, size_t n) +{ + size_t it, off = 0; + + while (dest[off]) off++; + + for (it = 0; it < n && src[it]; it++) { + dest[it+off] = src[it]; + } + + while (it++ < n) dest[it+off] = 0; + + return dest; +} + +// +// Appends a copy of at most n bytes of src at the end of dest +// Always null-terminates, and returne TRUE or FALSE depending on whether +// regular strcat() would have null-terminated this string, or not +// +int *strnzcat(char *restrict dest, const char *restrict src, size_t n) +{ + size_t it, off = 0; + + while (dest[off]) off++; + + for (it = 0; it < n - 1 && src[it]; it++) { + dest[it+off] = src[it]; + } + + // Was the copy complete? + if (it == n) { + if (dest[n+off] == 0) { + return TRUE; + } + + dest[n+off] = 0; + return FALSE; + } + + while (it++ < n) dest[it+off] = 0; + + return TRUE; +} // // Reverses the string src, putting the result into dest diff --git a/kaleid/include/common/kalcrt.h b/kaleid/include/common/kalcrt.h index 167ab52..165ba90 100644 --- a/kaleid/include/common/kalcrt.h +++ b/kaleid/include/common/kalcrt.h @@ -143,11 +143,11 @@ char *strtok_r(char *restrict, const char *restrict, char **restrict); char *strcpy (char *restrict, const char *restrict); char *strncpy (char *restrict, const char *restrict, size_t); -int xstrncpy(char *restrict, const char *restrict, size_t); +int strnzcpy(char *restrict, const char *restrict, size_t); char *strcat (char *restrict, const char *restrict); char *strncat (char *restrict, const char *restrict, size_t); -int *xstrncat(char *restrict, const char *restrict, size_t); +int *strnzcat(char *restrict, const char *restrict, size_t); char *strrev(char *restrict, const char *restrict); char *strrev2(char *); diff --git a/kaleid/include/common/kaldefs.h b/kaleid/include/common/kaldefs.h index 9dbf1be..cc43ee9 100644 --- a/kaleid/include/common/kaldefs.h +++ b/kaleid/include/common/kaldefs.h @@ -23,11 +23,7 @@ #endif #ifndef NULL -#define NULL ((void *)0) -#endif - -#ifndef INITOK -#define INITOK ((unsigned int)0xCAFEBABE) +#define NULL 0L #endif //------------------------------------------// @@ -61,8 +57,8 @@ // Attributes and macros // //------------------------------------------// -#ifndef PACKED -#define PACKED __attribute__((__packed__)) +#ifndef _PACKED +#define _PACKED __attribute__((__packed__)) #endif #ifndef noreturn @@ -77,6 +73,14 @@ #define unlikely(x) (__builtin_expect((x), 0)) #endif +#ifndef _STR +#define _STR(x) #x +#endif + +#ifndef _XSTR +#define _XSTR(x) _STR(x) +#endif + //------------------------------------------// // API specific macros // //------------------------------------------// diff --git a/kaleid/include/common/kalerror.h b/kaleid/include/common/kalerror.h index ce8b2c4..b29c660 100644 --- a/kaleid/include/common/kalerror.h +++ b/kaleid/include/common/kalerror.h @@ -11,7 +11,7 @@ #define _KALERROR_H //------------------------------------------// -// Preprocessor constants // +// "errno" values // //------------------------------------------// // Everything went fine diff --git a/kaleid/include/common/kallist.h b/kaleid/include/common/kallist.h index 96c5f9d..8257b64 100644 --- a/kaleid/include/common/kallist.h +++ b/kaleid/include/common/kallist.h @@ -204,7 +204,7 @@ static inline ListHead_t } // -// Remove node of list +// Remove node of list (and frees it) // static inline ListHead_t *RemoveNode(ListHead_t *head, ListNode_t *node) diff --git a/kaleid/include/common/kalmask.h b/kaleid/include/common/kalmask.h index 0295bd0..c185354 100644 --- a/kaleid/include/common/kalmask.h +++ b/kaleid/include/common/kalmask.h @@ -51,11 +51,11 @@ #define strcpy _osk_strcpy #define strncpy _osk_strncpy -#define xstrncpy _osk_xstrncpy +#define strnzcpy _osk_strnzcpy #define strcat _osk_strcat #define strncat _osk_strncat -#define xstrncat _osk_xstrncat +#define strnzcat _osk_strnzcat #define strrev _osk_strrev #define strrev2 _osk_strrev2 @@ -105,7 +105,7 @@ //------------------------------------------// -#define strerror _osk_strerror +#define strerror _osk_strerror //------------------------------------------// // End of header // diff --git a/kaleid/include/kernel/kernbase.h b/kaleid/include/kernel/kernbase.h index baf8e95..54307c7 100644 --- a/kaleid/include/kernel/kernbase.h +++ b/kaleid/include/kernel/kernbase.h @@ -59,6 +59,11 @@ typedef enum { // Multiprocessor misc. // //------------------------------------------// + +#ifndef INITOK +#define INITOK ((unsigned int)0xCAFEBABE) +#endif + #ifndef NCPU #define NCPU 4 #endif @@ -108,12 +113,6 @@ DECLARE_PER_CPU(CurThread, Thread_t *); // global constants // //------------------------------------------// -#define SetPanicStr(str) \ - do { \ - SetKernState(KSTATE_PANIC); \ - _SetPanicStr(str); \ - } while (0) - #define SetKernState(x) \ do { \ _SetKernState(x); \ @@ -184,7 +183,6 @@ void WriteByteOnPort(port_t port, port_t val) static inline uchar ReadByteFromPort(port_t port) { - KalAssert(FALSE && ENOSYS); (void)port; return 0; diff --git a/kaleid/include/kernel/kernlocks.h b/kaleid/include/kernel/kernlocks.h index f3dd7e2..6d32224 100644 --- a/kaleid/include/kernel/kernlocks.h +++ b/kaleid/include/kernel/kernlocks.h @@ -70,11 +70,15 @@ typedef struct sLock_t { //------------------------------------------// // -// Linux syscall... +// Linux syscall vs unimplemented syscall... // #ifndef _KALEID_KERNEL +#ifdef _OSK_SOURCE +int KalYieldCPU(void), +#else int sched_yield(void); #endif +#endif // // Initialize a lock @@ -127,7 +131,11 @@ void AquireLock(Lock_t *lock) StartPanic("AquireLock on an already locked object"); #else if likely (lock->type == KLOCK_SPINLOCK) continue; +#ifdef _OSK_SOURCE + else KalYieldCPU(); +#else else sched_yield(); +#endif #endif } __sync_synchronize(); diff --git a/kaleid/include/kernel/kernsched.h b/kaleid/include/kernel/kernsched.h index 1026d53..d99f8de 100644 --- a/kaleid/include/kernel/kernsched.h +++ b/kaleid/include/kernel/kernsched.h @@ -30,9 +30,10 @@ // Preprocessor // //------------------------------------------// +// +// Debug stuff +// #define printdbg printf -#define _STR(x) #x -#define _XSTR(x) _STR(x) // // States for a process @@ -56,6 +57,8 @@ DECLARE_PER_CPU(ReglPrioProcs, ListHead_t *); DECLARE_PER_CPU(ServPrioProcs, ListHead_t *); DECLARE_PER_CPU(TimeCritProcs, ListHead_t *); +extern const char *PrioClassesNames[]; + //------------------------------------------// // Data types // //------------------------------------------// @@ -63,7 +66,7 @@ DECLARE_PER_CPU(TimeCritProcs, ListHead_t *); // // A process // -typedef struct sProcess_t{ +typedef struct sProcess_t { // Identifier int pid; @@ -94,6 +97,16 @@ typedef struct sProcess_t{ } Process_t; +//------------------------------------------// +// Functions // +//------------------------------------------// + +void SchedInit(void); +void SchedFini(void); + +void SchedThisProc(Process_t *); +void SchedOnTick(void); + //------------------------------------------// // End of header // //------------------------------------------// diff --git a/kaleid/kernel/ke/panic.c b/kaleid/kernel/ke/panic.c index 72fe089..79c9a89 100644 --- a/kaleid/kernel/ke/panic.c +++ b/kaleid/kernel/ke/panic.c @@ -34,6 +34,7 @@ noreturn void StartPanic(const char *str) { DisableIRQs(); + // This should be made atomic SetKernState(KSTATE_PANIC); if (GetCurProc()) __CurProc[GetCurCPU()] = NULL; @@ -46,11 +47,11 @@ noreturn void StartPanic(const char *str) } if (GetPanicStr()) { - GetStdOut()->PrintOnTermUnlocked(GetStdOut(), "double panic!\n"); + GetStdOut()->PrintOnTermUnlocked(GetStdOut(), "\nDouble panic!\n"); HaltCPU(); } - SetPanicStr(str); + _SetPanicStr(str); GetStdOut()->PrintOnTermUnlocked(GetStdOut(), "PANIC! - "); GetStdOut()->PrintOnTermUnlocked(GetStdOut(), str); diff --git a/kaleid/kernel/proc/sched.c b/kaleid/kernel/proc/sched.c index b81ec45..ad040b1 100644 --- a/kaleid/kernel/proc/sched.c +++ b/kaleid/kernel/proc/sched.c @@ -66,12 +66,13 @@ void SchedUnlock(void) { // // The four priority classes of OS/2 // + CREATE_PER_CPU(IdlePrioProcs, ListHead_t *); CREATE_PER_CPU(ReglPrioProcs, ListHead_t *); CREATE_PER_CPU(ServPrioProcs, ListHead_t *); CREATE_PER_CPU(TimeCritProcs, ListHead_t *); -char *PrioClassesNames[] = { +const char *PrioClassesNames[] = { "Idle priority class", "Regular priority class", "Server priority class", @@ -203,47 +204,55 @@ void BlockCurProc(void) SetCurProc(SelectSchedNext()); } +static inline +void ReSchedCurProc(void) +{ + KalAssert(GetCurProc() && GetCurProc()->procState == STATE_RUNNING); + + // Restore default attributes, cancelling boosts + GetCurProc()->prioClass = GetCurProc()->defPrioClass; + GetCurProc()->prioLevel = GetCurProc()->defPrioLevel; + GetCurProc()->timeSlice = GetCurProc()->defTimeSlice; + GetCurProc()->procState = STATE_RUNNABLE; + + // Remove from list + RemoveNode(GetCurProc()->schedNode->head, GetCurProc()->schedNode); + GetCurProc()->schedNode = NULL; + + // Schedule again, with default attributes now + SchedThisProcUnlocked(GetCurProc()); +} + // // Should we schedule another process? // Called at each tick // void SchedOnTick(void) { - Process_t *procNext; - Process_t *winner; - SchedLock(); + Process_t *procNext, *winner, *previous = GetCurProc(); + // // We're either idle or running something // KalAssert(GetCurProc() == NULL || GetCurProc()->procState == STATE_RUNNING); // - // Has current process spent his timeslice? + // Have the current process spent its timeslice? // (To be handled in CPU decisions function) // if (GetCurProc() != NULL) { if (GetCurProc()->timeSlice <= 1) { + // Re-schedule + ReSchedCurProc(); - // Restore default attributes, cancelling boosts - GetCurProc()->prioClass = GetCurProc()->defPrioClass; - GetCurProc()->prioLevel = GetCurProc()->defPrioLevel; - GetCurProc()->timeSlice = GetCurProc()->defTimeSlice; - GetCurProc()->procState = STATE_RUNNABLE; - - // Remove from list - RemoveNode(GetCurProc()->schedNode->head, GetCurProc()->schedNode); - - // Schedule again, with default attributes now - SchedThisProcUnlocked(GetCurProc()); - - // Mark as idle - SetCurProc(NULL); + // See next 'if' statement + _SetCurProc(NULL); } // - // Otherwise, make him lose a tick + // Otherwise, make it lose a tick // else { GetCurProc()->timeSlice--; @@ -268,8 +277,10 @@ void SchedOnTick(void) // Yes, procNext should preempt current process // if (winner == procNext) { - GetCurProc()->procState = STATE_RUNNABLE; - SchedThisProcUnlocked(GetCurProc()); + // Re-schedule + ReSchedCurProc(); + + // Switch to procNext SetCurProc(procNext); } @@ -278,32 +289,10 @@ void SchedOnTick(void) // leave: SchedUnlock(); -} -#define PrintProc(proc) printdbg("{ %d, '%s', %d , %d}\n", (proc)->pid, \ - PrioClassesNames[(proc)->prioClass], (proc)->prioLevel, (proc)->timeSlice); - -// -// Print out process list -// -void PrintList(ListHead_t *head) -{ - KalAssert(head); - - Process_t *proc; - ListNode_t *node = head->first; - - printdbg("len: %d\n", head->length); - - while (node) { - proc = GetNodeData(node, Process_t *); - - PrintProc(proc); - - node = node->next; + if (GetCurProc() != NULL && GetCurProc() != previous) { + // XXX context switch } - - puts(""); } // @@ -355,6 +344,33 @@ void FiniSched(void) } #ifndef _KALEID_KERNEL + +#define PrintProc(proc) printdbg("{ %d, '%s', %d , %d}\n", (proc)->pid, \ + PrioClassesNames[(proc)->prioClass], (proc)->prioLevel, (proc)->timeSlice); + +// +// Print out process list +// +void PrintList(ListHead_t *head) +{ + KalAssert(head); + + Process_t *proc; + ListNode_t *node = head->first; + + printdbg("len: %d\n", head->length); + + while (node) { + proc = GetNodeData(node, Process_t *); + + PrintProc(proc); + + node = node->next; + } + + puts(""); +} + int main(void) { InitSched(); @@ -379,7 +395,17 @@ int main(void) int tick = 0; - while (tick < 20) { + while (tick < 120) { + if (tick > 0 && tick != 50 && tick % 10 == 0) { + puts("Blocking current process"); + BlockCurProc(); + } + + if (tick == 50) { + procs[2].procState = STATE_RUNNABLE; + SchedThisProc(&procs[2]); + } + printf("Tick %d - Running: ", tick); if (GetCurProc() == NULL) { @@ -390,14 +416,8 @@ int main(void) PrintProc(GetCurProc()); } - if (tick == 9 || tick == 14) { - puts("Blocking current process"); - BlockCurProc(); - } - SchedOnTick(); - //puts("\n---------------"); tick++; } @@ -405,5 +425,6 @@ int main(void) return 0; } + #endif From 52648a99079d59b852a2389711c654c47abc0af4 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 21 Jan 2019 09:53:54 +0100 Subject: [PATCH 5/5] Fixing broken stuff --- Makefile.in | 2 +- kaleid/common/itoa.c | 18 ++++++++--- kaleid/common/rand.c | 4 +-- kaleid/common/sprintf.c | 4 +++ kaleid/common/string.c | 2 +- kaleid/include/common/kalcrt.h | 2 +- kaleid/include/common/kaldefs.h | 4 +++ kaleid/include/kalkern.h | 7 ++++ kaleid/include/kernel/kernbase.h | 1 - kaleid/kernel/proc/Makefile | 2 +- kaleid/kernel/proc/sched.c | 55 +++++++++++++++++++------------- 11 files changed, 66 insertions(+), 35 deletions(-) diff --git a/Makefile.in b/Makefile.in index 3b7a9c6..e0deb10 100644 --- a/Makefile.in +++ b/Makefile.in @@ -13,7 +13,7 @@ CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" CC2NAME=gcc COPTIM=-O2 -CWARNS=-Wall -Wextra -Wshadow -Wpedantic +CWARNS=-Wall -Wextra -Wshadow // -Wpedantic CINCLUDES=-isystem./kaleid/include CFLAGS1=-std=gnu11 -nostdlib -ffreestanding -mcmodel=large diff --git a/kaleid/common/itoa.c b/kaleid/common/itoa.c index a5e3383..1144e26 100644 --- a/kaleid/common/itoa.c +++ b/kaleid/common/itoa.c @@ -12,8 +12,10 @@ // // Digits table for bases <=36 (unused) // +#if 0 static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; +#endif // // Integer to string in any base between 2 and 36 (included) @@ -21,29 +23,35 @@ static const char digits[] = #if defined(_NEED_ITOA) -char *itoa(int i, char *str, int base) #define _IL_MIN INT_MIN #define _IL_MIN_STRING "-2147483648" +char *itoa(int i, char *str, int base) +{ + int rem; #elif defined(_NEED_LTOA) -char *ltoa(long i, char *str, int base) #define _IL_MIN LONG_MIN #define _IL_MIN_STRING "-9223372036854775808" +char *ltoa(long i, char *str, int base) +{ + long rem; #elif defined(_NEED_UTOA) char *utoa(uint i, char *str, int base) +{ + uint rem; #elif defined(_NEED_ULTOA) char *ultoa(ulong i, char *str, int base) +{ + ulong rem; #else #error "What am I supposed to declare?" #endif - -{ char *orig = str; #if defined(_NEED_ITOA) || defined(_NEED_LTOA) @@ -72,7 +80,7 @@ char *ultoa(ulong i, char *str, int base) // if (base < 2 || base > 36) { __set_errno(EINVAL); - *orig = '\0; + *orig = '\0'; goto leave; } diff --git a/kaleid/common/rand.c b/kaleid/common/rand.c index fe576ef..41ca5d9 100644 --- a/kaleid/common/rand.c +++ b/kaleid/common/rand.c @@ -20,8 +20,8 @@ static ulong next = 7756; // int rand(void) { - next = next * 1103515245 + 12345; - return (uint)(next / 65536) % INT_MAX; + next = next * 1103515245 + 12347; + return (uint)(next / 65536); } // diff --git a/kaleid/common/sprintf.c b/kaleid/common/sprintf.c index a8dd3ba..fdd48dc 100644 --- a/kaleid/common/sprintf.c +++ b/kaleid/common/sprintf.c @@ -12,6 +12,9 @@ // // Format str according to fmt using ellipsed arguments // +// BE CAREFUL when using this +// you need to know for sure an overflow won't happen +// int sprintf(char *str, const char *fmt, ...) { int ret; @@ -31,6 +34,7 @@ int vsprintf(char *str, const char *fmt, va_list ap) // // (v)sprintf() but with a size limit: no more than n bytes are written in str +// Always null-terminate str // int snprintf(char *str, size_t n, const char *fmt, ...) { diff --git a/kaleid/common/string.c b/kaleid/common/string.c index d7c391d..dd1c210 100644 --- a/kaleid/common/string.c +++ b/kaleid/common/string.c @@ -292,7 +292,7 @@ char *strncat(char *restrict dest, const char *restrict src, size_t n) // Always null-terminates, and returne TRUE or FALSE depending on whether // regular strcat() would have null-terminated this string, or not // -int *strnzcat(char *restrict dest, const char *restrict src, size_t n) +int strnzcat(char *restrict dest, const char *restrict src, size_t n) { size_t it, off = 0; diff --git a/kaleid/include/common/kalcrt.h b/kaleid/include/common/kalcrt.h index 165ba90..8b9a43e 100644 --- a/kaleid/include/common/kalcrt.h +++ b/kaleid/include/common/kalcrt.h @@ -147,7 +147,7 @@ int strnzcpy(char *restrict, const char *restrict, size_t); char *strcat (char *restrict, const char *restrict); char *strncat (char *restrict, const char *restrict, size_t); -int *strnzcat(char *restrict, const char *restrict, size_t); +int strnzcat(char *restrict, const char *restrict, size_t); char *strrev(char *restrict, const char *restrict); char *strrev2(char *); diff --git a/kaleid/include/common/kaldefs.h b/kaleid/include/common/kaldefs.h index cc43ee9..c2421d6 100644 --- a/kaleid/include/common/kaldefs.h +++ b/kaleid/include/common/kaldefs.h @@ -26,6 +26,10 @@ #define NULL 0L #endif +#ifndef INITOK +#define INITOK ((unsigned int)0xCAFEBABE) +#endif + //------------------------------------------// // Keywords // //------------------------------------------// diff --git a/kaleid/include/kalkern.h b/kaleid/include/kalkern.h index df15c7d..d1121eb 100644 --- a/kaleid/include/kalkern.h +++ b/kaleid/include/kalkern.h @@ -38,6 +38,13 @@ #include #endif +// not ready for kernel compilation +#ifndef _KALEID_KERNEL +#ifndef _KALKERN_SCHED_H +#include +#endif +#endif + //------------------------------------------// // End of header // //------------------------------------------// diff --git a/kaleid/include/kernel/kernbase.h b/kaleid/include/kernel/kernbase.h index 54307c7..ab2cb76 100644 --- a/kaleid/include/kernel/kernbase.h +++ b/kaleid/include/kernel/kernbase.h @@ -59,7 +59,6 @@ typedef enum { // Multiprocessor misc. // //------------------------------------------// - #ifndef INITOK #define INITOK ((unsigned int)0xCAFEBABE) #endif diff --git a/kaleid/kernel/proc/Makefile b/kaleid/kernel/proc/Makefile index 8001a5e..7c15976 100644 --- a/kaleid/kernel/proc/Makefile +++ b/kaleid/kernel/proc/Makefile @@ -1,4 +1,4 @@ sched-test: - gcc -O2 -masm=intel -I../../include ./sched.c + gcc -O2 -Wall -Wextra -Wshadow -std=gnu11 -masm=intel -I../../include ./sched.c diff --git a/kaleid/kernel/proc/sched.c b/kaleid/kernel/proc/sched.c index ad040b1..2a8dd9e 100644 --- a/kaleid/kernel/proc/sched.c +++ b/kaleid/kernel/proc/sched.c @@ -7,7 +7,7 @@ // Desc: Scheduling algorithm // //----------------------------------------------------------------------------// -#include +#include #ifndef _KALEID_KERNEL @@ -17,17 +17,18 @@ CREATE_PER_CPU(CurProc, Process_t *); // // For test purpose only // -int procslen = 9; +int procslen = 10; Process_t procs[] = { { 0, 0, 0, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 1, 2, 2, 16, 16, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 2, 3, 3, 31, 31, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 3, 2, 2, 1, 1, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, - { 4, 0, 0, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 4, 3, 3, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 5, 0, 0, 30, 30, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 6, 1, 1, 19, 19, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 7, 1, 1, 0, 0, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 8, 3, 3, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 9, 2, 2, 21, 21, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, }; #endif @@ -67,22 +68,22 @@ void SchedUnlock(void) { // The four priority classes of OS/2 // -CREATE_PER_CPU(IdlePrioProcs, ListHead_t *); -CREATE_PER_CPU(ReglPrioProcs, ListHead_t *); -CREATE_PER_CPU(ServPrioProcs, ListHead_t *); CREATE_PER_CPU(TimeCritProcs, ListHead_t *); +CREATE_PER_CPU(ServPrioProcs, ListHead_t *); +CREATE_PER_CPU(ReglPrioProcs, ListHead_t *); +CREATE_PER_CPU(IdlePrioProcs, ListHead_t *); const char *PrioClassesNames[] = { - "Idle priority class", - "Regular priority class", - "Server priority class", "Time-critical class", + "Server priority class", + "Regular priority class", + "Idle priority class", }; -enum { IDLE_PRIO_PROC = 0, - REGL_PRIO_PROC = 1, - SERV_PRIO_PROC = 2, - TIME_CRIT_PROC = 3, +enum { TIME_CRIT_PROC = 0, + SERV_PRIO_PROC = 1, + REGL_PRIO_PROC = 2, + IDLE_PRIO_PROC = 3, }; // @@ -111,8 +112,8 @@ Process_t *CompareProcs(Process_t *proc1, Process_t *proc2) { KalAssert(proc1 && proc2); - if (proc1->prioClass > proc2->prioClass) return proc1; - if (proc1->prioClass < proc2->prioClass) return proc2; + if (proc1->prioClass < proc2->prioClass) return proc1; + if (proc1->prioClass > proc2->prioClass) return proc2; if (proc1->prioLevel > proc2->prioLevel) return proc1; if (proc1->prioLevel < proc2->prioLevel) return proc2; @@ -126,7 +127,7 @@ Process_t *CompareProcs(Process_t *proc1, Process_t *proc2) static inline void SchedThisProcUnlocked(Process_t *proc) { - KalAssert(proc && proc->procState == STATE_RUNNABLE); + KalAssert(proc && proc->procState == STATE_RUNNABLE && !proc->schedNode); bool found = false; ListNode_t *iterNode = NULL; @@ -137,13 +138,15 @@ void SchedThisProcUnlocked(Process_t *proc) proc->schedNode = procNode; - //printdbg("Adding process %d to '%s'\n", proc->pid, PrioClassesNames[proc->prioClass]); - // // Find a process with lesser priority // for (iterNode = head->first; iterNode; iterNode = iterNode->next) { if (proc->prioLevel > GetNodeData(iterNode, Process_t *)->prioLevel) { + // Detect double insertions + KalAssert(proc->pid != GetNodeData(iterNode, Process_t *)->pid); + + // Add process to schedule AddNodeBefore(head, iterNode, procNode); found = true; break; @@ -198,9 +201,12 @@ void BlockCurProc(void) ListNode_t *procNode = GetCurProc()->schedNode; + KalAssert(procNode && "Blocking non-scheduled process"); + GetCurProc()->procState = STATE_BLOCKED; RemoveNode(procNode->head, procNode); + GetCurProc()->schedNode = NULL; SetCurProc(SelectSchedNext()); } @@ -208,6 +214,7 @@ static inline void ReSchedCurProc(void) { KalAssert(GetCurProc() && GetCurProc()->procState == STATE_RUNNING); + KalAssert(GetCurProc()->schedNode); // Restore default attributes, cancelling boosts GetCurProc()->prioClass = GetCurProc()->defPrioClass; @@ -301,7 +308,6 @@ leave: void InitSched(void) { int pid; - Process_t *proc; SchedLock(); @@ -345,7 +351,7 @@ void FiniSched(void) #ifndef _KALEID_KERNEL -#define PrintProc(proc) printdbg("{ %d, '%s', %d , %d}\n", (proc)->pid, \ +#define PrintProc(proc) printdbg("{ %d, '%s', %d , %lu}\n", (proc)->pid, \ PrioClassesNames[(proc)->prioClass], (proc)->prioLevel, (proc)->timeSlice); // @@ -358,7 +364,7 @@ void PrintList(ListHead_t *head) Process_t *proc; ListNode_t *node = head->first; - printdbg("len: %d\n", head->length); + printdbg("len: %lu\n", head->length); while (node) { proc = GetNodeData(node, Process_t *); @@ -402,8 +408,8 @@ int main(void) } if (tick == 50) { - procs[2].procState = STATE_RUNNABLE; - SchedThisProc(&procs[2]); + procs[0].procState = STATE_RUNNABLE; + SchedThisProc(&procs[0]); } printf("Tick %d - Running: ", tick); @@ -418,6 +424,9 @@ int main(void) SchedOnTick(); + if (tick == 50) // already done + puts("Re-scheduling process 0"); + tick++; }