Browse Source

8.275u-user: put a message by sys_print (user)

rain 5 năm trước cách đây
mục cha
commit
f7252422bc
11 tập tin đã thay đổi với 116 bổ sung và 21 xóa
  1. 1 0
      LINES
  2. 8 4
      Makefile
  3. 4 0
      README.md
  4. 12 6
      include/sys/syscall.h
  5. 0 5
      init/main.c
  6. 1 1
      kernel/int.c
  7. 7 4
      kernel/sched.c
  8. 20 0
      kernel/sys.c
  9. 1 1
      modules/Makefile
  10. 10 0
      user/Makefile
  11. 52 0
      user/hello.c

+ 1 - 0
LINES

@@ -34,4 +34,5 @@
 2021-8-13 7.937: 7937 lines, 55 files, rain
 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
 

+ 8 - 4
Makefile

@@ -3,10 +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 modules/ide.c 
-MODULE_OBJS = modules/tty.o modules/ide.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)
-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)
+MODULE_SRCS = modules/tty.c  
+MODULE_OBJS = modules/tty.o  
+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)
 
 all: Makefile
 	make -C tools/
@@ -15,6 +17,7 @@ all: Makefile
 	make -C modules/
 	make -C kernel/ 
 	make -C kernel/rlib 
+	make -C user/ 
 	make xnix.img
 	make vmxnix
 
@@ -42,6 +45,7 @@ clear:
 	make -C boot/ clear
 	make -C init/ clear
 	make -C kernel/ clear
+	make -C user/ clear
 	make -C tools/ clear
 	rm xnix.img kbinary kernel.o vmxnix 
 	make -C modules/ clear

+ 4 - 0
README.md

@@ -140,3 +140,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 8.153u-user:  
 &nbsp;&nbsp;&nbsp;&nbsp;now we can run in the user-mode. 
 
+8.229u-user:  
+&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. 
+

+ 12 - 6
include/sys/syscall.h

@@ -35,15 +35,21 @@
 
 extern int no_syscall(void);
 extern int do_exit(__uint64_t);
+extern int sys_print(char *);
 
 
-void *syscall_table[SYSCALL_NR] = {
-	[0 ... SYSCALL_NR - 1] = no_syscall, 
+#define do_call_1(nr, a) 		\
+	__asm__ __volatile__ (		\
+		"int $0x80\n\t"		\
+		:: "a" (nr), 		\
+		   "b" ((__uint64_t) a)	\
+	)
 
-#define mksyscall(fn, nr) [nr] = fn, 
-
-	mksyscall(do_exit, 0)
-};
+#define do_call_0(nr) 		\
+	__asm__ __volatile__ (	\
+		"int $0x80\n\t"	\
+		:: "a" (nr)	\
+	)
 
 
 #endif

+ 0 - 5
init/main.c

@@ -28,8 +28,6 @@
 #include <kernel/types.h>
 
 #include <modules/tty.h>
-#include <modules/ide.h>
-
 #include <kernel/vsprintf.h>
 #include <kernel/schk.h>
 
@@ -71,9 +69,6 @@ int init(void) {
 	sti();
 
 	task_init();
-	kbd_init();
-
-	ide_init();
 
 	printk(white, "total %d devices. \n", pci_lspci());
 

+ 1 - 1
kernel/int.c

@@ -101,7 +101,7 @@ int int_init(void) {
 	register_irq(0x00, irq0x20_interrupt, do_timer);
 	register_irq(0x01, irq0x21_interrupt, do_keyboard);
 
-	set_int_gate(idt_table + 0x80, 1, &syscall);
+	set_sys_gate(idt_table + 0x80, 1, &syscall);
 
 	schk_leave();
 

+ 7 - 4
kernel/sched.c

@@ -36,6 +36,8 @@
 #include <kernel/schk.h>
 
 #include <kernel/mm.h>
+#include <sys/syscall.h>
+
 #include <arch/x86/int.h>
 #include <arch/io.h>
 #include <arch/sys.h>
@@ -56,6 +58,8 @@ struct task *current;
 
 struct task *tasks[MAX_TASKS] = {0};
 
+/* user/hello.c */
+extern __uint64_t utask_a(__uint64_t);
 
 
 int do_exit(__uint64_t exitcode) {
@@ -64,10 +68,6 @@ int do_exit(__uint64_t exitcode) {
 	for (;;);
 }
 
-__uint64_t utask_a(__uint64_t arg) {
-	for (;;);
-}
-
 
 int task_init(void) {
 	/* initial the init-tss */
@@ -379,6 +379,9 @@ void schedule(void) {
 			}
 		}
 	}
+	
+	if (current->pid == bestpid) 
+		return;
 
 	/* will sti() in switch_to() */
 

+ 20 - 0
kernel/sys.c

@@ -28,13 +28,33 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
+#include <sys/syscall.h>
 #include <modules/tty.h>
 
 
+void *syscall_table[SYSCALL_NR] = {
+	[0 ... SYSCALL_NR - 1] = no_syscall, 
+
+#define mksyscall(fn, nr) [nr] = fn, 
+
+	mksyscall(do_exit, 1)
+	mksyscall(sys_print, 2)
+};
+
+
 int no_syscall(void) {
 	printk(yellow, "no such system call. ");
 
 	return -ENOSYS;
 }
 
+int sys_print(char *s) {
+	printk(yellow, "sys_print: s = %q\n", s);
 
+	if (!s) 
+		return -EINVAL;
+
+	print(yellow, s);
+
+	return 0;
+}

+ 1 - 1
modules/Makefile

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

+ 10 - 0
user/Makefile

@@ -0,0 +1,10 @@
+CFLAGS = -fno-builtin -m64 -I ../include/ -Wall -Werror -g -Wno-unused-variable
+
+all: hello.o
+
+%.o: %.c
+	gcc -c $*.c -o $*.o $(CFLAGS)
+
+clear:
+	rm *.o 
+

+ 52 - 0
user/hello.c

@@ -0,0 +1,52 @@
+/* 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 <sys/syscall.h>
+
+
+static char msg[] = "hello, world";
+
+
+__uint64_t utask_a(__uint64_t arg) {
+	__uint64_t addr;
+
+	/* we must use this, because &msg will returns the 
+	 * msg(%rip) (0x800000 based), but msg is in the 
+	 * .data section. 
+	 * */
+
+	__asm__ __volatile__ (
+		"leaq msg, %0\n\t"
+		: "=r" (addr)
+	);
+
+	do_call_1(2, addr);
+	do_call_0(1);
+
+	/* it won't here */
+	for (;;);
+}
+
+