Quellcode durchsuchen

3.831u-mm: use alloc_page and free_page to manage memory

rain vor 5 Jahren
Ursprung
Commit
b337e3c519
23 geänderte Dateien mit 514 neuen und 553 gelöschten Zeilen
  1. 2 0
      LINES
  2. 8 3
      Makefile
  3. 5 2
      README.md
  4. 36 2
      boot/boot.asm
  5. 0 153
      boot/boot.lst
  6. 0 285
      boot/loader.lst
  7. 1 1
      include/arch/x86/int.h
  8. 0 2
      include/arch/x86/paging.h
  9. 17 0
      include/kernel/init.h
  10. 117 0
      include/kernel/mm.h
  11. 27 5
      include/kernel/schk.h
  12. 5 1
      init/head.S
  13. 17 4
      init/main.c
  14. 3 1
      kernel/Makefile
  15. 5 1
      kernel/graphic.c
  16. 4 3
      kernel/int.c
  17. 171 0
      kernel/mm.c
  18. 11 0
      kernel/rlib/Makefile
  19. 30 13
      kernel/rlib/bitman.c
  20. 46 0
      kernel/rlib/bitmap.c
  21. 4 0
      kernel/trap.S
  22. 5 11
      kernel/vsprintf.c
  23. 0 66
      modules/tty.c

+ 2 - 0
LINES

@@ -16,3 +16,5 @@
 2021-7-08 2.325: 2325 lines, 26 files, rain
 2021-7-08 2.327: 2327 lines, 26 files, rain
 2021-7-11 3.455: 3455 lines, 32 files, rain
+2021-7-18 3.831: 3831 lines, 33 files, rain
+

+ 8 - 3
Makefile

@@ -1,6 +1,8 @@
 OBJS = boot/bootsect 
-KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o kernel/irq.o kernel/panic.o font.o 
-KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c kernel/vsprintf.c kernel/irq.c kernel/panic.c 
+RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c
+RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o 
+KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o font.o $(RLIB_OBJS)
+KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c $(RLIB_SRCS)
 
 all: Makefile
 	make -C tools/
@@ -8,6 +10,7 @@ all: Makefile
 	make -C init/
 	make -C modules/
 	make -C kernel/ 
+	make -C kernel/rlib 
 	make xnix.img
 	make vmxnix
 
@@ -25,7 +28,7 @@ vmxnix: $(KSRCS) $(KOBJS) Makefile
 	ld -b elf64-x86-64 $(KOBJS) -e init -T kernel.lds -o vmxnix
 
 
-xnix.img: boot/bootsect $(KSRCS) $(KOBJS) Makefile kbinary
+xnix.img: boot/bootsect $(KSRCS) $(KOBJS) Makefile kbinary 
 	dd if=/dev/zero of=xnix.img bs=1024 count=1440
 	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
 	dd if=kbinary of=xnix.img conv=notrunc bs=512 seek=2
@@ -34,6 +37,8 @@ xnix.img: boot/bootsect $(KSRCS) $(KOBJS) Makefile kbinary
 lines: 
 	wc -l `find ./ -name *.asm -o -name *.c -o -name *.h -o -name *.S -o -name *.s`
 
+files: 
+	find ./ -name *.asm -o -name *.c -o -name *.h -o -name *.S -o -name *.s | wc -l 
 
 clear: 
 	make -C boot/ clear

+ 5 - 2
README.md

@@ -47,12 +47,10 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;add handler for divide-error (always divide by zero). `do_divide_error` write in C, it pucs a message and return. 
 &nbsp;&nbsp;&nbsp;&nbsp;`divide_error` is an assembly function. it saves all registers, and call `do_divide_error`. then it recover all registers and iretq. 
 
-
 2.156u-modules:  
 &nbsp;&nbsp;&nbsp;&nbsp;now we can support colouring printf, and I think it's beautifuller than VGA (well...). 
 &nbsp;&nbsp;&nbsp;&nbsp;...and, loader loaded kernel to 0x100000 (1MB). 
 
-
 2.040u-modules:  
 &nbsp;&nbsp;&nbsp;&nbsp;now we use graphic.c to display PSF font on VGA 320x200 screen. (but it just can display upper characters, can't display some lower character)
 
@@ -66,6 +64,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;now interrupt handlers and head.S can use print now. 
 
 2.325u-modules:  
+
 &nbsp;&nbsp;&nbsp;&nbsp;now we can use 1024x768 VBE and vsprintf. this is a big change. 
 &nbsp;&nbsp;&nbsp;&nbsp;NOTE that interrupts can also work now, but only divide-error and syscall. 
 
@@ -75,3 +74,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 3.455u-int:  
 &nbsp;&nbsp;&nbsp;&nbsp;now we support all 20 traps and IRQ. 
 
+3.831u-mm:  
+&nbsp;&nbsp;&nbsp;&nbsp;we can use `alloc_page` and `free_page` to allocate/free memory. 
+&nbsp;&nbsp;&nbsp;&nbsp;and don't forget to call `mm_init` first. 
+

+ 36 - 2
boot/boot.asm

@@ -53,7 +53,7 @@ _start:
 .set_vga:
 	; get mode info
 	mov ax, 0x4f01
-	mov cx, 0x4180
+	mov cx, 0x4118
 	mov di, 0x7000
 
 	int 0x10
@@ -73,11 +73,44 @@ die:	jc die
 	mov [FB_ADDR], eax
 
 	mov ax, 0x4f02
-	mov bx, 0x4180
+	mov bx, 0x4118
 	int 0x10
 
+	; get e820 memory
+
+	xor ebx, ebx
+
+	; yeah, we used it in .set_vga. 
+	; and I think this is a good place (no one used it)
+
+	xor ax, ax
+	mov es, ax
+
+	mov di, 0x7000
+
+.get_loop:
+	mov eax, 0x0000e820
+	mov ecx, 20
+	mov edx, 0x534d4150
+
+	int 0x15
+
+	jc .get_failed
+
+	add di, 20
+
+	cmp ebx, 0
+	jne .get_loop
+
 	jmp 0x0000:0x7e00
 
+.get_failed:
+	mov si, e820_fail_msg
+	call print
+
+.die:	hlt
+	jmp .die
+
 
 print:
 	cld
@@ -290,6 +323,7 @@ read_lba:
 boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
 read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
 lamsg: db "LBA mode is avaliable. ", 0x0d, 0x0a, 0x00
+e820_fail_msg: db "panic: read E820 memory info failed. ", 0x0d, 0x0a, 0x00
 
 
 times 510 - ($ - $$) db 0x00

+ 0 - 153
boot/boot.lst

@@ -1,153 +0,0 @@
-     1                                  ; Copyright (C) 2021 Rain
-     2                                  
-     3                                  ; This file is part of XNIX. 
-     4                                  ; XNIX is free software: you can redistribute it and/or modify 
-     5                                  ; it under the terms of the GNU General Public License as published by 
-     6                                  ; the Free Software Foundation, either version 3 of the License, or 
-     7                                  ; (at your option) and later version. 
-     8                                  
-     9                                  ; XNIX is distributed in the hope that it will be useful, 
-    10                                  ; but WITHOUT ANY WARRANTY; without even the implied warranty of 
-    11                                  ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-    12                                  ; GNU General Public License for more details. 
-    13                                  
-    14                                  ; You should have received a copy of the GNU General Public License 
-    15                                  ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
-    16                                  
-    17                                  
-    18                                  
-    19                                  
-    20                                  org 0x7c00
-    21                                  
-    22                                  cyldsneed equ 10
-    23                                  
-    24                                  
-    25                                  ; yeah, it's really 0x7c00 because I don't want to use other memory. 
-    26                                  
-    27                                  BOOTDRIVER equ 0x7c00
-    28                                  
-    29                                  _start:
-    30 00000000 31C0                    	xor ax, ax
-    31                                  
-    32 00000002 8ED8                    	mov ds, ax
-    33 00000004 8EC0                    	mov es, ax
-    34                                  
-    35 00000006 8ED0                    	mov ss, ax
-    36 00000008 BC007C                  	mov sp, 0x7c00
-    37                                  
-    38                                  	; save startup driver
-    39 0000000B 8816007C                	mov [BOOTDRIVER], dl
-    40                                  
-    41                                  	; display message 'Loading System'
-    42                                  
-    43 0000000F BE[7400]                	mov si, boot_msg
-    44 00000012 E80800                  	call print
-    45                                  
-    46 00000015 E81500                  	call read_it
-    47                                  
-    48 00000018 EA007E0000              	jmp 0x0000:0x7e00
-    49                                  
-    50                                  
-    51                                  
-    52                                  print:
-    53 0000001D FC                      	cld
-    54 0000001E AC                      	lodsb
-    55                                  
-    56 0000001F 84C0                    	test al,al
-    57 00000021 7409                    	jz .ret
-    58                                  
-    59 00000023 B40E                    	mov ah, 0x0e
-    60 00000025 BB0F00                  	mov bx, 0x0f
-    61 00000028 CD10                    	int 0x10
-    62                                  
-    63 0000002A EBF1                    	jmp print
-    64                                  
-    65                                  .ret:
-    66 0000002C C3                      	ret
-    67                                  
-    68                                  
-    69                                  ; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
-    70                                  
-    71                                  read_it:
-    72                                  	; BIOS 0x13, AH = 0x02: read disk in CHS mode
-    73                                  	; AL: sectors will read
-    74                                  	; CH: cylinder
-    75                                  	; CL: sector start
-    76                                  	; DH: head
-    77                                  	; DL: driver
-    78                                  	; BX: buffer address (segment register ES)
-    79                                  
-    80                                  	; segment address
-    81 0000002D B8E007                  	mov ax, 0x07e0
-    82 00000030 8EC0                    	mov es, ax
-    83                                  
-    84 00000032 B500                    	mov ch, 0
-    85 00000034 B600                    	mov dh, 0
-    86                                  
-    87                                  	; sector 1 is MBR (this code)
-    88 00000036 B102                    	mov cl, 2
-    89                                  
-    90                                  .retry:
-    91 00000038 B402                    	mov ah, 0x02
-    92                                  
-    93                                  	; only read one sectors
-    94 0000003A B001                    	mov al, 1
-    95 0000003C BB0000                  	mov bx, 0x0000
-    96                                  
-    97 0000003F 8A16007C                	mov dl, [BOOTDRIVER]
-    98                                  	
-    99 00000043 CD13                    	int 0x13
-   100                                  
-   101 00000045 7224                    	jc read_failed
-   102                                  
-   103                                  .next_sector:
-   104 00000047 8CC0                    	mov ax, es
-   105                                  	; add 512 for memory address
-   106 00000049 83C020                  	add ax, 512 / 16
-   107 0000004C 8EC0                    	mov es, ax
-   108                                  
-   109                                  	; sector++
-   110 0000004E 80C101                  	add cl, 1
-   111                                  	; if sector > 18
-   112 00000051 80F912                  	cmp cl, 18
-   113 00000054 76E2                    	jbe .retry
-   114                                  
-   115                                  	; next head
-   116 00000056 B101                    	mov cl, 1
-   117                                  	; head++
-   118 00000058 80C601                  	add dh, 1
-   119                                  	; if head == 2 (head can only be 0 or 1)
-   120 0000005B 80FE02                  	cmp dh, 2
-   121 0000005E 72D8                    	jb .retry
-   122                                  
-   123                                  	; next cylinder
-   124 00000060 B600                    	mov dh, 0
-   125 00000062 80C501                  	add ch, 1
-   126                                  	
-   127                                  	; if cylinder == CYLINDERS_NEED
-   128 00000065 80FD0A                  	cmp ch, cyldsneed
-   129 00000068 72CE                    	jb .retry
-   130                                  
-   131 0000006A C3                      	ret
-   132                                  
-   133                                  
-   134                                  read_failed:
-   135 0000006B BE[8800]                	mov si, read_fail_msg
-   136 0000006E E8ACFF                  	call print
-   137                                  
-   138 00000071 F4                      .die:	hlt
-   139 00000072 EBFD                    	jmp .die
-   140                                  
-   141                                  
-   142 00000074 4C6F6164696E672053-     boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
-   142 0000007D 797374656D2E2E2E0D-
-   142 00000086 0A00               
-   143 00000088 70616E69633A207265-     read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
-   143 00000091 6164206469736B2066-
-   143 0000009A 61696C65642E200D0A-
-   143 000000A3 00                 
-   144                                  
-   145                                  
-   146 000000A4 00<rep 15Ah>            times 510 - ($ - $$) db 0x00
-   147 000001FE 55AA                    db 0x55, 0xaa
-   148                                  

+ 0 - 285
boot/loader.lst

@@ -1,285 +0,0 @@
-     1                                  ; Copyright (C) 2021 Rain
-     2                                  
-     3                                  ; This file is part of XNIX. 
-     4                                  ; XNIX is free software: you can redistribute it and/or modify 
-     5                                  ; it under the terms of the GNU General Public License as published by 
-     6                                  ; the Free Software Foundation, either version 3 of the License, or 
-     7                                  ; (at your option) and later version. 
-     8                                  
-     9                                  ; XNIX is distributed in the hope that it will be useful, 
-    10                                  ; but WITHOUT ANY WARRANTY; without even the implied warranty of 
-    11                                  ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-    12                                  ; GNU General Public License for more details. 
-    13                                  
-    14                                  ; You should have received a copy of the GNU General Public License 
-    15                                  ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
-    16                                  
-    17                                  
-    18                                  
-    19                                  
-    20                                  org 0x7e00
-    21                                  
-    22                                  
-    23                                  %include "boot/paging.asm"
-    24                              <1> ; Copyright (C) 2021 Rain
-    25                              <1> 
-    26                              <1> ; This file is part of XNIX. 
-    27                              <1> ; XNIX is free software: you can redistribute it and/or modify 
-    28                              <1> ; it under the terms of the GNU General Public License as published by 
-    29                              <1> ; the Free Software Foundation, either version 3 of the License, or 
-    30                              <1> ; (at your option) and later version. 
-    31                              <1> 
-    32                              <1> ; XNIX is distributed in the hope that it will be useful, 
-    33                              <1> ; but WITHOUT ANY WARRANTY; without even the implied warranty of 
-    34                              <1> ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-    35                              <1> ; GNU General Public License for more details. 
-    36                              <1> 
-    37                              <1> ; You should have received a copy of the GNU General Public License 
-    38                              <1> ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
-    39                              <1> 
-    40                              <1> 
-    41                              <1> 
-    42                              <1> 
-    43                              <1> PATT_PRESENT 	equ 0x0001
-    44                              <1> PATT_RDWR	equ 0x0002
-    45                              <1> PATT_USER	equ 0x0004
-    46                              <1> PATT_CACHEMEM	equ 0x0008
-    47                              <1> PATT_NOCACHE	equ 0x0010
-    48                              <1> PATT_ACCESSED	equ 0x0020
-    49                              <1> PATT_DIRTY	equ 0x0040
-    50                              <1> PATT_PAT	equ 0x0080
-    51                              <1> PATT_GLOBAL	equ 0x0100
-    52                              <1> 
-    53                              <1> ; 0x83 it is. 
-    54                              <1> PAGE_KERNEL	equ PATT_PRESENT + PATT_RDWR + PATT_PAT
-    55                              <1> 
-    56                              <1> ; this is linux's 007. (8-based 007)
-    57                              <1> PAGE_USER	equ PATT_PRESENT + PATT_RDWR + PATT_USER
-    58                              <1> 
-    24                                  
-    25                                  
-    26                                  _start:
-    27 00000000 31C0                    	xor ax, ax
-    28                                  
-    29 00000002 8ED8                    	mov ds, ax
-    30 00000004 8EC0                    	mov es, ax
-    31                                  
-    32 00000006 8ED0                    	mov ss, ax
-    33 00000008 BC007C                  	mov sp, 0x7c00
-    34                                  
-    35                                  	; display message 'Starting Loader...'
-    36                                  
-    37 0000000B BE[EC00]                	mov si, message
-    38 0000000E E81E00                  	call print
-    39                                  
-    40                                  .open_a20:
-    41                                  	; disable interrupts before we initial done (enable in kernel)
-    42                                  
-    43                                  	; A20 line uses to control memory higher than 1MB. we can't access mem > 1MB when it's closing. 
-    44                                  	; So we must open it before switching to protect-mode. 
-    45                                  
-    46 00000011 FA                      	cli
-    47                                  
-    48 00000012 E492                    	in al, 0x92
-    49 00000014 0C02                    	or al, 0x02
-    50 00000016 E692                    	out 0x92, al
-    51                                  
-    52                                  .to_prot:
-    53                                  	; switch to protect-mode. 
-    54                                  	
-    55 00000018 0F0116[1801]            	lgdt [gdt32_desc]
-    56                                  	
-    57                                  	; CR0 (control register 0) uses to control CPU. It's first bit PE controls Protect-mode Enable. 
-    58 0000001D 0F20C0                  	mov eax, cr0
-    59 00000020 6683C801                	or eax, 1
-    60 00000024 0F22C0                  	mov cr0, eax
-    61                                  
-    62                                  	; CPU is in protect-mode now, but segment registers didn't initialze. 
-    63                                  	
-    64                                  	; We cannot 'mov cx, xxx'
-    65                                  
-    66 00000027 66EA[3F000000]0800      	jmp dword code32:init_prot
-    67                                  
-    68                                  
-    69                                  print:
-    70 0000002F FC                      	cld
-    71 00000030 AC                      	lodsb
-    72                                  
-    73 00000031 84C0                    	test al,al
-    74 00000033 7409                    	jz .ret
-    75                                  
-    76 00000035 B40E                    	mov ah, 0x0e
-    77 00000037 BB0F00                  	mov bx, 0x0f
-    78 0000003A CD10                    	int 0x10
-    79                                  
-    80 0000003C EBF1                    	jmp print
-    81                                  
-    82                                  .ret:
-    83 0000003E C3                      	ret
-    84                                  
-    85                                  
-    86                                  bits 32
-    87                                  
-    88                                  init_prot:
-    89                                  	; we set segment selectors for segment register (CS was set by far-jmp)
-    90 0000003F 66B81000                	mov ax, data32
-    91                                  
-    92 00000043 8ED8                    	mov ds, ax
-    93 00000045 8EC0                    	mov es, ax
-    94 00000047 8EE0                    	mov fs, ax
-    95 00000049 8EE8                    	mov gs, ax
-    96                                  
-    97 0000004B 8ED0                    	mov ss, ax
-    98                                  
-    99 0000004D 66BC007C                	mov sp, 0x00007c00
-   100                                  
-   101                                  	; copy kernel to 0x100000
-   102                                  
-   103 00000051 BE00800000              	mov esi, 0x8000
-   104 00000056 BF00001000              	mov edi, 0x100000
-   105                                  
-   106 0000005B B900800000              	mov ecx, 64 * 512
-   107                                  
-   108 00000060 E883000000              	call memcpy
-   109                                  
-   110                                  	; now we switch to long-mode
-   111                                  
-   112                                  .to_long_mode:
-   113                                  	; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
-   114 00000065 31FF                    	xor edi, edi
-   115                                  	; mov edi, 0x00000000
-   116                                  
-   117 00000067 31C0                    	xor eax, eax
-   118                                  	; store for 4096 times
-   119 00000069 B900100000              	mov ecx, 4096
-   120                                  
-   121 0000006E FC                      	cld
-   122 0000006F F3AB                    	rep stosd
-   123                                  
-   124                                  .init_paging:
-   125                                  	; we set page map at 0x00000000 (a good address)
-   126                                  
-   127 00000071 C70500000000071000-     	mov dword [0x00000000], 0x00001000 | PAGE_USER
-   127 0000007A 00                 
-   128                                  ;	mov dword [0x00000004], 0x00000000
-   129                                  
-   130 0000007B C70500080000071000-     	mov dword [0x00000800], 0x00001000 | PAGE_USER
-   130 00000084 00                 
-   131                                  ;	mov dword [0x00000804], 0x00000000
-   132                                  
-   133                                  	; and page directory pointer table
-   134                                  
-   135 00000085 C70500100000072000-     	mov dword [0x00001000], 0x00002000 | PAGE_USER
-   135 0000008E 00                 
-   136                                  ;	mov dword [0x00001004], 0x00000000
-   137                                  
-   138                                  	; page directory entry (2 MB)
-   139                                  
-   140 0000008F C70500200000830000-     	mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
-   140 00000098 00                 
-   141                                  ;	mov dword [0x00002004], 0x00000000
-   142                                  
-   143 00000099 C70508200000830020-     	mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
-   143 000000A2 00                 
-   144                                  ;	mov dword [0x0000200c], 0x00000000
-   145                                  
-   146                                  .enable_long_mode:
-   147 000000A3 0F0115[36010000]        	lgdt [gdt64_desc]
-   148                                  
-   149 000000AA 66B81000                	mov ax, data64
-   150                                  
-   151 000000AE 8ED8                    	mov ds, ax
-   152 000000B0 8EC0                    	mov es, ax
-   153 000000B2 8EE0                    	mov fs, ax
-   154 000000B4 8EE8                    	mov gs, ax
-   155 000000B6 8ED0                    	mov ss, ax
-   156                                  
-   157                                  	; open PAE (page map level 3)
-   158 000000B8 0F20E0                  	mov eax, cr4
-   159 000000BB 0FBAE805                	bts eax, 5
-   160 000000BF 0F22E0                  	mov cr4, eax
-   161                                  
-   162                                  	; set Page Table Base Register
-   163                                  
-   164                                  	; mov eax, 0x00000000
-   165 000000C2 31C0                    	xor eax, eax
-   166 000000C4 0F22D8                  	mov cr3, eax
-   167                                  
-   168                                  	; enable long mode (must before paging)
-   169                                  
-   170 000000C7 B9800000C0              	mov ecx, 0xc0000080
-   171 000000CC 0F32                    	rdmsr
-   172                                  
-   173 000000CE 0FBAE808                	bts eax, 8
-   174 000000D2 0F30                    	wrmsr
-   175                                  
-   176                                  	; enable paging
-   177                                  
-   178 000000D4 0F20C0                  	mov eax, cr0
-   179                                  
-   180                                  	; PE again
-   181 000000D7 83C801                  	or eax, 1
-   182 000000DA 0FBAE81F                	bts eax, 31
-   183                                  	
-   184 000000DE 0F22C0                  	mov cr0, eax
-   185                                  
-   186                                  	; okay! now set segment registers and init long mode
-   187                                  
-   188 000000E1 EA000010000800          	jmp code64:0x100000
-   189                                  
-   190                                  memcpy:
-   191 000000E8 FC                      	cld
-   192 000000E9 F3A4                    	rep movsb
-   193                                  
-   194 000000EB C3                      	ret
-   195                                  
-   196                                  
-   197                                  
-   198 000000EC 5374617274696E6720-     message: db "Starting Loader", 0x0d, 0x0a, 0x00
-   198 000000F5 4C6F616465720D0A00 
-   199                                  
-   200                                  
-   201 000000FE ????                    alignb 16
-   202                                  
-   203                                  gdt32:
-   204 00000100 0000000000000000        	dd 0x00000000, 0x00000000
-   205                                  
-   206                                  gdt32_code:
-   207 00000108 FFFF0000009ACF00        	dd 0x0000ffff, 0x00cf9a00
-   208                                  
-   209                                  gdt32_data:
-   210 00000110 FFFF00000092CF00        	dd 0x0000ffff, 0x00cf9200
-   211                                  
-   212                                  gdt32_len equ $ - gdt32
-   213                                  
-   214                                  gdt32_desc:
-   215 00000118 1700                    	dw gdt32_len - 1
-   216 0000011A [00010000]              	dd gdt32
-   217                                  
-   218                                  code32 equ gdt32_code - gdt32
-   219                                  data32 equ gdt32_data - gdt32
-   220                                  
-   221                                  
-   222                                  
-   223                                  gdt64:
-   224 0000011E 0000000000000000        	dq 0x0000000000000000
-   225                                  
-   226                                  gdt64_code:
-   227 00000126 0000000000982000        	dq 0x0020980000000000
-   228                                  
-   229                                  gdt64_data:
-   230 0000012E 0000000000920000        	dq 0x0000920000000000
-   231                                  
-   232                                  gdt64_len equ $ - gdt64
-   233                                  
-   234                                  gdt64_desc:
-   235 00000136 1700                    	dw gdt64_len - 1
-   236 00000138 [1E010000]              	dd gdt64
-   237                                  
-   238                                  code64 equ gdt64_code - gdt64
-   239                                  data64 equ gdt64_data - gdt64
-   240                                  
-   241                                  
-   242 0000013C 00<rep C2h>             times 510 - ($ - $$) db 0x00
-   243 000001FE 55AA                    db 0x55, 0xaa
-   244                                  

+ 1 - 1
include/arch/x86/int.h

@@ -44,7 +44,7 @@ struct idt_entry {
 /* 16B per entry, 256 entries */
 extern struct idt_entry idt_table[];
 
-int init_int(void);
+int int_init(void);
 void set_gate(struct idt_entry *, char, char, void *);
 void syscall(void);
 

+ 0 - 2
include/arch/x86/paging.h

@@ -57,6 +57,4 @@
 #define PDPT_ADDR 0x71000
 #define PTE_ADDR  0x72000
 
-#define PAGE_SIZE 0x200000
-
 #endif

+ 17 - 0
include/kernel/init.h

@@ -27,6 +27,23 @@
 #ifndef _INIT_H
 #define _INIT_H
 
+#define DEFAULT_DEBUG_LEVEL 	3
+#define DEV_DEBUG_LEVEL		2
+#define TEST_DEBUG_LEVEL	1
+
+
+/* fastest, but no syslog and schk */
+
+// #define DEBUG_LEVEL 		DEFAULT_DEBUG_LEVEL
+
+/* standard */
+
+// #define DEBUG_LEVEL		DEV_DEBUG_LEVEL
+
+/* slowest: schk will display function info in this mode */
+#define DEBUG_LEVEL		TEST_DEBUG_LEVEL
+
+
 #include <kernel/types.h>
 #include <kernel/errno.h>
 

+ 117 - 0
include/kernel/mm.h

@@ -0,0 +1,117 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#ifndef _MM_H
+#define _MM_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+#include <lib/bitman.h>
+
+
+extern void *_text, *_etext;
+extern void *_data, *_edata;
+extern void *_rodata, *_erodata;
+extern void *_bss, *_ebss;
+extern void *_end;
+
+void mm_init(void);
+void *alloc_page(void);
+void free_page(void *);
+
+
+#define E820_RAM 1
+#define E820_ROM 2
+
+/* we doesn't need other types, even they are present */
+
+/* if >= E820_INV, break */
+#define E820_INV 5
+
+
+#define E820_ADDR ((struct e820_entry *) 0x7000)
+
+struct e820_entry {
+	__uint64_t addr;
+	__uint64_t len;
+	__uint32_t type;
+} __attribute__ ((packed));
+
+
+/* a memory zone that describes some pages */
+
+struct zone {
+	/* in e820 */
+	__uint32_t type;
+	__uint32_t total_pages;
+
+	__uint32_t start_addr;
+	__uint32_t end_addr;
+
+	__uint32_t size;
+
+	__uint32_t using;
+};
+
+
+struct sysmm {
+	__uint64_t total_ram;
+	__uint64_t total_pages;
+
+	__uint32_t e820_count;
+	struct e820_entry *e820;
+
+	__uint32_t total_zones;
+	struct zone *zones;
+
+	__uint8_t *bitmap;
+};
+
+extern struct sysmm *mm;
+
+#define PAGE_4K_SHIFT 12
+#define PAGE_2M_SHIFT 21
+
+#define PAGE_SHIFT PAGE_2M_SHIFT
+
+#define PAGE_4K_SIZE (1ul << 12)
+#define PAGE_2M_SIZE (1ul << 21)
+
+#define PAGE_SIZE PAGE_2M_SIZE
+
+#define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1))
+#define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1))
+
+#define PAGE_MASK PAGE_2M_MASK
+
+#define PAGE_4K_ALIGN(a) ((a + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
+#define PAGE_2M_ALIGN(a) ((a + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
+
+#define PAGE_ALIGN(a) PAGE_2M_ALIGN(a)
+
+#endif
+

+ 27 - 5
include/kernel/schk.h

@@ -27,6 +27,8 @@
 #ifndef _SCHK_H
 #define _SCHK_H
 
+#include <kernel/init.h>
+
 #include <kernel/types.h>
 #include <kernel/errno.h>
 #include <kernel/panic.h>
@@ -37,8 +39,9 @@
 
 void __stack_chk_fail(void);
 
+#if DEBUG_LEVEL == TEST_DEBUG_LEVEL
 
-#define schk_init() 					\
+#define schk_init()					\
 	__uint64_t old_sp = get_rsp();			\
 	syslog("  entering function ");			\
 	syslog(__FUNCTION__);				\
@@ -51,15 +54,34 @@ void __stack_chk_fail(void);
 	else if (old_sp < 0x5000)			\
 		syslog("#3#WARNING: SP < 0x5000 now");	\
 	else if (old_sp < 0x6000)			\
-		syslog("#4#WARNING: SP < 0x6000 now");
-	
-	
+		syslog("#4#WARNING: SP < 0x6000 now");	\
+
+#elif DEBUG_LEVEL == DEV_DEBUG_LEVEL
+
+#define schk_init() __uint64_t old_sp = get_rsp();
+
+#elif DEBUG_LEVEL == DEFAULT_DEBUG_LEVEL
 
+#define schk_init()
+
+#else
+
+#error "unknow debug level (in init.h)"
+
+#endif
+
+
+#if DEBUG_LEVEL == DEFAULT_DEBUG_LEVEL
+
+#define schk_leave()
+
+#else
 
 #define schk_leave()			\
 	if (old_sp != get_rsp())	\
-		__stack_chk_fail();
+		__stack_chk_fail()
 
+#endif
 
 #endif
 

+ 5 - 1
init/head.S

@@ -49,7 +49,11 @@
 .globl _start
 
 _start:
-	movq $0x0000000000007c00, %rsp
+	leaq _end(%rip), %rax
+	addq $32768, %rax
+
+	movq %rax, %rsp
+
 	/* setup IDT */
 	lidt idt_desc(%rip)
 

+ 17 - 4
init/main.c

@@ -31,6 +31,8 @@
 #include <kernel/vsprintf.h>
 #include <kernel/schk.h>
 
+#include <kernel/mm.h>
+
 #include <lib/string.h>
 
 #include <arch/io.h>
@@ -51,13 +53,24 @@ struct bootarg *bootarg = (struct bootarg *) 0x7c00;
  * */
 
 int init(void) {
-	init_int();
+	print(0x00ffffff, 0x00000000, "XNIX kernel is starting...\n");
+
+	int_init();
+	mm_init();
+
+	int *i = alloc_page();
+	int *p = alloc_page();
+
+	printk(0x00ffffff, 0x00000000, "alloc_page: %q\n", i);
+	printk(0x00ffffff, 0x00000000, "alloc_page: %q\n", p);
 
-	print(0x00ffffff, 0x00, "XNIX kernel is starting...\n");
+	free_page(i);
+	i = alloc_page();
 
-	printk(0x00ffffff, 0x00, "hello, can you see this? %d, and this? %q\n", 123, 0x0123456789abcdef);
+	printk(0x00ffffff, 0x00000000, "free i and alloc_page: %q\n", i);
 
-	sti();
+	free_page(i);
+	free_page(p);
 
 	return 0;
 }

+ 3 - 1
kernel/Makefile

@@ -1,5 +1,6 @@
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
-all: int.o trap.o graphic.o vsprintf.o irq.o panic.o 
+
+all: int.o trap.o graphic.o vsprintf.o irq.o panic.o mm.o 
 
 
 %.o: %.S
@@ -10,5 +11,6 @@ all: int.o trap.o graphic.o vsprintf.o irq.o panic.o
 	gcc -c $*.c -o $*.o $(CFLAGS)
 
 clear:
+	make -C rlib/ clear
 	rm *.o *.s
 

+ 5 - 1
kernel/graphic.c

@@ -172,8 +172,12 @@ void putc(char c, __uint32_t fg, __uint32_t bg) {
 void print(__uint32_t fg, __uint32_t bg, char *s) {
 	schk_init();
 
-	for (; *s; s++) 
+	for (; *s; s++) {
+		if (*s == '\n') 
+			putc(' ', fg, bg);
+
 		putc(*s, fg, bg);
+	}
 
 	schk_leave();
 

+ 4 - 3
kernel/int.c

@@ -41,7 +41,7 @@ build_irq(0x20);
 build_irq(0x21);
 
 
-int init_int(void) {
+int int_init(void) {
 	schk_init();
 
 	/* init PIC */
@@ -135,7 +135,9 @@ void die(char *s, __uint64_t *rsp, char nr) {
 	printk(0x00ff0000, 0x00000000, "int #%b: %s. \n", nr, s);
 	printk(0x00ff0000, 0x00000000, "rsp: %q\n", rsp);
 
-	for (;;);
+	for (;;) hlt();
+
+	return;
 }
 
 
@@ -359,4 +361,3 @@ void do_virtualization(__uint64_t *rsp) {
 	return;
 }
 
-

+ 171 - 0
kernel/mm.c

@@ -0,0 +1,171 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/graphic.h>
+#include <kernel/int.h>
+#include <kernel/mm.h>
+
+#include <lib/bitmap.h>
+#include <lib/string.h>
+
+#include <arch/x86/int.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+
+
+struct sysmm *mm;
+
+
+void mm_init(void) {
+	mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
+	
+	struct e820_entry *e820 = E820_ADDR;
+	struct zone *zones = (struct zone *) (((char *) mm) + sizeof(struct sysmm));
+
+	int i;
+	__uint64_t total_ram = 0;
+
+	struct e820_entry *e = e820;
+
+	memset(mm, 0x00, sizeof(struct sysmm));
+
+	for (i = 0; i < 32; i++) {
+		if (!e->type || e->type >= E820_INV) 
+			break;
+
+		printk(0x00ffffff, 0x00000000, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
+
+		/* create a `struct zone` for this */
+
+#define z (zones + mm->total_zones)
+
+		z->type = e->type;
+		z->total_pages = e->len >> PAGE_SHIFT;
+
+		z->start_addr = e->addr;
+		z->end_addr = e->addr + e->len;
+
+		z->size = e->len;
+		z->using = 0;
+
+		mm->total_zones++;
+
+		if (e->type == E820_RAM) 
+			total_ram += e->len;
+
+		mm->total_pages += e->len >> PAGE_SHIFT;
+		e++;
+	}
+
+	printk(0x00ffffff, 0x00000000, "total usable ram: %d MB. \n", total_ram >> 20);
+	printk(0x00ffffff, 0x00000000, "total pages: %d (includes unusable pages) = %d MB\n", mm->total_pages, mm->total_pages << PAGE_SHIFT >> 20);
+
+	printk(0x00ffffff, 0x00000000, "total zones: %d\n", mm->total_zones);
+
+	/* setup page bitmap */
+
+	/* first we should allocate some memory for it */
+	/* at end of zones */
+
+	mm->bitmap = (__uint8_t *) (zones + mm->total_zones);
+
+	/* set all bits. if this bit points to a RAM page, clear it */
+
+	/* it always not bad. */
+#define BYTESNEED ((mm->total_pages >> 3) + 1)
+
+	memset(mm->bitmap, 0xff, BYTESNEED);
+
+	e = e820;
+
+	for (i = 0; i < mm->total_zones; i++) {
+
+#undef z
+#define z (zones + i)
+
+		if (z->type == E820_RAM) {
+			/* clear bits in bitmap */
+			int j;
+			__uint64_t start_page = PAGE_ALIGN(z->start_addr) >> PAGE_SHIFT;
+
+			for (j = start_page; j < start_page + z->total_pages; j++) 
+				bitmap_set(mm->bitmap, j, 0);
+		}
+	}
+
+	/* 
+	 * the first and second page is for kernel, 
+	 * and the 3rd, 4th, 5th are for frame buffer (they setup in boot/loader.asm). 
+	 * */
+
+	/* 0MB */
+	bitmap_set(mm->bitmap, 0, 1);
+
+	/* 2MB */
+	bitmap_set(mm->bitmap, 1, 1);
+
+	/* 4MB (FB_ADDR) */
+	bitmap_set(mm->bitmap, 2, 1);
+	/* 6MB */
+	bitmap_set(mm->bitmap, 3, 1);
+	/* 8MB */
+	bitmap_set(mm->bitmap, 4, 1);
+
+	/* so next free page is 10MB (0xa00000) */
+
+	return;
+
+#undef z
+}
+
+
+void *alloc_page(void) {
+	__uint64_t i;
+
+	for (i = 0; bitmap_read(mm->bitmap, i); i++) {
+		if (i > mm->total_pages) 
+			return NULL;
+	}
+
+	bitmap_set(mm->bitmap, i, 1);
+
+	return (void *) (i << PAGE_SHIFT);
+}
+
+
+void free_page(void *p) {
+	__uint64_t i;
+
+	i = (__uint64_t) p >> PAGE_SHIFT;
+
+	bitmap_set(mm->bitmap, i, 0);
+
+	return;
+}

+ 11 - 0
kernel/rlib/Makefile

@@ -0,0 +1,11 @@
+C_FLAGS = -mcmodel=large -m64 -fno-builtin -nostdlib -I ../../include/ -Wall -Werror -O3 -g 
+
+all: bitmap.c bitman.c bitmap.o bitman.o 
+
+%.o: %.c
+	gcc -c $*.c $(C_FLAGS)
+
+
+clear:
+	rm *.o
+

+ 30 - 13
kernel/kernel.c → kernel/rlib/bitman.c

@@ -24,23 +24,40 @@
 
 
 
-#include <kernel/init.h>
-#include <kernel/errno.h>
-#include <kernel/types.h>
+#include <lib/rlib.h>
 
-#include <kernel/graphic.h>
-#include <kernel/int.h>
-
-#include <arch/x86/int.h>
-#include <arch/io.h>
-#include <arch/sys.h>
 
+/*
+	find a new bit in bm->bitmap. 
 
+	STEPS: 
+		1. save free position. 
+		2. find a new free position. 
+		3. save new free position. 
+*/
+unsigned int bitman_alloc(struct bitmap_manager *bm) {
+	unsigned int p = bm->next_free;
+	bitmap_set(bm->bitmap, bm->next_free, 1);
 
-void panic(char *s) {
-	printk(0x00ff0000, 0x00000000, "kernel panic: %s\n", s);
-	printk(0x00ff0000, 0x00000000, "for more debug information, see syslog 0x90000\n");
+	for (; bitmap_read(bm->bitmap, bm->next_free) == 1; bm->next_free++);
 
-	for (;;) hlt();
+	return p;
 }
 
+
+/*
+	free a bit. 
+
+	STEPS: 
+		1. free bit pos. 
+		2. if pos is lowwer than bm->next_free
+			2a. set bm->next_free = pos. 
+*/
+void bitman_free(struct bitmap_manager *bm, unsigned int pos) {
+	bitmap_set(bm->bitmap, pos, 0);
+
+	if (pos < bm->next_free) {
+		bm->next_free = pos;
+	}
+	return;
+}

+ 46 - 0
kernel/rlib/bitmap.c

@@ -0,0 +1,46 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of Cunix. */
+
+/* 
+  Cunix is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  Cunix is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with Cunix. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <kernel/types.h>
+
+void bitmap_set(unsigned char *bitmap, unsigned int pos, unsigned char value) {
+	unsigned char mask = 0x80 >> (pos & 0x7);
+
+	if (value) 
+		bitmap[pos >> 3] |= mask;
+	
+	else 
+		bitmap[pos >> 3] &= ~mask;
+	
+	return;
+}
+
+char bitmap_read(unsigned char *bitmap, unsigned int pos) {
+	unsigned char mask = 0x80 >> (pos & 0x7);
+
+	return (mask & bitmap[pos >> 3]) == mask ? 1 : 0;
+}
+

+ 4 - 0
kernel/trap.S

@@ -43,6 +43,7 @@ divide_error:
 	leaq do_divide_error(%rip), %rax
 
 no_error_code:
+	cli
 	/* and save other registers seriously now */
 	pushq %rcx
 	pushq %rdx
@@ -118,6 +119,8 @@ restore_all:
 
 	popq %rax
 
+	sti
+
 	/* then say goodbye to this interrupt */
 	iretq
 
@@ -185,6 +188,7 @@ double_fault:
 
 
 error_code:
+	cli
 
 	/* save registers, and get that error-code */
 	/* now error-code is on the top of stack */

+ 5 - 11
kernel/vsprintf.c

@@ -54,7 +54,6 @@
  * 		%l: a 64-bit 10-based signed   long number. 
  *	 	%u: a 64-bit 10-based unsigned long number. 
  * 		%x: a 64-bit 16-based unsigned long number. 
- * 		%o: a 64-bit  8-based unsigned long number. 
  *
  *	OTHERS: 
  *	8 :	%b: a 16-based unsigned char  number. 
@@ -113,9 +112,6 @@ int vsprintf(char *buf, char *fmt, va_list args) {
 			else if (c == 'x') 
 				ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 0);
 
-			else if (c == 'o') 
-				ri += itos(buf + ri, va_arg(args, unsigned long), 8, 0, 0);
-
 			else if (c == 'b') 
 				ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1);
 
@@ -147,7 +143,7 @@ int itos(char *buffer, unsigned long n, int base, int sign, int withzero) {
 
 	static char digits[] = "0123456789abcdef";
 
-	char tbuf[32];
+	char tbuf[16] = {0};
 	int i, neg, j;
 
 	__uint64_t x;
@@ -168,13 +164,11 @@ int itos(char *buffer, unsigned long n, int base, int sign, int withzero) {
 		tbuf[i++] = digits[x % base];
 	} while (x /= base);
 
-	if (withzero && (base == 8 || base == 16)) {
-		/* fill zero */
-		int i;
-
-		for (i = 0; i < 32; i++) 
-			if (!tbuf[i]) 
+	if (withzero && base == 16) {
+		for (i = 0; i < 16; i++) {
+			if (!tbuf[i])
 				tbuf[i] = '0';
+		}
 	}
 
 	if (neg) 

+ 0 - 66
modules/tty.c

@@ -1,66 +0,0 @@
-/* Copyright (C) 2021 Rain */
-
-/* This file is part of XNIX. */
-
-/* 
-  XNIX is free software: you can redistribute it and/or modify 
-  it under the terms of the GNU General Public License as published by 
-  the Free Software Foundation, either version 3 of the License, or 
-  (at your option) and later version. 
-*/
-
-/*
-  XNIX is distributed in the hope that it will be useful, 
-  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-  GNU General Public License for more details. 
-*/
-
-/*
-  You should have received a copy of the GNU General Public License 
-   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
-*/
-
-
-
-#include <kernel/init.h>
-#include <kernel/errno.h>
-#include <kernel/types.h>
-
-#include <modules/tty.h>
-
-#include <arch/io.h>
-#include <arch/sys.h>
-#include <arch/x86/int.h>
-
-
-
-/* 
- * we supports PSF format in XNIX. 
- * in linux, PSF fonts are in /usr/share/kbd/consolefonts, and they are always compressed. 
- * */
-
-
-#define PSF_MAGIC 0x864ab572
-
-struct psf_header {
-	__uint32_t magic;
-
-	/* zero */
-	__uint32_t version;
-
-	__uint32_t header_size;
-
-	/* zero if there's no unicode table */
-	__uint32_t flags;
-
-	/* in psf, a font calls glyph */
-	__uint32_t glyphs_count;
-
-	/* size of a glyph */
-	__uint32_t glyph_size;
-
-	__uint32_t height;
-	__uint32_t width;
-};
-