Selaa lähdekoodia

8.463u-tty: move tty to main kernel (for posix)

rain 5 vuotta sitten
vanhempi
sitoutus
bb72276384
19 muutettua tiedostoa jossa 265 lisäystä ja 26 poistoa
  1. 1 0
      LINES
  2. 4 4
      Makefile
  3. 4 0
      README.md
  4. 4 3
      include/kernel/console.h
  5. 39 4
      include/kernel/init.h
  6. 38 0
      include/kernel/tty.h
  7. 2 0
      include/kernel/types.h
  8. 8 2
      init/main.c
  9. 1 1
      kernel/Makefile
  10. 82 2
      kernel/console.c
  11. 2 1
      kernel/int.c
  12. 1 1
      kernel/irq.c
  13. 1 1
      kernel/keyboard.c
  14. 1 1
      kernel/mm.c
  15. 1 1
      kernel/panic.c
  16. 3 3
      kernel/sched.c
  17. 1 1
      kernel/sys.c
  18. 71 0
      kernel/tty.c
  19. 1 1
      modules/Makefile

+ 1 - 0
LINES

@@ -35,4 +35,5 @@
 2021-8-13 7.968: 7968 lines, 55 files, rain
 2021-8-14 8.153: 8153 lines, 54 files, rain
 2021-8-14 8.275: 8229 lines, 55 files, rain
+2021-8-15 8.413: 8413 lines, 57 files, rain
 

+ 4 - 4
Makefile

@@ -3,12 +3,12 @@ BOOT_SRCS = boot/boot.asm boot/loader.asm
 BOOT_OBJS = boot/boot.bin boot/loader.bin 
 RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c kernel/rlib/fifo.c 
 RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o kernel/rlib/fifo.o 
-MODULE_SRCS = modules/tty.c  
-MODULE_OBJS = modules/tty.o  
+MODULE_SRCS = 
+MODULE_OBJS = 
 USER_SRCS = user/hello.c
 USER_OBJS = user/hello.o 
-KOBJS = init/head.o init/main.o kernel/int.o kernel/keyboard.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 $(RLIB_OBJS) $(MODULE_OBJS) $(USER_OBJS) 
-KSRCS = init/head.S init/main.c kernel/int.c kernel/keyboard.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) $(USER_SRCS)
+KOBJS = init/head.o init/main.o kernel/int.o kernel/keyboard.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 kernel/tty.o kernel/console.o $(RLIB_OBJS) $(MODULE_OBJS) $(USER_OBJS) 
+KSRCS = init/head.S init/main.c kernel/int.c kernel/keyboard.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 kernel/tty.c kernel/console.c $(RLIB_SRCS) $(MODULE_SRCS) $(USER_SRCS)
 
 all: Makefile
 	make -C tools/

+ 4 - 0
README.md

@@ -144,3 +144,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;add syscall `sys_print` (will remove it later), and now user can call it 
 &nbsp;&nbsp;&nbsp;&nbsp;to put a message on the screen. 
 
+8.463u-tty:  
+&nbsp;&nbsp;&nbsp;&nbsp;move the `tty.c` from module, to the main kernel because we need it, and 
+&nbsp;&nbsp;&nbsp;&nbsp;it's not a device. 
+

+ 4 - 3
include/modules/tty.h → include/kernel/console.h

@@ -24,8 +24,8 @@
 
 
 
-#ifndef _MOD_TTY_H
-#define _MOD_TTY_H
+#ifndef _CONSOLE_H
+#define _CONSOLE_H
 
 
 #include <kernel/types.h>
@@ -34,7 +34,7 @@
 
 #define TAB_SIZE  8
 
-errno_t tty_init(void);
+errno_t console_init(void);
 
 void putc(char, __uint8_t);
 void print(__uint8_t, char *);
@@ -48,6 +48,7 @@ __uint16_t read_cursor(void);
 void roll_screen(void);
 
 extern __uint8_t x, y;
+extern struct tty tty0;
 
 #define gotoxy(__x, __y) x = (__x);y = (__y);update_cursor();
 

+ 39 - 4
include/kernel/init.h

@@ -43,7 +43,7 @@
 /* slowest: schk will display function info in this mode */
 #define DEBUG_LEVEL		TEST_DEBUG_LEVEL
 
-#include <modules/tty.h>
+#include <kernel/console.h>
 
 #define trappoint() \
 	printk(yellow, "system runs at %s:%s.%d\n", __FILE__, __FUNCTION__, __LINE__);
@@ -116,6 +116,21 @@ struct inode_desc {
 
 struct file_opt {
 	/* open a file */
+
+#define O_EXEC 		0x00000001
+#define O_RDONLY 	0x00000002
+#define O_WRONLY 	0x00000004
+#define O_RDWR 		(O_RDONLY | O_WRONLY)
+#define O_SEARCH 	0x00000008
+#define O_APPEND 	0x00000010
+#define O_CREAT 	0x00000020
+#define O_DIRECTORY 	0x00000040
+#define O_EXCL 		0x00000080
+#define O_NOCTTY 	0x00000100
+#define O_NOFOLLOW 	0x00000200
+#define O_NONBLOCK 	0x00000400
+#define O_TRUNC 	0x00000800
+
 	int (* open) (char *, __uint32_t, __uint32_t, errno_t *);
 
 	/* or create one */
@@ -123,9 +138,6 @@ struct file_opt {
 	/* `creat` doesn't return file-descriptor, it returns errno */
 	errno_t (* creat) (char *, __uint32_t, __uint32_t);
 
-	/* `fill` fills a inode descriptor */
-	errno_t (* fill) (char *, __uint32_t, __uint32_t, struct inode_desc *);
-
 	/* read from file-descriptor */
 	errno_t (* read) (int, char *, __uint64_t);
 	
@@ -142,5 +154,28 @@ struct file_opt {
 	void (* close) (int);
 };
 
+
+struct tty {
+	struct fifo *input_fifo;
+
+	/* unreadable (wr-only) */
+#define T_NORD 		0x01
+	/* tty_read returns character / line */
+#define T_COOKED 	0x02
+
+	/* put the input character to screen (in tty_read) */
+#define T_INPUT_BACK	0x04
+
+	__uint8_t mode;
+
+	/* the controller process id */
+	__uint32_t ctl;
+
+	__uint8_t vx, vy;
+
+	int (* putc) (struct tty *, char c);
+};
+
+
 #endif
 

+ 38 - 0
include/kernel/tty.h

@@ -0,0 +1,38 @@
+/* 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 _TTY_H
+#define _TTY_H
+
+#include <kernel/init.h>
+
+int tty_read(struct tty *, char *, size_t);
+int tty_write(struct tty *, char *, size_t);
+
+extern struct tty stdin, stdout, stderr;
+
+#endif
+

+ 2 - 0
include/kernel/types.h

@@ -47,5 +47,7 @@ typedef __ushort_t 	__uint16_t;
 typedef __uint_t 	__uint32_t;
 typedef __ulong_t 	__uint64_t;
 
+typedef __ulong_t 	size_t;
+
 #endif
 

+ 8 - 2
init/main.c

@@ -27,7 +27,7 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
-#include <modules/tty.h>
+#include <kernel/console.h>
 #include <kernel/vsprintf.h>
 #include <kernel/schk.h>
 
@@ -37,7 +37,9 @@
 
 #include <kernel/sched.h>
 #include <kernel/keyboard.h>
+#include <kernel/console.h>
 
+#include <kernel/tty.h>
 #include <lib/string.h>
 #include <sys/syscall.h>
 
@@ -59,15 +61,19 @@ struct bootarg *bootarg = (struct bootarg *) 0x7c00;
  * */
 
 int init(void) {
-	tty_init();
+	console_init();
 
 	print(red, "XNIX kernel starting...\n");
 
+	char buf[256] = {0};
+
 	int_init();
 	mm_init();
 
 	sti();
 
+	tty_read(&stdin, buf, 256);
+
 	task_init();
 
 	printk(white, "total %d devices. \n", pci_lspci());

+ 1 - 1
kernel/Makefile

@@ -1,6 +1,6 @@
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
 
-all: int.o trap.o vsprintf.o irq.o panic.o mm.o pci.o timer.o sched.o sys.o keyboard.o 
+all: int.o trap.o vsprintf.o irq.o panic.o mm.o pci.o timer.o sched.o sys.o keyboard.o tty.o console.o 
 
 
 %.o: %.S

+ 82 - 2
modules/tty.c → kernel/console.c

@@ -27,7 +27,7 @@
 #include <kernel/types.h>
 #include <kernel/errno.h>
 
-#include <modules/tty.h>
+#include <kernel/console.h>
 #include <kernel/vsprintf.h>
 #include <kernel/schk.h>
 
@@ -45,19 +45,99 @@ static int syslog_index = 0;
 static __uint16_t *fb = FB_ADDR;
 __uint8_t x, y;
 
+struct tty tty0;
 
-errno_t tty_init(void) {
+struct tty stdin;
+struct tty stdout;
+struct tty stderr;
+
+
+extern struct fifo kbd_fifo;
+int nc_putc(struct tty *, char);
+
+
+errno_t console_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;
 
+	tty0.input_fifo = NULL;
+	tty0.mode = T_COOKED | T_NORD;
+
+	tty0.ctl = 0;
+	
+	/* this is virtual */
+	tty0.vx = x;
+	tty0.vy = y;
+
+	tty0.putc = nc_putc;
+
+	stdin.input_fifo = &kbd_fifo;
+	stdin.mode = T_COOKED;
+
+	stdin.ctl = 0;
+
+	stdin.vx = x;
+	stdin.vy = y;
+
+	memcpy(&stdout, &tty0, sizeof(struct tty));
+	memcpy(&stdout, &tty0, sizeof(struct tty));
+
 	/* well, this is the end */
 
 	return 0;
 }
 
+int nc_putc(struct tty *tty, char c) {
+	if (c == '\n') {
+		tty->vy++;
+		tty->vx = 0;
+
+		/* no roll screen for tty->vy */
+
+		putc('\n', white);
+
+		return 0;
+	}
+
+	else if (c == '\b') {
+		if (tty->vx) 
+			putc('\b', white);
+
+		else 
+			return 0;
+	}
+
+	else if (c == '\t') {
+		__uint64_t ax = (tty->vx + 3) & ~3;
+
+		if (ax == tty->vx) 
+			tty->vx += TAB_SIZE;
+
+		else 
+			tty->vx = ax;
+
+		putc('\t', white);
+
+		return 0;
+	}
+
+	else if (c == '\r') {
+		tty->vx = 0;
+
+		putc('\r', white);
+
+		return 0;
+	}
+
+	putc(c, white);
+	tty->vx++;
+
+	return 0;
+}
+
 
 void syslog(const char *s) {
 	/* we can't use schk in this function, */

+ 2 - 1
kernel/int.c

@@ -28,11 +28,12 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
-#include <modules/tty.h>
 #include <kernel/int.h>
 #include <kernel/schk.h>
 #include <kernel/mm.h>
 
+#include <kernel/console.h>
+
 #include <arch/x86/int.h>
 #include <arch/io.h>
 #include <arch/sys.h>

+ 1 - 1
kernel/irq.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
-#include <modules/tty.h>
+#include <kernel/console.h>
 #include <kernel/int.h>
 #include <kernel/schk.h>
 

+ 1 - 1
kernel/keyboard.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
-#include <modules/tty.h>
+#include <kernel/console.h>
 #include <lib/string.h>
 #include <lib/fifo.h>
 #include <kernel/schk.h>

+ 1 - 1
kernel/mm.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
-#include <modules/tty.h>
+#include <kernel/console.h>
 #include <kernel/int.h>
 #include <kernel/mm.h>
 

+ 1 - 1
kernel/panic.c

@@ -28,7 +28,7 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
-#include <modules/tty.h>
+#include <kernel/console.h>
 #include <kernel/int.h>
 
 #include <arch/x86/int.h>

+ 3 - 3
kernel/sched.c

@@ -28,14 +28,14 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
-#include <modules/tty.h>
-
 #include <kernel/panic.h>
 #include <kernel/int.h>
 #include <kernel/sched.h>
-#include <kernel/schk.h>
 
+#include <kernel/console.h>
+#include <kernel/schk.h>
 #include <kernel/mm.h>
+
 #include <sys/syscall.h>
 
 #include <arch/x86/int.h>

+ 1 - 1
kernel/sys.c

@@ -29,7 +29,7 @@
 #include <kernel/types.h>
 
 #include <sys/syscall.h>
-#include <modules/tty.h>
+#include <kernel/console.h>
 
 
 void *syscall_table[SYSCALL_NR] = {

+ 71 - 0
kernel/tty.c

@@ -0,0 +1,71 @@
+/* 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/console.h>
+
+#include <lib/fifo.h>
+
+
+/* read a character from tty (keyboard) with non-cooked */
+
+int tty_read(struct tty *tty, char *buf, size_t nbytes) {
+	if (!buf) 
+		return -EINVAL;
+
+	if (!nbytes) 
+		return 0;
+
+	/* it's read-only */
+	if (!tty->input_fifo) 
+		/* read from stdout, or stderr */
+		return -EINVAL;
+
+	return -EIO;
+}
+
+
+int tty_write(struct tty *tty, char *buf, size_t nbytes) {
+	if (!buf) 
+		return -EINVAL;
+
+	if (!nbytes) 
+		return 0;
+
+	int i;
+
+	for (i = 0; i < nbytes; i++) {
+		if (!buf[i]) 
+			return i;
+
+		tty->putc(tty, buf[i]);
+	}
+
+	return i;
+}
+

+ 1 - 1
modules/Makefile

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