Преглед изворни кода

7.274u-tty: using tty but not the vga graphic

rain пре 5 година
родитељ
комит
97fc7d54e0
23 измењених фајлова са 373 додато и 393 уклоњено
  1. 1 0
      LINES
  2. 4 2
      Makefile
  3. 4 0
      README.md
  4. 18 35
      boot/loader.asm
  5. 0 106
      include/kernel/graphic.h
  6. 7 3
      include/kernel/init.h
  7. 26 0
      include/kernel/int.h
  8. 0 1
      include/kernel/schk.h
  9. 67 0
      include/modules/tty.h
  10. 2 5
      init/head.S
  11. 5 9
      init/main.c
  12. 1 1
      kernel/Makefile
  13. 0 199
      kernel/graphic.c
  14. 4 4
      kernel/int.c
  15. 2 2
      kernel/irq.c
  16. 9 11
      kernel/mm.c
  17. 3 3
      kernel/panic.c
  18. 0 2
      kernel/pci.c
  19. 4 6
      kernel/sched.c
  20. 2 2
      kernel/sys.c
  21. 0 1
      kernel/vsprintf.c
  22. 1 1
      modules/Makefile
  23. 213 0
      modules/tty.c

+ 1 - 0
LINES

@@ -26,4 +26,5 @@
 2021-8-04 7.073: 7073 lines, 50 files, rain
 2021-8-04 7.073: 7073 lines, 50 files, rain
 2021-8-04 7.110: 7110 lines, 50 files, rain
 2021-8-04 7.110: 7110 lines, 50 files, rain
 2021-8-08 7.301: 7301 lines, 50 files, rain
 2021-8-08 7.301: 7301 lines, 50 files, rain
+2021-8-08 7.274: 7274 lines, 50 files, rain
 
 

+ 4 - 2
Makefile

@@ -3,8 +3,10 @@ BOOT_SRCS = boot/boot.asm boot/loader.asm
 BOOT_OBJS = boot/boot.bin boot/loader.bin 
 BOOT_OBJS = boot/boot.bin boot/loader.bin 
 RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c
 RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c
 RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o 
 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 kernel/timer.o kernel/sched.o kernel/sys.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 kernel/sys.c $(RLIB_SRCS)
+MODULE_SRCS = modules/tty.c 
+MODULE_OBJS = modules/tty.o 
+KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o kernel/sys.o font.o $(RLIB_OBJS) $(MODULE_OBJS)
+KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c kernel/pci.c kernel/timer.S kernel/sched.c kernel/sys.c $(RLIB_SRCS) $(MODULE_SRCS)
 
 
 all: Makefile
 all: Makefile
 	make -C tools/
 	make -C tools/

+ 4 - 0
README.md

@@ -112,3 +112,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;maybe that is too large? <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;maybe that is too large? <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;and, main didn't work on the real-machine. :-(
 &nbsp;&nbsp;&nbsp;&nbsp;and, main didn't work on the real-machine. :-(
 
 
+7.274u-tty:  
+&nbsp;&nbsp;&nbsp;&nbsp;now we use the vga-text, not that graphic mode because it's buggy :-(. now we can roll that fing screen (I tried to do this for many times) <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;and we can put it in colors. 
+

+ 18 - 35
boot/loader.asm

@@ -30,6 +30,8 @@ VMODE	 	equ 0x7c01
 SCREENX 	equ 0x7c02
 SCREENX 	equ 0x7c02
 SCREENY 	equ 0x7c04
 SCREENY 	equ 0x7c04
 FB_ADDR 	equ 0x7c06
 FB_ADDR 	equ 0x7c06
+CURSORX		equ 0x7c0a
+CURSORY 	equ 0x7c0b
 
 
 
 
 _start:
 _start:
@@ -48,31 +50,29 @@ _start:
 	call load_ksetup
 	call load_ksetup
 
 
 .set_vga:
 .set_vga:
-	; get mode info
-	mov ax, 0x4f01
-	mov cx, 0x4118
-	mov di, 0x7000
+	; well, now we use the VGA text mode, so...
 
 
-	int 0x10
-
-	jc die
+	mov byte [VMODE], 0x00
+	mov word [SCREENX], 80
+	mov word [SCREENY], 25
+	mov dword [FB_ADDR], 0xb8000
 
 
-	mov al,[es:di]
-	mov [VMODE], al
+	; we can't put any messages after this, because kernel will 
+	; use this to put messages. 
 
 
-	mov ax, [es:di + 18]
-	mov [SCREENX], ax
+	; if we put msgs again, so must get cursor position again. 
 
 
-	mov ax, [es:di + 20]
-	mov [SCREENY], ax
+.get_cursor:
+	mov ah, 3
 
 
-	mov eax, [es:di + 40]
-	mov [FB_ADDR], eax
+	; page number
+	xor bh, bh
 
 
-	mov ax, 0x4f02
-	mov bx, 0x4118
 	int 0x10
 	int 0x10
-	
+
+	; dh: y  dl: x
+	mov [CURSORX], dl
+	mov [CURSORY], dh
 
 
 .open_a20:
 .open_a20:
 	; disable interrupts before we initial done (enable in kernel)
 	; disable interrupts before we initial done (enable in kernel)
@@ -502,23 +502,6 @@ init_prot:
 	mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
 	mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
 ;	mov dword [0x0000200c], 0x00000000
 ;	mov dword [0x0000200c], 0x00000000
 
 
-	; get frame buffer address and map it here. 
-
-	mov eax, [0x7c06]
-
-	; eax &= PAGE_SIZE
-	and eax, ~(1048576 * 2 - 1)
-	or eax, PAGE_KERNEL
-
-	mov dword [0x00002010], eax
-; 	mov dword [0x00002014], 0x00000000
-
-	add eax, 1048576 * 2
-	mov dword [0x00002018], eax
-
-	add eax, 1048576 * 2
-	mov dword [0x00002020], eax
-
 
 
 .enable_long_mode:
 .enable_long_mode:
 	lgdt [gdt64_desc]
 	lgdt [gdt64_desc]

+ 0 - 106
include/kernel/graphic.h

@@ -1,106 +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/>.  
-*/
-
-
-
-
-#ifndef _GRAPHIC_H
-#define _GRAPHIC_H
-
-
-#include <kernel/types.h>
-
-#define TAB_SIZE  8
-
-#define rgb(r, g, b) ((b) + ((g) << 8) + ((r) << 16))
-
-#define black 0x00000000
-#define red     rgb(255, 0, 0)
-#define success rgb(114, 176, 161)
-#define error   rgb(244, 130, 100)
-#define yellow  rgb(0, 255, 255)
-#define green   rgb(0, 255, 0)
-#define white   rgb(255, 255, 255)
-
-void putblock(__uint32_t, int, int, int, int);
-void putc(char, __uint32_t, __uint32_t);
-void print(__uint32_t, __uint32_t, char *);
-int printk(__uint32_t, __uint32_t, char *, ...);
-void syslog(const char *);
-
-
-/* 
- * we support 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;
-};
-
-
-/* '__uint64_t * ' can change to other types */
-/* because they are all just lables with an address */
-
-extern __uint8_t *font_start;
-extern __uint8_t *font_end;
-
-/* but 'font_size' must be __uint64_t because it's a value */
-extern __uint64_t font_size;
-
-
-#define FB_ADDR ((__uint32_t *) 0x400000)
-
-/* x and y in this vga mode */
-#define VGA_X   (bootarg->vga_x)
-#define VGA_Y   (bootarg->vga_y)
-
-/* this is default value */
-
-#define CHAR_HEIGHT 16
-#define CHAR_WIDTH  8
-
-#define X_CHARS (VGA_X / CHAR_WIDTH)
-#define Y_LINES (VGA_Y / CHAR_HEIGHT)
-
-#endif

+ 7 - 3
include/kernel/init.h

@@ -43,10 +43,10 @@
 /* slowest: schk will display function info in this mode */
 /* slowest: schk will display function info in this mode */
 #define DEBUG_LEVEL		TEST_DEBUG_LEVEL
 #define DEBUG_LEVEL		TEST_DEBUG_LEVEL
 
 
-#include <kernel/graphic.h>
+#include <modules/tty.h>
 
 
-#define trappoint() \
-	printk(0x0000ff00, 0x00000000, "system runs at %s:%s.%d\n", __FILE__, __FUNCTION__, __LINE__);
+#define trappoint() 
+//	printk(0x0000ff00, 0x00000000, "system runs at %s:%s.%d\n", __FILE__, __FUNCTION__, __LINE__);
 
 
 #define emerg(s, ...) printk(red, black, s, __VA_ARGS__)
 #define emerg(s, ...) printk(red, black, s, __VA_ARGS__)
 #define log(s, ...)   printk(green, black, s, __VA_ARGS__)
 #define log(s, ...)   printk(green, black, s, __VA_ARGS__)
@@ -84,6 +84,10 @@ struct bootarg {
 
 
 	/* yes, just 32 bytes this pointer is */
 	/* yes, just 32 bytes this pointer is */
 	__uint32_t fb_addr;
 	__uint32_t fb_addr;
+
+	/* cursor position x and y */
+	__uint8_t x;
+	__uint8_t y;
 } __attribute__((packed));
 } __attribute__((packed));
 
 
 /* init/main.c */
 /* init/main.c */

+ 26 - 0
include/kernel/int.h

@@ -135,6 +135,32 @@ extern struct irq_entry irq_table[MAX_IRQ_NR];
 #endif
 #endif
 
 
 /* offsets value in stack */
 /* offsets value in stack */
+#define off_gs	0
+#define off_fs 	1
+#define off_es 	2
+#define off_ds 	3
+#define off_r15	4
+#define off_r14	5
+#define off_r13	6
+#define off_r12	7
+#define off_r11	8
+#define off_r10	9
+#define off_r9	10
+#define off_r8	11
+#define off_rdi	12
+#define off_rsi	13
+#define off_rbp	14
+#define off_rbx 15
+#define off_rdx	16
+#define off_rcx	17
+#define off_rax	18
+
+#define off_handler 	19
+#define off_errcode	20
+#define off_rip 	21
+#define off_rflags	22
+#define off_rsp		23
+#define off_ss		24
 
 
 #define GS 	0
 #define GS 	0
 #define FS 	8
 #define FS 	8

+ 0 - 1
include/kernel/schk.h

@@ -33,7 +33,6 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/panic.h>
 #include <kernel/panic.h>
 
 
-#include <kernel/graphic.h>
 #include <arch/sys.h>
 #include <arch/sys.h>
 
 
 
 

+ 67 - 0
include/modules/tty.h

@@ -0,0 +1,67 @@
+/* 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 _MOD_TTY_H
+#define _MOD_TTY_H
+
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+
+#define TAB_SIZE  8
+
+errno_t tty_init(void);
+
+void putc(char, __uint8_t);
+void print(__uint8_t, char *);
+
+int printk(__uint8_t, char *, ...);
+void syslog(const char *);
+
+void update_cursor(void);
+__uint16_t read_cursor(void);
+
+void roll_screen(void);
+
+#define FB_ADDR ((__uint16_t *) 0xb8000)
+
+/* x and y in this vga mode */
+#define VGA_X   (bootarg->vga_x)
+#define VGA_Y   (bootarg->vga_y)
+
+#define CURSOR_INDEX 	0x03d4
+#define CURSOR_DATA 	0x03d5
+
+#define CMD_POS_HI 	0x0e
+#define CMD_POS_LO 	0x0f
+
+#define white	0x07
+#define red 	0x04
+#define yellow 	0x0e
+
+#endif
+

+ 2 - 5
init/head.S

@@ -35,9 +35,8 @@
  */
  */
 
 
 #define WARNING(msg)	\
 #define WARNING(msg)	\
-	movq RED, %rdi;	\
-	xorq %rsi, %rsi;\
-	leaq msg, %rdx;	\
+	movq 0x0e, %rdi;\
+	leaq msg, %rsi;	\
 	call print;
 	call print;
 			
 			
 	
 	
@@ -125,8 +124,6 @@ init_done:
 
 
 	movq $0, %rax
 	movq $0, %rax
 
 
-	int $0x80
-
 init_die:
 init_die:
 	jmp init_die
 	jmp init_die
 
 

+ 5 - 9
init/main.c

@@ -27,7 +27,7 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
-#include <kernel/graphic.h>
+#include <modules/tty.h>
 #include <kernel/vsprintf.h>
 #include <kernel/vsprintf.h>
 #include <kernel/schk.h>
 #include <kernel/schk.h>
 
 
@@ -57,15 +57,9 @@ struct bootarg *bootarg = (struct bootarg *) 0x7c00;
  * */
  * */
 
 
 int init(void) {
 int init(void) {
-	print(0x00ffffff, 0x00000000, "XNIX kernel is starting...\n");
+	tty_init();
 
 
-	emerg("emergency: %s %x\n", "test", 0xabcdef0123456789);
-	log("log: %s %x\n", "test", 0xabcdef0123456789);
-	warn("warn: %s %x\n", "test", 0xabcdef0123456789);
-	mesg("mesg: %s %x\n", "test", 0xabcdef0123456789);
-
-	print(success, black, "test for color success. \n");
-	print(error, black, "test for color black. \n");
+	print(red, "XNIX kernel starting...\n");
 
 
 	int_init();
 	int_init();
 	mm_init();
 	mm_init();
@@ -74,6 +68,8 @@ int init(void) {
 
 
 	sti();
 	sti();
 
 
+	trace_mm();
+
 	return 0;
 	return 0;
 }
 }
 
 

+ 1 - 1
kernel/Makefile

@@ -1,6 +1,6 @@
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
 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 timer.o sched.o sys.o 
+all: int.o trap.o vsprintf.o irq.o panic.o mm.o pci.o timer.o sched.o sys.o  
 
 
 
 
 %.o: %.S
 %.o: %.S

+ 0 - 199
kernel/graphic.c

@@ -1,199 +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/types.h>
-#include <kernel/errno.h>
-
-#include <kernel/graphic.h>
-#include <kernel/vsprintf.h>
-#include <kernel/schk.h>
-
-#include <lib/string.h>
-
-#include <stdarg.h>
-
-
-static char buffer[256];
-
-
-static int syslog_index = 0;
-
-
-void syslog(const char *s) {
-	/* we can't use schk in this function, */
-	/* because this will make a die-loop (schk_init calls this function) */
-	int i;
-
-	for (i = 0; s[i]; i++) {
-		if (syslog_index + i == 32768) 
-			syslog_index = 0;
-	
-		*(syslog_index + SYSLOG_BUFFER) = s[i];			
-		syslog_index++;
-	}
-
-
-	return;
-}
-
-
-int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
-	schk_init();
-
-	va_list args;
-	int i;
-
-	/* I think 256 is enough in kernel */
-	/* too long, return it. */
-
-	va_start(args, s);
-
-	/* it maybe overflow, but that's in .data section and we did check. */
-
-	i = vsprintf(buffer, s, args);
-	print(fg, bg, buffer);
-	
-	memset(buffer, 0, 256);
-
-	va_end(args);
-
-	schk_leave();
-
-	return i;
-}
-
-
-static int x = 0, y = 0;
-
-
-void putc(char c, __uint32_t fg, __uint32_t bg) {
-	schk_init();
-
-	if (c == '\n') {
-		/* it's '\n', but we use '\r\n' */
-
-		if (y + CHAR_HEIGHT > VGA_Y) 
-			y = 0;
-
-		else 
-			y += 16;
-
-		x = 0;
-
-		/* and clear next line */
-
-		return;
-	}
-
-	if (c == '\r') {
-		x = 0;
-		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;
-
-	/* 
-	 * yeah, it's very ugly. but it just get address of this glyph, and check 
-	 * 'c' is really in this font. 
-	 * */
-
-	__uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size + 
-		(c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
-
-	/* need next line? */
-	if (x >= VGA_X) {
-		x = 0;
-		y += CHAR_HEIGHT;
-	}
-
-	/* no enough space for this character */
-	/* VGA_Y % CHAR_HEIGHT != 0, so we cannot use y == VGA_Y */
-
-	if (y + CHAR_HEIGHT > VGA_Y) 
-		y = 0;
-
-	/* okay, now we put it on the screen */
-
-	int i;
-	int mask;
-	char d;
-
-	for (i = 0; i < 16; i++) {
-		__uint32_t *addr;
-		int j;
-
-		addr = FB_ADDR + VGA_X * (y + i) + x;
-		mask = 0x0100;
-
-		d = glyph[i];
-
-		for (j = 0; j < 8; j++) {
-			mask >>= 1;
-
-			if (d & mask) 
-				addr[j] = fg;
-			
-			else 
-				addr[j] = bg;
-		}
-	}
-
-	x += 8;
-
-	schk_leave();
-
-	return;
-}
-
-
-void print(__uint32_t fg, __uint32_t bg, char *s) {
-	schk_init();
-
-	for (; *s; s++) {
-		if (*s == '\n') 
-			putc(' ', fg, bg);
-
-		putc(*s, fg, bg);
-	}
-
-	schk_leave();
-
-	return;
-}
-

+ 4 - 4
kernel/int.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
-#include <kernel/graphic.h>
+#include <modules/tty.h>
 #include <kernel/int.h>
 #include <kernel/int.h>
 #include <kernel/schk.h>
 #include <kernel/schk.h>
 #include <kernel/mm.h>
 #include <kernel/mm.h>
@@ -132,10 +132,10 @@ void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
 void die(char *s, __uint64_t *rsp, char nr) {
 void die(char *s, __uint64_t *rsp, char nr) {
 	/* should we use schk here? */
 	/* should we use schk here? */
 
 
-	printk(0x00ff0000, 0x00000000, "int #%b: %s. \n", nr, s);
-	printk(0x00ff0000, 0x00000000, "rsp: %q\n", rsp);
+	printk(red, "int #%b: %s. \n", nr, s);
+	printk(red, "rsp: %q\n", rsp);
 
 
-	printk(0x00ff0000, 0x00000000, "rip: %q\n", rsp + RIP);
+	printk(red, "rip: %q\n", rsp[off_rip]);
 
 
 	trace_mm();
 	trace_mm();
 
 

+ 2 - 2
kernel/irq.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
-#include <kernel/graphic.h>
+#include <modules/tty.h>
 #include <kernel/int.h>
 #include <kernel/int.h>
 #include <kernel/schk.h>
 #include <kernel/schk.h>
 
 
@@ -98,7 +98,7 @@ void do_irq(__uint64_t *rsp, __uint64_t nr) {
 		fn();
 		fn();
 
 
 	else 
 	else 
-		printk(0x00ff0000, 0x00000000, "unknow irq %x\n", nr - 0x20);
+		printk(red, "unknow irq %x\n", nr - 0x20);
 		
 		
 	outb(0x20, 0x20);
 	outb(0x20, 0x20);
 
 

+ 9 - 11
kernel/mm.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
-#include <kernel/graphic.h>
+#include <modules/tty.h>
 #include <kernel/int.h>
 #include <kernel/int.h>
 #include <kernel/mm.h>
 #include <kernel/mm.h>
 
 
@@ -64,7 +64,7 @@ void mm_init(void) {
 		if (!e->type || e->type >= E820_INV) 
 		if (!e->type || e->type >= E820_INV) 
 			break;
 			break;
 
 
-		printk(0x00ffffff, 0x00000000, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
+		printk(white, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
 
 
 		/* create a `struct zone` for this */
 		/* create a `struct zone` for this */
 
 
@@ -88,10 +88,10 @@ void mm_init(void) {
 		e++;
 		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(white, "total usable ram: %d MB. \n", total_ram >> 20);
+	printk(white, "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);
+	printk(white, "total zones: %d\n", mm->total_zones);
 
 
 	/* setup page bitmap */
 	/* setup page bitmap */
 
 
@@ -257,8 +257,6 @@ void *kmalloc(struct mempool *pool, __uint64_t size) {
 		return pool->page + (i << POOL_ENTRY_SHIFT);
 		return pool->page + (i << POOL_ENTRY_SHIFT);
 	}
 	}
 
 
-	printk(0x00ffffff, 0x00000000, "need %d points. \n", points);
-
 	/* it's not so good... */
 	/* it's not so good... */
 
 
 	for (i = 0; i < pool->entries; i++) {
 	for (i = 0; i < pool->entries; i++) {
@@ -290,7 +288,7 @@ repeat:
 
 
 panic:
 panic:
 
 
-	printk(0x00ff0000, 0x00000000, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n", 
+	printk(yellow, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n", 
 		pool->size >> 10, pool->used << 6, points << 6);
 		pool->size >> 10, pool->used << 6, points << 6);
 
 
 	return NULL;
 	return NULL;
@@ -313,11 +311,11 @@ void kfree(struct mempool *pool, void *p, __uint64_t size) {
 
 
 
 
 void trace_mm(void) {
 void trace_mm(void) {
-	printk(0x0000ff00, 0x00000000, "tracing system memory: \n");
+	printk(yellow, "tracing system memory: \n");
 
 
 	__uint64_t *cr3 = (__uint64_t *) get_cr3();
 	__uint64_t *cr3 = (__uint64_t *) get_cr3();
 
 
-	printk(0x0000ff00, 0x00000000, "pml4: 0x%q  pdp: 0x%q  pt: 0x%q\n", 
+	printk(yellow, "pml4: 0x%q  pdp: 0x%q  pt: 0x%q\n", 
 		cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
 		cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
 
 
 	__uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
 	__uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
@@ -325,7 +323,7 @@ void trace_mm(void) {
 
 
 	for (i = 0; i < 512; i++) {
 	for (i = 0; i < 512; i++) {
 		if (pt[i]) 
 		if (pt[i]) 
-			printk(0x0000ff00, 0x00000000, "%q -> %q\n", i << PAGE_SHIFT, pt[i] & ~0xff);
+			printk(yellow, "%q -> %q\n", i << PAGE_SHIFT, pt[i] & ~0xff);
 	}
 	}
 		
 		
 
 

+ 3 - 3
kernel/panic.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
-#include <kernel/graphic.h>
+#include <modules/tty.h>
 #include <kernel/int.h>
 #include <kernel/int.h>
 
 
 #include <arch/x86/int.h>
 #include <arch/x86/int.h>
@@ -38,8 +38,8 @@
 
 
 
 
 void panic(char *s) {
 void panic(char *s) {
-	printk(0x00ff0000, 0x00000000, "kernel panic: %s\n", s);
-	printk(0x00ff0000, 0x00000000, "for more debug information, see syslog 0x90000\n");
+	printk(red, "kernel panic: %s\n", s);
+	printk(red, "for more debug information, see syslog 0x90000\n");
 
 
 	for (;;) hlt();
 	for (;;) hlt();
 }
 }

+ 0 - 2
kernel/pci.c

@@ -27,8 +27,6 @@
 #include <kernel/init.h>
 #include <kernel/init.h>
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
-
-#include <kernel/graphic.h>
 #include <kernel/int.h>
 #include <kernel/int.h>
 
 
 #include <arch/x86/int.h>
 #include <arch/x86/int.h>

+ 4 - 6
kernel/sched.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
-#include <kernel/graphic.h>
+#include <modules/tty.h>
 #include <kernel/int.h>
 #include <kernel/int.h>
 #include <kernel/sched.h>
 #include <kernel/sched.h>
 
 
@@ -57,14 +57,14 @@ __uint32_t next_pid = 0;
 
 
 
 
 __uint64_t ktask(__uint64_t arg) {
 __uint64_t ktask(__uint64_t arg) {
-	printk(0x00ffffff, 0x00000000, "main is now running. arg: %q\n", arg);
+	printk(white, "main is now running. arg: %q\n", arg);
 
 
 	return 1;
 	return 1;
 }
 }
 
 
 
 
 int do_exit(__uint64_t exitcode) {
 int do_exit(__uint64_t exitcode) {
-	printk(0x00ffffff, 0x00000000, "do_exit: %q\n", exitcode);
+	printk(white, "do_exit: %q\n", exitcode);
 
 
 	for (;;);
 	for (;;);
 }
 }
@@ -85,7 +85,7 @@ int task_init(void) {
 	current = task_lists[0];
 	current = task_lists[0];
 
 
 	if (ktask_create(ktask, 0x12345678) == -ENOMEM) {
 	if (ktask_create(ktask, 0x12345678) == -ENOMEM) {
-		printk(0x00ff0000, 0x00000000, "ktask_create returns -ENOMEM (no enough memory). ");
+		printk(red, "ktask_create returns -ENOMEM (no enough memory). ");
 		return 1;
 		return 1;
 	}
 	}
 		
 		
@@ -286,8 +286,6 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 	else 
 	else 
 		t->next = child;
 		t->next = child;
 
 
-	printk(0x00ffffff, 0x00000000, "task creates: rsp %q, rip %q\n", child->rsp, child->rip);
-
 	return 0;
 	return 0;
 }
 }
 
 

+ 2 - 2
kernel/sys.c

@@ -28,11 +28,11 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
-#include <kernel/graphic.h>
+#include <modules/tty.h>
 
 
 
 
 int no_syscall(void) {
 int no_syscall(void) {
-	printk(0x00ffffff, 0x00000000, "no such system call. ");
+	printk(yellow, "no such system call. ");
 
 
 	return -ENOSYS;
 	return -ENOSYS;
 }
 }

+ 0 - 1
kernel/vsprintf.c

@@ -28,7 +28,6 @@
 #include <kernel/errno.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
-#include <kernel/graphic.h>
 #include <kernel/vsprintf.h>
 #include <kernel/vsprintf.h>
 #include <kernel/schk.h>
 #include <kernel/schk.h>
 
 

+ 1 - 1
modules/Makefile

@@ -1,4 +1,4 @@
-OBJS = 
+OBJS = tty.o 
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 
 
 all: $(OBJS)
 all: $(OBJS)

+ 213 - 0
modules/tty.c

@@ -0,0 +1,213 @@
+/* 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/types.h>
+#include <kernel/errno.h>
+
+#include <modules/tty.h>
+#include <kernel/vsprintf.h>
+#include <kernel/schk.h>
+
+#include <lib/string.h>
+#include <arch/io.h>
+
+#include <stdarg.h>
+
+
+
+static char buffer[256];
+
+
+static int syslog_index = 0;
+static __uint16_t *fb = FB_ADDR;
+static __uint8_t x, y;
+
+
+errno_t tty_init(void) {
+	x = bootarg->x, y = bootarg->y;
+
+	/* setup the frame buffer address fb */
+	/* note that `fb` is a 16-bits pointer, so we don't need to '<< 1' */
+	fb += y * VGA_X + x;
+
+	/* well, this is the end */
+
+	return 0;
+}
+
+
+void syslog(const char *s) {
+	/* we can't use schk in this function, */
+	/* because this will make a die-loop (schk_init calls this function) */
+	int i;
+
+	for (i = 0; s[i]; i++) {
+		if (syslog_index + i == 32768) 
+			syslog_index = 0;
+	
+		*(syslog_index + SYSLOG_BUFFER) = s[i];			
+		syslog_index++;
+	}
+
+
+	return;
+}
+
+
+int printk(__uint8_t color, char *s, ...) {
+	schk_init();
+
+	va_list args;
+	int i;
+
+	/* I think 256 is enough in kernel */
+	/* too long, return it. */
+
+	va_start(args, s);
+
+	/* it maybe overflow, but that's in .data section and we did check. */
+
+	i = vsprintf(buffer, s, args);
+	print(color, buffer);
+	
+	memset(buffer, 0, 256);
+
+	va_end(args);
+
+	schk_leave();
+
+	return i;
+}
+
+void print(__uint8_t color, char *s) {
+	while (*s) 
+		putc(*s++, color);
+
+	return;
+}
+
+
+void putc(char c, __uint8_t color) {
+	if (c == '\n') {
+		/* we do it like \r\n */
+		x = 0;
+
+		if (++y == VGA_Y) {
+			roll_screen();
+			y = 24;
+		}
+
+		update_cursor();
+
+		return;
+	}
+
+	else if (c == '\r') {
+		x = 0;
+		update_cursor();
+
+		return;
+	}
+
+	else if (c == '\t') {
+		x = (x + TAB_SIZE - 1) & ~(TAB_SIZE - 1);
+		update_cursor();
+		
+		return;
+	}
+
+	__asm__ __volatile__ (
+		/* rdi is the frame buffer, and rax is the  */
+		"cld\n\t"
+		"movb %%cl, %%ah\n\t"
+		"stosw\n\t"
+		
+		: "+D" (fb)
+		: "a" (c), "c" (color)
+	);
+
+	if (++x == VGA_X) {
+		x = 0;
+
+		if (++y == VGA_Y) {
+			roll_screen();
+			y = 24;
+		}
+	}
+
+	update_cursor();
+
+	return;
+}
+
+
+__uint16_t read_cursor(void) {
+	__uint16_t pos = 0;
+
+	outb(CMD_POS_LO, CURSOR_INDEX);
+	pos |= inb(CURSOR_DATA);
+
+	outb(CMD_POS_HI, CURSOR_INDEX);
+	pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
+
+	return pos;
+}
+
+void update_cursor(void) {
+	__uint16_t pos = y * bootarg->vga_x + x;
+	fb = FB_ADDR + pos;
+
+	outb(CMD_POS_LO, CURSOR_INDEX);
+	outb(pos & 0xff, CURSOR_DATA);
+
+	outb(CMD_POS_HI, CURSOR_INDEX);
+	outb((pos >> 8) & 0xff, CURSOR_DATA);
+
+	return;
+}
+
+
+void roll_screen(void) {
+	/* roll down the screen */
+
+	__asm__ __volatile__ (
+		"cld\n\t"
+		"movq $960, %%rcx\n\t"
+		"movq $0xb80a0, %%rsi\n\t"
+		"movq $0xb8000, %%rdi\n\t"
+		"rep\n\t"
+		"movsq\n\t"
+
+		"movq $0xb8f00, %%rdi\n\t"
+		"movq $0x0720, %%rax\n\t"
+		"movq $80, %%rcx\n\t"
+		"rep\n\t"
+		"stosw\n\t"
+		:::
+	);
+
+	return;
+}