ソースを参照

4.678u-mm: using kmalloc and kfree to manage memory, and map page to page-table when alloc_page

rain 5 年 前
親
コミット
7bf5b7419b
22 ファイル変更、946 行追加、178 行削除
  1. 2 1
      LINES
  2. 2 2
      Makefile
  3. 2 0
      README.md
  4. 2 2
      boot/boot.asm
  5. 2 3
      include/arch/x86/int.h
  6. 3 6
      include/arch/x86/paging.h
  7. 85 0
      include/arch/x86/tss.h
  8. 2 0
      include/kernel/graphic.h
  9. 42 2
      include/kernel/int.h
  10. 26 0
      include/kernel/mm.h
  11. 175 0
      include/kernel/sched.h
  12. 2 2
      include/lib/string.h
  13. 41 3
      init/head.S
  14. 20 2
      init/main.c
  15. 1 1
      kernel/Makefile
  16. 13 0
      kernel/graphic.c
  17. 35 21
      kernel/int.c
  18. 2 17
      kernel/irq.c
  19. 166 7
      kernel/mm.c
  20. 140 0
      kernel/sched.c
  21. 64 0
      kernel/timer.S
  22. 119 109
      kernel/trap.S

+ 2 - 1
LINES

@@ -18,4 +18,5 @@
 2021-7-11 3.455: 3455 lines, 32 files, rain
 2021-7-18 3.831: 3831 lines, 33 files, rain
 2021-7-18 3.913: 3913 lines, 35 files, rain
-
+2021-7-21 4.618: 4618 lines, 39 files, rain
+ 

+ 2 - 2
Makefile

@@ -1,8 +1,8 @@
 OBJS = boot/bootsect 
 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 kernel/pci.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 kernel/pci.c $(RLIB_SRCS)
+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 kernel/pci.o kernel/timer.o kernel/sched.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 kernel/pci.c kernel/timer.S kernel/sched.c $(RLIB_SRCS)
 
 all: Makefile
 	make -C tools/

+ 2 - 0
README.md

@@ -81,3 +81,5 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 3.913u-pci:  
 &nbsp;&nbsp;&nbsp;&nbsp;I only wrote one function that called `pci_read_config`, and I think PCI is only for this (read device's config). 
 
+4.678u-mm:  
+&nbsp;&nbsp;&nbsp;&nbsp;setup TSS and memory pool. now we can use `kmalloc()` and `kfree()` to allocate memory, not `alloc_page()`. 

+ 2 - 2
boot/boot.asm

@@ -53,7 +53,7 @@ _start:
 .set_vga:
 	; get mode info
 	mov ax, 0x4f01
-	mov cx, 0x4118
+	mov cx, 0x4180
 	mov di, 0x7000
 
 	int 0x10
@@ -73,7 +73,7 @@ die:	jc die
 	mov [FB_ADDR], eax
 
 	mov ax, 0x4f02
-	mov bx, 0x4118
+	mov bx, 0x4180
 	int 0x10
 
 	; get e820 memory

+ 2 - 3
include/arch/x86/int.h

@@ -23,9 +23,8 @@
 
 
 
-#ifndef INCLUDED_INT_H
-#define INCLUDED_INT_H
-
+#ifndef _INT_H
+#define _INT_H
 
 
 struct idt_entry {

+ 3 - 6
include/arch/x86/paging.h

@@ -23,8 +23,8 @@
 
 
 
-#ifndef INCLUDED_PAGING_H
-#define INCLUDED_PAING_H
+#ifndef _PAGING_H
+#define _PAGING_H
 
 
 #define PAGE_PRESENT  0x0001
@@ -52,9 +52,6 @@
 /* 0x07 */
 #define PATT_USER   (PAGE_PRESENT | PAGE_RDWR | PAGE_USER)
 
-
-#define PML4_ADDR 0x70000
-#define PDPT_ADDR 0x71000
-#define PTE_ADDR  0x72000
+#define PAGE_TABLE_BASE (0x0000000000000000)
 
 #endif

+ 85 - 0
include/arch/x86/tss.h

@@ -0,0 +1,85 @@
+/* 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 _TSS_H
+#define _TSS_H
+
+
+/* init/head.S */
+extern __uint64_t *gdt_table[];
+
+
+struct tss_entry {
+	__uint64_t low;
+	__uint64_t high;
+};
+
+
+
+extern void *stack_start;
+
+
+struct tss {
+	__uint32_t rsvd0;
+	__uint64_t rsp0;
+	__uint64_t rsp1;
+	__uint64_t rsp2;
+	
+	__uint64_t rsvd1;
+
+	__uint64_t ist1, ist2, ist3, ist4;
+	__uint64_t ist5, ist6, ist7;
+
+	__uint64_t rsvd2;
+
+	__uint16_t rsvd3;
+
+	__uint16_t iomap_base;
+} __attribute__ ((packed));
+
+
+#define INIT_TSS {	\
+	.rsvd0 = 0, 	\
+	.rsp0 = 0,	\
+	.rsp1 = 0,	\
+	.rsp2 = 0,	\
+	.rsvd1 = 0, 	\
+	.ist1 = 0, 	\
+	.ist2 = 0, 	\
+	.ist3 = 0,	\
+	.ist4 = 0, 	\
+	.ist5 = 0, 	\
+	.ist6 = 0, 	\
+	.ist7 = 0, 	\
+	.rsvd2 = 0, 	\
+	.rsvd3 = 0,	\
+	.iomap_base = 0	\
+}
+
+
+struct tss init_tss = INIT_TSS;
+
+
+#endif

+ 2 - 0
include/kernel/graphic.h

@@ -30,6 +30,8 @@
 
 #include <kernel/types.h>
 
+#define TAB_SIZE  8
+
 
 void putblock(__uint32_t, int, int, int, int);
 void putc(char, __uint32_t, __uint32_t);

+ 42 - 2
include/kernel/int.h

@@ -23,9 +23,10 @@
 
 
 
+#ifndef _KINT_H
+#define _KINT_H
 
-#ifndef _INT_H
-#define _INT_H
+#ifndef __ASM__
 
 #include <kernel/types.h>
 #include <kernel/errno.h>
@@ -61,12 +62,17 @@ void virtualization(void);
 void register_irq(int, void *, void *);
 void unregister_irq(int);
 
+/* kernel/timer.S */
 void do_irq(__uint64_t *, __uint64_t);
 
 void do_timer(void);
 void do_keyboard(void);
 
 
+/* kernel/timer.S */
+extern __uint64_t jiffies;
+
+
 #define IRQ_SAVE_ALL()		\
 	"pushq %rax;\n\t"	\
 	"pushq %rcx;\n\t"	\
@@ -107,6 +113,7 @@ void do_keyboard(void);
 	__asm__ (				\
 		".globl irq"#nr"_interrupt\n\t;"\
 		"irq"#nr"_interrupt:	\n\t"	\
+		"cli\n\t"			\
 		IRQ_SAVE_ALL()			\
 		/* arguments for do_irq */	\
 		"movq %rsp, %rdi\n\t"		\
@@ -126,6 +133,39 @@ struct irq_entry {
 
 extern struct irq_entry irq_table[MAX_IRQ_NR];
 
+#endif
+
+/* offsets value in stack */
+
+#define GS 	0
+#define FS 	8
+#define ES 	16
+#define DS 	24
+#define R15 	32
+#define R14 	40
+#define R13 	48
+#define R12 	56
+#define R11 	64
+#define R10 	72
+#define R9 	80
+#define R8 	88
+#define RDI	96
+#define RSI 	104
+#define RBP 	112
+#define RDX 	120
+#define RCX 	128
+#define RAX 	136
+
+#define HANDLER 144
+#define ERRCODE 152
+
+#define RIP 	160
+#define RFLAGS 	168
+
+/* maybe, maybe, well... */
+#define RSP 	176
+#define SS 	184
+
 
 #endif
 

+ 26 - 0
include/kernel/mm.h

@@ -113,5 +113,31 @@ extern struct sysmm *mm;
 
 #define PAGE_ALIGN(a) PAGE_2M_ALIGN(a)
 
+
+/* slab-like memory management */
+
+struct mempool {
+	__uint64_t size;
+	__uint64_t used;
+
+	__uint64_t entries;
+
+	__uint8_t *bitmap;
+
+	void *page;
+
+	struct mempool *next;
+};
+
+#define POOL_ENTRY_SIZE  64
+#define POOL_ENTRY_SHIFT 6ul
+
+extern struct mempool pool;
+
+void *kmalloc(struct mempool *, __uint64_t);
+void kfree(struct mempool *, void *, __uint64_t);
+void *mapping(void *, __uint64_t);
+
+
 #endif
 

+ 175 - 0
include/kernel/sched.h

@@ -0,0 +1,175 @@
+/* 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 _SCHED_H
+#define _SCHED_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+#include <arch/io.h>
+#include <arch/sys.h>
+
+
+
+
+/* 
+ * switch_to(next): switch to task next. 
+ *
+ * STEPS: 
+ * 	save that registers that will use. 
+ * 	call __do
+ *	do: 	set data segments. 
+ *		return;
+ *
+ *	return to next task. 
+ *
+ * NOTES: 
+ * 	we don't need to restore / save registers, because 
+ * 	tasks restored them. 
+ *
+ * 	this function may called from an interrupt-handler 
+ * 	(timer IRQ), so we must sti first, else tasks won't 
+ * 	switch again. 
+ *
+ * 	__do maybe changes some registers, like %rbp 
+ * 	(maybe doesn't), so we also should save/restore them. 
+ * */
+
+#define switch_to(next) 			\
+	__asm__ __volatile__ (			\
+		"cli\n\t"			\
+		"pushq %%rbp\n\t"		\
+		"pushq %%rax\n\t"		\
+		"movq %%rsp, %0\n\t"		\
+		"movq %2, %%rsp\n\t"		\
+		"leaq goback(%%rip), %%rax\n\t"	\
+	/* fake return address */		\
+		"pushq %3\n\t"			\
+		"jmp __do\n\t"			\
+		"__do:\n\t"			\
+		"movw %4, %%ds\n\t"		\
+		"movw %4, %%es\n\t"		\
+		"movw %4, %%fs\n\t"		\
+		"movw %4, %%gs\n\t"		\
+		"sti\n\t"			\
+		"retq\n\t"			\
+		"goback:\n\t"			\
+		"popq %%rax\n\t"		\
+		"popq %%rbp\n\t"		\
+		: "=m" (current->rsp), 		\
+		  "=m" (current->rip)		\
+		: "m" ((next)->rsp), 		\
+		  "m" ((next)->rip), 		\
+		  "r" ((next)->dataseg)		\
+	)
+
+
+/* standard run time for user tasks */
+/* 1 jiffy = 10ms */
+
+#define USER_STD_TIME 1
+
+struct task {
+
+	/* died */
+#define STATE_ZOMBIE		0
+
+	/* it's running on cpu */
+#define STATE_RUNNING 		1
+
+	/* waiting, but unlike STATE_UNINTERRUPTIBLE, you can wake_up() */
+#define STATE_INTERRUPTIBLE 	2
+
+	/* waiting, and can't wake_up() */
+#define STATE_UNINTERRUPTIABLE	3
+
+	/* you can schedule this task any time */
+#define STATE_READY		4
+
+	__uint32_t state;
+
+	/* total run time in kernel space (jiffies) */
+	__uint32_t ktime;
+
+	/* total run time in user space (0 in kernel tasks) */
+	__uint32_t utime;
+
+	/* 
+	 * 0 - 16 
+	 * 8 is the default priorty for all user tasks,  
+	 * 0 is for all kernel tasks. 
+	 * */
+
+	__uint32_t priority;
+
+	/* signal (a small bitmap) */
+	__uint32_t signal;
+
+	/* mask of signal (can be set by signal() or sigaction(), but SIGKILL) */
+	__uint32_t signal_mask;
+
+	int exit_code;
+
+#define INIT_PID 0
+
+	/* pid and his father's pid */
+	__uint32_t pid, father;
+
+	/* point to its tss */
+	struct tss *tss;
+
+	__uint64_t rsp0;
+	__uint64_t rsp;
+	__uint64_t rip;
+
+	__uint64_t cr3;
+
+	__uint16_t dataseg, codeseg;
+
+	struct task *next;
+};
+
+
+extern __uint32_t next_pid;
+
+extern struct task init_task;
+
+/* unlike linux we use a pointer to save current task */  
+extern struct task *current;
+
+extern struct task *task_queues[16];
+
+
+int task_init(void);
+void set_tss(struct tss *, __uint64_t);
+
+
+#define ltr(nr) __asm__ __volatile__ ("ltr %%ax" :: "a" (nr))
+
+
+#endif
+

+ 2 - 2
include/lib/string.h

@@ -33,7 +33,7 @@
 
 /* copy src to dest for n bytes */
 #define memcpy(dest, src, n) ({ \
-	__asm__ ( \
+	__asm__ __volatile__ ( \
 		"cld\n\t" \
 		"rep movsb\n\t" \
 		:: "c" (n), "S" (src), "D" (dest) \
@@ -44,7 +44,7 @@
 
 /* set n to src for n bytes */
 #define memset(dest, c, n) ({ \
-	__asm__ ( \
+	__asm__ __volatile__ ( \
 		"cld\n\t" \
 		"rep stosb\n\t" \
 		:: "c" (n), "D" (dest), "a" (c) \

+ 41 - 3
init/head.S

@@ -49,10 +49,12 @@
 .globl _start
 
 _start:
-	leaq _end(%rip), %rax
-	addq $32768, %rax
+	/* stack_start is the end address of the first page */
 
-	movq %rax, %rsp
+	movq $0x200000, %rsp
+
+	/* setup GDT */
+	lgdt gdt_desc(%rip)
 
 	/* setup IDT */
 	lidt idt_desc(%rip)
@@ -210,8 +212,38 @@ default_int_handler:
 	
 	
 .data
+
 .align 16
 
+.globl gdt_table
+
+gdt_table:
+	/* +0: null descriptor */
+	.quad 0x0000000000000000
+
+	/* +8: kernel code 64-bit */
+	.quad 0x0020980000000000
+
+	/* +16: kernel data 64-bit */
+	.quad 0x0000920000000000
+
+	/* +24: user code 64-bit */
+	.quad 0x0020f80000000000
+
+	/* +32: user data 64-bit */
+	.quad 0x0000f20000000000
+
+	/* tss starts at +40 (5th entry) */
+	/* and gdt_table.size = 64B */
+
+	.fill 3, 8, 0
+gdt_end:
+
+gdt_desc:
+	.word gdt_end - gdt_table - 1
+	.quad gdt_table
+	
+
 .globl idt_table
 
 idt_table:
@@ -224,6 +256,12 @@ idt_desc:
 	.quad idt_table
 
 
+.globl stack_start
+
+stack_start:
+	.quad 0x200000
+
+
 .section .rodata
 .align 8
 

+ 20 - 2
init/main.c

@@ -33,7 +33,9 @@
 
 #include <kernel/mm.h>
 #include <kernel/pci.h>
+#include <kernel/int.h>
 
+#include <kernel/sched.h>
 #include <lib/string.h>
 
 #include <arch/io.h>
@@ -58,10 +60,26 @@ int init(void) {
 
 	int_init();
 	mm_init();
+	task_init();
 
-	__uint32_t config = pci_read_config(0x0, 0x00, 0x00, 0x00);
+	sti();
 
-	printk(0x00ffffff, 0x00000000, "read pci config: %x\n", config);
+	int *i = kmalloc(&pool, 128);
+
+	printk(0x00ffffff, 0x000000, "kmalloc: %q\n", i);
+
+	kfree(&pool, i, 128);
+
+	i = kmalloc(&pool, 128);
+	int *p = kmalloc(&pool, 128);
+
+	printk(0x00ffffff, 0x00000000, "kmalloc: %q, %q\n", i, p);
+
+	kfree(&pool, p, 256);
+
+	i = kmalloc(&pool, 266);
+
+	printk(0x00ffffff, 0x00000000, "kmalloc: %q\n", i);
 
 	return 0;
 }

+ 1 - 1
kernel/Makefile

@@ -1,6 +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 mm.o pci.o 
+all: int.o trap.o graphic.o vsprintf.o irq.o panic.o mm.o pci.o timer.o sched.o 
 
 
 %.o: %.S

+ 13 - 0
kernel/graphic.c

@@ -113,6 +113,19 @@ void putc(char c, __uint32_t fg, __uint32_t bg) {
 		return;
 	}
 
+	if (c == '\t') {
+		/* align to next tab point */
+
+		int spaces = ((x + TAB_SIZE - 1) & (~(TAB_SIZE - 1))) - x;
+
+		/* this is not good: maybe uses too many stack  */
+
+		for (; spaces; spaces--) 
+			putc(' ', fg, bg);
+
+		return;
+	}
+
 	struct psf_header *psf = (struct psf_header *) &font_start;
 
 	/* 

+ 35 - 21
kernel/int.c

@@ -87,6 +87,18 @@ int int_init(void) {
 
 	set_int_gate(idt_table + 0x80, 0, &syscall);
 
+	/* initial PIT timer */
+	outb(0x34, 0x43);
+
+	/* real time = 1.1932HZ / this */
+	/* about 10ms */
+
+#define TIMER_HZ_LOW  0x9c
+#define TIMER_HZ_HIGH 0x2e
+
+	outb(TIMER_HZ_LOW, 0x40);
+	outb(TIMER_HZ_HIGH, 0x40);
+
 	register_irq(0x00, irq0x20_interrupt, do_timer);
 	register_irq(0x01, irq0x21_interrupt, do_keyboard);
 
@@ -135,13 +147,15 @@ 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);
 
+	printk(0x00ff0000, 0x00000000, "rip: %q\n", rsp - RIP);
+
 	for (;;) hlt();
 
 	return;
 }
 
 
-void do_divide_error(__uint64_t *rsp) {
+void do_divide_error(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("divide error", rsp, 0);
@@ -152,7 +166,7 @@ void do_divide_error(__uint64_t *rsp) {
 }
 
 
-void do_debug(__uint64_t *rsp) {
+void do_debug(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("debug", rsp, 1);
@@ -162,7 +176,7 @@ void do_debug(__uint64_t *rsp) {
 	return;
 }
 
-void do_nmi(__uint64_t *rsp) {
+void do_nmi(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("nmi", rsp, 2);
@@ -172,7 +186,7 @@ void do_nmi(__uint64_t *rsp) {
 	return;
 }
 
-void do_int3(__uint64_t *rsp) {
+void do_int3(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("int3", rsp, 3);
@@ -182,7 +196,7 @@ void do_int3(__uint64_t *rsp) {
 	return;
 }
 
-void do_overflow(__uint64_t *rsp) {
+void do_overflow(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("overflow", rsp, 4);
@@ -192,7 +206,7 @@ void do_overflow(__uint64_t *rsp) {
 	return;
 }
 
-void do_bound(__uint64_t *rsp) {
+void do_bound(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("bound", rsp, 5);
@@ -202,7 +216,7 @@ void do_bound(__uint64_t *rsp) {
 	return;
 }
 
-void do_ud(__uint64_t *rsp) {
+void do_ud(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("undefined instruction", rsp, 6);
@@ -212,7 +226,7 @@ void do_ud(__uint64_t *rsp) {
 	return;
 }
 
-void do_no_fpu(__uint64_t *rsp) {
+void do_no_fpu(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("fpu not present", rsp, 7);
@@ -222,7 +236,7 @@ void do_no_fpu(__uint64_t *rsp) {
 	return;
 }
 
-void do_double_fault(__uint64_t *rsp) {
+void do_double_fault(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("double fault", rsp, 8);
@@ -232,7 +246,7 @@ void do_double_fault(__uint64_t *rsp) {
 	return;
 }
 
-void do_fpu_overrun(__uint64_t *rsp) {
+void do_fpu_overrun(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("fpu overrun", rsp, 9);
@@ -242,7 +256,7 @@ void do_fpu_overrun(__uint64_t *rsp) {
 	return;
 }
 
-void do_invalid_tss(__uint64_t *rsp) {
+void do_invalid_tss(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("invalid tss", rsp, 10);
@@ -252,7 +266,7 @@ void do_invalid_tss(__uint64_t *rsp) {
 	return;
 }
 
-void do_no_segment(__uint64_t *rsp) {
+void do_no_segment(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("no such segment", rsp, 11);
@@ -263,7 +277,7 @@ void do_no_segment(__uint64_t *rsp) {
 }
 
 
-void do_stack(__uint64_t *rsp) {
+void do_stack(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("stack error", rsp, 12);
@@ -274,7 +288,7 @@ void do_stack(__uint64_t *rsp) {
 }
 
 
-void do_gp(__uint64_t *rsp) {
+void do_gp(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("general protection", rsp, 13);
@@ -285,7 +299,7 @@ void do_gp(__uint64_t *rsp) {
 }
 
 
-void do_page_fault(__uint64_t *rsp) {
+void do_page_fault(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("page fault", rsp, 14);
@@ -296,7 +310,7 @@ void do_page_fault(__uint64_t *rsp) {
 }
 
 
-void do_reserved(__uint64_t *rsp) {
+void do_reserved(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("reserved", rsp, 15);
@@ -307,7 +321,7 @@ void do_reserved(__uint64_t *rsp) {
 }
 
 
-void do_fpu_error(__uint64_t *rsp) {
+void do_fpu_error(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("fpu error", rsp, 16);
@@ -318,7 +332,7 @@ void do_fpu_error(__uint64_t *rsp) {
 }
 
 
-void do_alignment(__uint64_t *rsp) {
+void do_alignment(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("alignment error", rsp, 17);
@@ -329,7 +343,7 @@ void do_alignment(__uint64_t *rsp) {
 }
 
 
-void do_machine(__uint64_t *rsp) {
+void do_machine(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("machine check", rsp, 18);
@@ -340,7 +354,7 @@ void do_machine(__uint64_t *rsp) {
 }
 
 
-void do_simd(__uint64_t *rsp) {
+void do_simd(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("simd error", rsp, 19);
@@ -351,7 +365,7 @@ void do_simd(__uint64_t *rsp) {
 }
 
 
-void do_virtualization(__uint64_t *rsp) {
+void do_virtualization(__uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("virtualization error", rsp, 20);

+ 2 - 17
kernel/irq.c

@@ -92,8 +92,6 @@ void unregister_irq(int nr) {
 void do_irq(__uint64_t *rsp, __uint64_t nr) {
 	schk_init();
 
-	printk(0x00ff0000, 0x00000000, "do_irq #%b\n", nr);
-
 	void (*fn) (void) = irq_table[nr - 0x20].handler;
 
 	if (fn) 
@@ -110,29 +108,16 @@ void do_irq(__uint64_t *rsp, __uint64_t nr) {
 }
 
 
-void do_timer(void) {
-	schk_init();
-
-	printk(0x00ff0000, 0x00000000, "irq 0x20: PIT timer. \n");
-
-	schk_leave();
-
-	return;
-}
-
-
 #define KEYBOARD_DATA 0x0060
 
 void do_keyboard(void) {
 	schk_init();
 
-	printk(0x00ff0000, 0x00000000, "irq 0x21: keyboard interrupt. \n");
-
 	/* get keyboard scan code */
 
-	char code = inb(KEYBOARD_DATA);
+	// char code = inb(KEYBOARD_DATA);
 
-	printk(0x00ff0000, 0x00000000, "key code: %b\n", code & 0xff);
+	/* we don't know how to use it now */
 
 	schk_leave();
 

+ 166 - 7
kernel/mm.c

@@ -36,12 +36,16 @@
 #include <lib/string.h>
 
 #include <arch/x86/int.h>
+#include <arch/x86/paging.h>
+
 #include <arch/io.h>
 #include <arch/sys.h>
 
 
 struct sysmm *mm;
 
+struct mempool pool;
+
 
 void mm_init(void) {
 	mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
@@ -131,20 +135,105 @@ void mm_init(void) {
 	/* 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) */
 
+	/* now we setup that memory pool */
+
+	/* default pool size: 2MB (1 page) */
+
+	pool.size = 0x200000;
+	pool.used = 0;
+
+	pool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
+
+	/* 32 bytes per bit */
+
+	/* we don't want use alloc_page() here, because it just want 
+	 * 4 KB, but alloc_page() gives us 2MB. 
+	 *
+	 * so we must find a place to store this. 
+	 *
+	 * and after that we can use kmalloc() to alloc this. 
+	 * (this is only for starting-up)
+	 *
+	 * it's at the end of syslog-buffer. 
+	 * */
+
+	pool.bitmap = (__uint8_t *) (SYSLOG_BUFFER + 32768);
+
+	pool.page = alloc_page();
+
+	pool.next = NULL;
+
 	return;
 
 #undef z
 }
 
+/* find an empty page table entry, and return its number */
+
+void *mapping(void *addr, __uint64_t attr) {
+	__uint64_t i;
+
+	/* well... */
+
+#define PML4E_ADDR ((__uint64_t *) 0x0000)
+#define PDPT_ADDR  ((__uint64_t *) 0x1000)
+#define PT_ADDR    ((__uint64_t *) 0x2000);
+
+	__uint64_t *pml4e = PML4E_ADDR;
+	__uint64_t *pdpt = PDPT_ADDR;
+	__uint64_t *pt = PT_ADDR;
+
+#define TABLE_ENTRIES 512
+
+	for (i = 0; ((__uint64_t *) pt)[i]; i++) {
+		if (i > TABLE_ENTRIES) {
+			/* reload pt */
+			pt = (__uint64_t *) *(++pdpt);
+
+			if (pdpt == PDPT_ADDR  + TABLE_ENTRIES) {
+				pdpt = (__uint64_t *) *(++pml4e);
+				pt = (__uint64_t *) *pdpt;
+
+				if (pml4e == PML4E_ADDR + TABLE_ENTRIES) 
+					return 0;
+			}
+		}
+	}
+
+	/* map it with attribute attr */
+
+	pt[i] = (__uint64_t) addr | attr;
+
+	/* get virtual address */
+
+#define PT_NR 		i
+#define PDPT_NR 	((pdpt - PDPT_ADDR) >> 3)
+#define PML4E_NR	((pml4e - PML4E_ADDR) >> 3)
+
+#define TABLE_SHIFT 9ul
+
+	__uint64_t vaddr = (PT_NR << PAGE_SHIFT)
+	       		+ (PDPT_NR << TABLE_SHIFT << PAGE_SHIFT)
+			+ (PML4E_NR << TABLE_SHIFT << PAGE_SHIFT << TABLE_SHIFT << PAGE_SHIFT);
+
+	return (void *) vaddr;
+
+#undef TABLE_SHIFT
+
+#undef PML4E_NR
+#undef PDPT_NR
+#undef PT_NR
+
+#undef TABLE_ENTRIES
+
+#undef PT_ADDR
+#undef PDPT_ADDR
+#undef PML4E_ADDR
+}
+
+
 
 void *alloc_page(void) {
 	__uint64_t i;
@@ -156,6 +245,13 @@ void *alloc_page(void) {
 
 	bitmap_set(mm->bitmap, i, 1);
 
+	/* we must map it to page-table */
+
+	if (!mapping((void *) (i << PAGE_SHIFT), PATT_KERNEL)) {
+		bitmap_set(mm->bitmap, i, 0);
+		return NULL;
+	}
+
 	return (void *) (i << PAGE_SHIFT);
 }
 
@@ -169,3 +265,66 @@ void free_page(void *p) {
 
 	return;
 }
+
+
+void *kmalloc(struct mempool *pool, __uint64_t size) {
+	if (!size || size > pool->size - pool->used) 
+		return NULL;
+
+	__uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
+	__uint64_t i;
+
+	if (points == 1) {
+		for (i = 0; bitmap_read(pool->bitmap, i); i++) {
+			if (i > pool->entries) 
+				return NULL;
+		}
+
+		bitmap_set(pool->bitmap, i, 1);
+		pool->used++;
+
+		return pool->page + (i << POOL_ENTRY_SHIFT);
+	}
+
+	/* it's not so good... */
+
+	for (i = 0; i < pool->entries; i++) {
+
+repeat:
+		/* if all 0... */
+		__uint64_t j;
+
+		for (j = 0; j < points; j++) {
+			if (bitmap_read(pool->bitmap, i + j)) {
+				i++;
+				goto repeat;
+			}
+		}
+
+		/* it will here when we found enough */
+
+		for (j = 0; j < points; j++) 
+			bitmap_set(pool->bitmap, j, 1);
+
+		pool->used += points;
+
+		return pool->page + (i << POOL_ENTRY_SHIFT);
+	}
+
+	return NULL;
+}
+
+
+void kfree(struct mempool *pool, void *p, __uint64_t size) {
+	__uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
+	__uint64_t i;
+	
+	i = (p - pool->page) >> POOL_ENTRY_SHIFT;
+
+	for (; points; points--, i++) 
+		bitmap_set(pool->bitmap, i, 0);
+
+	pool->used -= points;
+
+	return;
+}

+ 140 - 0
kernel/sched.c

@@ -0,0 +1,140 @@
+/* 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/sched.h>
+
+#include <arch/x86/int.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+
+#include <arch/x86/tss.h>
+#include <arch/x86/paging.h>
+
+#include <lib/string.h>
+
+
+struct task init_task;
+struct task *current;
+
+struct task *task_queues[16];
+
+__uint32_t next_pid = 0;
+
+
+int task_init(void) {
+	/* initial the init-tss */
+
+	init_tss.rsp0 = init_tss.rsp1 = init_tss.rsp2 = get_rsp();
+	init_tss.ist1 = init_tss.ist2 = init_tss.ist3 = get_rsp();
+	init_tss.ist4 = init_tss.ist5 = init_tss.ist6 = get_rsp();
+	init_tss.ist7 = get_rsp();
+
+	set_tss(&init_tss, 0);
+
+	/* initial the task struct for init task */
+
+	memset(&init_task, 0x00, sizeof(struct task));
+
+	next_pid++;
+
+	init_task.state = STATE_RUNNING;
+	init_task.ktime = init_task.utime = 0;
+
+	/* min priority */
+	init_task.priority = 15;
+
+	init_task.signal = 0;
+
+	/* init_task can't have any signals */
+	init_task.signal_mask = 0xffffffff;
+
+	init_task.exit_code = 0;
+
+	init_task.pid = init_task.father = INIT_PID;
+	init_task.tss = &init_tss;
+
+	init_task.cr3 = PAGE_TABLE_BASE;
+	init_task.rsp0 = get_rsp();
+	init_task.rsp = get_rsp();
+
+	init_task.dataseg = KERNEL_DS;
+	init_task.codeseg = KERNEL_CS;
+
+	init_task.rip = 0;
+	
+	/* copy registers to top of its stack */
+
+	init_task.next = NULL;
+
+	current = &init_task;
+	task_queues[15] = &init_task;
+
+//	switch_to(&init_task);
+
+	return 0;
+}
+
+
+int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
+	return 0;
+}
+
+
+
+void do_clock(void) {
+	printk(0x00ff0000, 0x00000000, "%d\n", jiffies / 100);
+
+	return;
+}
+
+
+void set_tss(struct tss *table, __uint64_t dpl) {
+	__uint64_t tss = (__uint64_t) table;
+
+	/* create a descriptor (in gdt) and register it */
+
+	struct tss_entry *entry = (struct tss_entry *) (gdt_table + 5);
+
+	entry->low = entry->high = 0x0000000000000000;
+
+	/* `103` is the size of a tss table */
+
+	entry->low = 103ul | ((tss & 0xffff) << 16) | (((tss >> 16) & 0xff) << 32) 
+		| ((0x80 + dpl + 0x09) << 40) | (tss << 56 & 0xff);
+
+	entry->high = (tss >> 32) & 0xffffffff;
+
+	/* load it to tr */
+	ltr(40);
+
+	return;
+}

+ 64 - 0
kernel/timer.S

@@ -0,0 +1,64 @@
+/* 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/>.  
+*/
+
+
+
+.globl do_timer
+
+do_timer:
+	/* we don't do a lot of things here */
+
+	incq jiffies(%rip)
+
+	movq time_to_sched(%rip), %rax
+	decq %rax
+
+	cmpq $0, %rax
+	
+	jne dt_ret
+
+	callq do_clock
+
+	movq $2, %rax
+
+dt_ret:
+	movq %rax, time_to_sched(%rip)
+
+	ret
+
+
+.data
+
+.globl jiffies
+
+jiffies:
+	.quad 0
+
+.globl time_to_sched
+
+/* time to next schedule */
+/* schedule time: 0.0200 s = 20 jiffies */
+
+time_to_sched:
+	.quad 100
+

+ 119 - 109
kernel/trap.S

@@ -23,6 +23,10 @@
 
 
 
+#define __ASM__
+
+#include <kernel/int.h>
+
 
 .globl __stack_chk_fail
 
@@ -39,14 +43,28 @@ scf_die:
 .globl divide_error
 
 divide_error:
+	/* that fake error-code */
+	pushq $0
+
+	/* we are going to change %rax */
 	pushq %rax
+
+	/* function handler */
 	leaq do_divide_error(%rip), %rax
 
-no_error_code:
+	/* restore %rax, and save handler to stack */
+	/* but now %rax is not in stack, push it later in error_code */
+	xorq %rax, (%rsp)
+
+error_code:
 	cli
+
 	/* and save other registers seriously now */
+
+	pushq %rax
 	pushq %rcx
 	pushq %rdx
+
 	pushq %rbp
 	pushq %rsi
 	pushq %rdi
@@ -62,26 +80,31 @@ no_error_code:
 
 	/* segments also */
 
-	movq %ds, %rcx
-	pushq %rcx
+	movq %ds, %rax
+	pushq %rax
 
-	movq %es, %rcx
-	pushq %rcx
+	movq %es, %rax
+	pushq %rax
 
-	movq %fs, %rcx
-	pushq %rcx
+	movq %fs, %rax
+	pushq %rax
 
-	movq %gs, %rcx
-	pushq %rcx
+	movq %gs, %rax
+	pushq %rax
 
 	/* KERNEL_DS */
-	movq $0x10, %rcx
-	movq %ds, %rcx
-	movq %es, %rcx
-	movq %fs, %rcx
-	movq %gs, %rcx
+	movq $0x10, %rax
+	movq %rax, %ds
+	movq %rax, %es
+	movq %rax, %fs
+	movq %rax, %gs
+
+	/* call that interrupt handler with two arguments */
 
-	/* call that interrupt handler */
+	movq %rsp, %rdi
+	movq ERRCODE(%rsp), %rsi
+
+	movq HANDLER(%rsp), %rax
 
 	callq *%rax
 
@@ -119,6 +142,9 @@ restore_all:
 
 	popq %rax
 
+	/* ignore that error-code and handler */
+	addq $16, %rsp
+
 	sti
 
 	/* then say goodbye to this interrupt */
@@ -127,147 +153,96 @@ restore_all:
 .globl debug
 
 debug:
+	pushq $0
+	
 	pushq %rax
 	leaq do_debug(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl nmi
 
 nmi:
+	pushq $0
+
 	pushq %rax
 	leaq do_nmi(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl int3
 
 int3:
+	pushq $0
+
 	pushq %rax
 	leaq do_int3(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl overflow
 
 overflow:
+	pushq $0
+
 	pushq %rax
 	leaq do_overflow(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl bound
 
 bound:
+	pushq $0
 	pushq %rax
 	leaq do_bound(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl invalid_code
 
 invalid_code:
+	pushq $0
+
 	pushq %rax
 	leaq do_ud(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl device_not_avaliable
 
 device_not_avaliable:
+	pushq $0
+
 	pushq %rax
 	leaq do_no_fpu(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl double_fault
 
 double_fault:
+	/* we shoudn't push $0 because error-code is already in stack */
+
 	pushq %rax
 	leaq do_double_fault(%rip), %rax
 
+	xorq %rax, (%rsp)
 
-error_code:
-	cli
-
-	/* save registers, and get that error-code */
-	/* now error-code is on the top of stack */
-
-	pushq %rcx
-	pushq %rbx
-	pushq %rdx
-
-	pushq %rbp
-	pushq %rsi
-	pushq %rdi
-
-	pushq %r8
-	pushq %r9
-	pushq %r10
-	pushq %r11
-
-	pushq %r12
-	pushq %r13
-	pushq %r14
-	pushq %r15
-
-	movq %ds, %rcx
-	pushq %rcx
-
-	movq %es, %rcx
-	pushq %rcx
-
-	movq %fs, %rcx
-	pushq %rcx
-
-	movq %gs, %rcx
-	pushq %rcx
-
-	movq $0x10, %rcx
-	movq %rcx, %ds
-	movq %rcx, %es
-	movq %rcx, %fs
-	movq %rcx, %gs
-
-	/* error code is the second argument */
-
-	movq %rsp, %rdi
-
-	call *%rax
-
-	/* restore and return */
-	popq %rax
-	movq %rax, %gs
-
-	popq %rax
-	movq %rax, %fs
-
-	popq %rax
-	movq %rax, %es
-
-	popq %rax
-	movq %rax, %ds
-
-	popq %r15
-	popq %r14
-	popq %r13
-	popq %r12
-
-	popq %r11
-	popq %r10
-	popq %r9
-	popq %r8
-
-	popq %rdi
-	popq %rsi
-	popq %rbp
-
-	popq %rdx
-	popq %rbx
-	popq %rcx
-	popq %rax
-
-	iretq
 
 .globl fpu_segment_overrun
 
@@ -275,13 +250,18 @@ fpu_segment_overrun:
 	pushq %rax
 	leaq do_fpu_overrun(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
+
 
 .globl invalid_tss
 
 invalid_tss:
 	pushq %rax
 	leaq do_invalid_tss(%rip), %rax
+
+	xorq %rax, (%rsp)
 	
 	jmp error_code
 
@@ -291,6 +271,8 @@ segment_not_present:
 	pushq %rax
 	leaq do_no_segment(%rip), %rax
 
+	xorq %rax, (%rsp)
+
 	jmp error_code
 
 
@@ -300,6 +282,8 @@ stack_error:
 	pushq %rax
 	leaq do_stack(%rip), %rax
 
+	xorq %rax, (%rsp)
+
 	jmp error_code
 
 
@@ -309,6 +293,8 @@ general_protection:
 	pushq %rax
 	leaq do_gp(%rip), %rax
 
+	xorq %rax, (%rsp)
+
 	jmp error_code
 
 .globl page_fault
@@ -317,23 +303,33 @@ page_fault:
 	pushq %rax
 	leaq do_page_fault(%rip), %rax
 
+	xorq %rax, (%rsp)
+
 	jmp error_code
 
 .globl reserved
 
 reserved:
+	pushq $0
+
 	pushq %rax
 	leaq do_reserved(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl fpu_error
 
 fpu_error:
+	pushq $0
+
 	pushq %rax
 	leaq do_fpu_error(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl alignment
 
@@ -341,31 +337,45 @@ alignment:
 	pushq %rax
 	leaq do_alignment(%rip), %rax
 
+	xorq %rax, (%rsp)
+
 	jmp error_code
 
 .globl machine
 
 machine:
+	pushq $0
+
 	pushq %rax
 	leaq do_machine(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl simd
 
 simd:
+	pushq $0
+
 	pushq %rax
 	leaq do_simd(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 .globl virtualization
 
 virtualization:
+	pushq $0
+
 	pushq %rax
 	leaq do_virtualization(%rip), %rax
 
-	jmp no_error_code
+	xorq %rax, (%rsp)
+
+	jmp error_code
 
 
 .globl ret_intr