Parcourir la source

9.765u-mods: now print uses both com0 and console

rain il y a 5 ans
Parent
commit
0f5b485454
11 fichiers modifiés avec 89 ajouts et 28 suppressions
  1. 2 2
      LINES
  2. 4 1
      README.md
  3. 3 0
      include/kernel/tty.h
  4. 7 0
      include/modules/serial.h
  5. 2 5
      init/main.c
  6. 3 0
      kernel/console.c
  7. 10 0
      kernel/irq.c
  8. 7 0
      kernel/sched.c
  9. 17 1
      kernel/timer.S
  10. 4 1
      kernel/tty.c
  11. 30 18
      modules/serial.c

+ 2 - 2
LINES

@@ -44,6 +44,6 @@
 2021-8-23 9.640: 9640 lines, 63 files, rain
 2021-8-23 9.380: 9380 lines, 61 files, rain
 2021-8-24 9.742: 9742 lines, 65 files, rain
-2021-8-24 9.742: 9742 lines, 65 files, rain
-2021-8-24 9.966: 9966 lines, 67 files, rain
 2021-8-24 9.707: 9707 lines, 65 files, rain
+2021-8-24 9.765: 9765 lines, 65 files, rain
+

+ 4 - 1
README.md

@@ -169,7 +169,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;now we use another mode to `register_irq`: a new api that `set_irq`. 
 &nbsp;&nbsp;&nbsp;&nbsp;`set_irq` just need 2 arguments, it's simplier, and faster. 
 
-<<<<<<< HEAD
 9.380u-mods:  
 &nbsp;&nbsp;&nbsp;&nbsp;move the ahci-driver to the dev branch, because it's unworkable (well...). 
 
@@ -181,3 +180,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 
 9.966u-mods:  
 &nbsp;&nbsp;&nbsp;&nbsp;add support for serial port: it creates a new tty 'tty1' (COM0). 
+
+10.024u-mods:  
+&nbsp;&nbsp;&nbsp;&nbsp;now `print` can use `com0` and `console` to put, not only one. 
+

+ 3 - 0
include/kernel/tty.h

@@ -60,5 +60,8 @@ struct tty {
 /* tty0: system console (0xb8000)  tty1: serial port 0 (if exists) */
 extern struct tty tty0, tty1;
 
+/* point to that tty the print uses. */
+extern struct tty *stdout;
+
 #endif
 

+ 7 - 0
include/modules/serial.h

@@ -33,7 +33,14 @@
 #define serial_info(fmt, ...) printk(yellow, "serial: " fmt, ##__VA_ARGS__)
 
 errno_t serial_init(void);
+
 void com_init(__io_t port);
+
 void serial_write(__io_t port, __uint8_t);
+char serial_read(__io_t port);
+
 int serial_putc(struct tty *, char);
 
+void serial1_interrupt(void);
+void serial0_interrupt(void);
+

+ 2 - 5
init/main.c

@@ -83,8 +83,9 @@ int init(void) {
 	schk_init();
 
 	console_init();
+	serial_init();
 
-	print(red, "XNIX kernel starting...\n");
+	print(red, "\nXNIX kernel starting...\n");
 
 	int_init();
 	mm_init();
@@ -106,10 +107,6 @@ int init(void) {
 
 	time_init();
 
-	serial_init();
-
-	tty_write(&tty1, "hello, world. \n", 15);
-
 	schk_leave();
 
 	return 0;

+ 3 - 0
kernel/console.c

@@ -50,6 +50,9 @@ int nc_putc(struct tty *, char);
 errno_t console_init(void) {
 	schk_init();
 
+	/* stdout will be the tty1 if com0 exists */
+	stdout = &tty0;
+
 	x = bootarg->x, y = bootarg->y;
 
 	/* setup the frame buffer address fb */

+ 10 - 0
kernel/irq.c

@@ -60,6 +60,9 @@ struct irq_entry irq_table[MAX_IRQ_NR];
 void set_irq(int nr, const void *handler) {
 	schk_init();
 
+	if (nr > 16) 
+		panic("set_irq: bad nr. ");
+
 	irq_table[nr].handler = handler;
 
 	schk_leave();
@@ -71,6 +74,12 @@ void set_irq(int nr, const void *handler) {
 void register_irq(int nr, void *entry, const void *handler) {
 	schk_init();
 
+	if (nr > 16) 
+		panic("register_irq: bad nr. ");
+
+	if (irq_table[nr].handler) 
+		return;
+
 	__uint8_t mask;
 	
 	if (nr < 8) {
@@ -173,3 +182,4 @@ void trace_irq(void) {
 
 	return;
 }
+

+ 7 - 0
kernel/sched.c

@@ -419,6 +419,13 @@ void schedule(void) {
 void do_clock(void) {
 	schk_init();
 
+	/* still in the init mode */
+
+	if (!current) {
+		schk_leave();
+		return;
+	}
+
 	current->counter--;
 
 	if (!current->counter) {

+ 17 - 1
kernel/timer.S

@@ -30,7 +30,23 @@ do_timer:
 
 	incq jiffies(%rip)
 
-//	callq do_clock
+	movq jiffies(%rip), %rax
+
+	andq $3, %rax
+	jnz 1f
+
+	xorq %rax, %rax
+
+	call serial_read
+
+	cmpl $0, %eax
+	je 1f
+
+	movq stdout(%rip), %rdi
+	movl %eax, %esi
+	call serial_write
+
+1:	callq do_clock
 
 	ret
 

+ 4 - 1
kernel/tty.c

@@ -39,6 +39,7 @@
 
 
 static char buffer[256];
+struct tty *stdout;
 
 /* read a character from tty (keyboard) with non-cooked */
 
@@ -109,8 +110,10 @@ int printk(__uint8_t color, char *s, ...) {
 }
 
 void print(__uint8_t color, char *s) {
-	while (*s) 
+	while (*s) {
+		stdout->putc(stdout, *s);
 		putc(*s++, color);
+	}
 
 	return;
 }

+ 30 - 18
modules/serial.c

@@ -44,16 +44,14 @@ struct tty tty1;
 
 
 errno_t serial_init(void) {
-	serial_info("COM0: %w  ", bootarg->com0);
-	serial_info("COM1: %w\n", bootarg->com1);
-	serial_info("COM2: %w  ", bootarg->com2);
-	serial_info("COM3: %w\n", bootarg->com3);
-
 	if (bootarg->com0) com_init(bootarg->com0);
 	if (bootarg->com1) com_init(bootarg->com1);
 	if (bootarg->com2) com_init(bootarg->com2);
 	if (bootarg->com3) com_init(bootarg->com3);
 
+	set_irq(3, serial1_interrupt);
+	set_irq(4, serial0_interrupt);
+
 	if (!bootarg->com0) return 0;
 
 	tty1.input_fifo = NULL;
@@ -69,30 +67,25 @@ errno_t serial_init(void) {
 
 	tty0.next = &tty1;
 
+	/* com0 exists, so set it to stdout */
+	stdout = &tty1;
+
+	serial_write(bootarg->com0, serial_read(bootarg->com0));
+
 	return 0;
 }
 
 void com_init(__io_t port) {
-	outb(0x00, port + 1);
-
 	outb(0x80, port + 3);
-	outb(0x03, port + 0);
+	outb(0x30, port + 0);
 	outb(0x00, port + 1);
 
 	outb(0x03, port + 3);
-	outb(0xc7, port + 2);
 	outb(0x0b, port + 4);
-	outb(0x1e, port + 4);
-
-	outb(0xae, port + 0);
+	outb(0x0d, port + 1);
 
-	if (inb(port + 0) != 0xae) {
-		serial_info("com_init: warning: port %w not responed. \n", 
-				port);
-		return;
-	}
+	inb(port + 0);
 
-	outb(0x0f, port + 4);
 	return;
 }
 
@@ -153,3 +146,22 @@ void serial_write(__io_t port, __uint8_t data) {
 
 	return;
 }
+
+char serial_read(__io_t port) {
+	/* zero if no data */
+	return inb(port);
+}
+
+void serial0_interrupt(void) {
+	serial_info("interrupt from serial0. \n");
+
+	return;
+}
+
+void serial1_interrupt(void) {
+	serial_info("interrupt from serial1. \n");
+
+	return;
+}
+
+