Forráskód Böngészése

8.153u-user: run in the user mode

rain 5 éve
szülő
commit
199fa7c3d5
17 módosított fájl, 292 hozzáadás és 103 törlés
  1. 1 0
      LINES
  2. 2 2
      Makefile
  3. 3 0
      README.md
  4. 2 0
      include/arch/io.h
  5. 1 1
      include/arch/x86/paging.h
  6. 2 2
      include/kernel/init.h
  7. 2 0
      include/kernel/mm.h
  8. 132 0
      include/kernel/pci.h
  9. 4 0
      include/kernel/sched.h
  10. 0 88
      include/modules/ide.h
  11. 7 1
      init/main.c
  12. 26 0
      kernel/int.c
  13. 3 1
      kernel/keyboard.c
  14. 23 4
      kernel/mm.c
  15. 45 0
      kernel/pci.c
  16. 38 3
      kernel/sched.c
  17. 1 1
      modules/Makefile

+ 1 - 0
LINES

@@ -33,4 +33,5 @@
 2021-8-13 7.783: 7783 lines, 55 files, rain
 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
 

+ 2 - 2
Makefile

@@ -3,8 +3,8 @@ 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 = 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)
 

+ 3 - 0
README.md

@@ -137,3 +137,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 7.968u-pci:  
 &nbsp;&nbsp;&nbsp;&nbsp;add the function `pci_lspci()` to scan all devices. 
 
+8.153u-user:  
+&nbsp;&nbsp;&nbsp;&nbsp;now we can run in the user-mode. 
+

+ 2 - 0
include/arch/io.h

@@ -50,6 +50,8 @@ typedef __uint16_t port_t;
 	_r; \
 })
 
+#define insw(port, buf, c) __asm__ volatile ("cld;rep;insw;"::"d" (port), "D" (buf), "c" (c))
+
 
 #define outb(value, port) \
 	__asm__ volatile ("outb %%al, %%dx" :: "a" (value), "d" (port));

+ 1 - 1
include/arch/x86/paging.h

@@ -50,7 +50,7 @@
 #define PATT_KERNEL (PAGE_PRESENT | PAGE_RDWR | PAGE_PAT)
 
 /* 0x07 */
-#define PATT_USER   (PAGE_PRESENT | PAGE_RDWR | PAGE_USER)
+#define PATT_USER   (PAGE_PRESENT | PAGE_RDWR | PAGE_USER | PAGE_PAT)
 
 #define PAGE_TABLE_BASE (0x0000000000000000)
 

+ 2 - 2
include/kernel/init.h

@@ -66,8 +66,8 @@
 #define KERNEL_CS 0x0008
 #define KERNEL_DS 0x0010
 
-#define USER_CS   0x0018
-#define USER_DS   0x0020
+#define USER_CS   0x001b
+#define USER_DS   0x0023
 
 
 #define SEEK_SET 0x0001

+ 2 - 0
include/kernel/mm.h

@@ -41,6 +41,7 @@ extern void *_end;
 
 void mm_init(void);
 void *alloc_page(void);
+void *alloc_page_att(__uint64_t);
 void free_page(void *);
 
 
@@ -133,6 +134,7 @@ struct mempool {
 #define POOL_ENTRY_SHIFT 6ul
 
 extern struct mempool pool;
+extern struct mempool upool;
 
 void *kmalloc(struct mempool *, __uint64_t);
 void kfree(struct mempool *, void *, __uint64_t);

+ 132 - 0
include/kernel/pci.h

@@ -33,6 +33,7 @@
 
 __uint32_t pci_read_config(__uint8_t, __uint8_t, __uint8_t, __uint8_t);
 __uint32_t pci_lspci(void);
+void pci_infodev(__uint8_t, __uint8_t, __uint8_t);
 
 
 struct pci_device {
@@ -45,5 +46,136 @@ struct pci_device {
 	char *config;
 };
 
+/* the general pci header */
+
+struct pci_header {
+	__uint16_t vendor;
+	__uint16_t dev;
+
+	__uint16_t cmd;
+	__uint16_t status;
+
+	__uint8_t revision;
+	__uint8_t prof_if;
+
+	__uint8_t subclass;
+	__uint8_t class_code;
+
+	__uint8_t cache_size;
+	__uint8_t latency;
+
+	__uint8_t hdr_type;
+	__uint8_t bist;
+} __attribute__ ((packed));
+
+
+/* regular device */
+
+struct pci_header0 {
+	struct pci_header hdr;
+
+	__uint32_t bar0, bar1, bar2, 
+		   bar3, bar4, bar5;
+
+	/* a pointer that points to the info of the card (?) */
+	__uint32_t cardbus_cis;
+	__uint16_t subsys_vendor;
+	__uint16_t subsys;
+
+	/* extend rom base */
+	__uint32_t erom_base;
+	/* ? */
+	__uint8_t capabilities;
+	__uint16_t rsvd0;
+	__uint8_t rsvd1;
+
+	__uint32_t rsvd2;
+
+	__uint8_t irqnr;
+
+	/* it looks only for apic :-( */
+	__uint8_t intpin;
+
+	/* ? */
+	__uint8_t min_grant;
+	__uint8_t max_latency;
+} __attribute__ ((packed));
+
+/* pci-pci bridge */
+
+struct pci_header1 {
+	struct pci_header hdr;
+	__uint32_t bar0, bar1;
+
+	__uint8_t busnr_primary;
+	__uint8_t busnr_secondary;
+	__uint8_t busnr_sub;
+	__uint8_t second_latency;
+
+	__uint8_t io_base, io_limit;
+	__uint16_t mem_limit;
+
+	__uint16_t pre_mem_base;
+	__uint16_t pre_mem_limit;
+
+	/* for 48-bit long-mode */
+	__uint32_t pre_base_high;
+	__uint32_t pre_base_limit;
+
+	__uint16_t io_high;
+	__uint16_t io_limit_high;
+
+	__uint8_t capabilities;
+	__uint16_t rsvd0;
+	__uint8_t rsvd1;
+
+	__uint32_t erom_base;
+
+	__uint8_t irqnr;
+	__uint8_t intpin;
+
+	__uint16_t bridge_ctl;
+} __attribute__ ((packed));
+
+/* pci-cardbus bridge */
+
+struct pci_header2 {
+	struct pci_header hdr;
+
+	__uint32_t cardbus_base;
+
+	__uint8_t caplist_offset;
+	__uint8_t rsvd0;
+	__uint16_t second_status;
+
+	__uint8_t pcibus_nr;
+	__uint8_t cardbus_nr;
+	__uint8_t sub_bus_nr;
+	__uint8_t latency;
+
+	__uint32_t mmbase_0;
+	__uint32_t mmlimit_0;
+
+	__uint32_t mmbase_1;
+	__uint32_t mmlimit_1;
+
+	__uint32_t iobase_0;
+	__uint32_t iolimit_0;
+
+	__uint32_t iobase_1;
+	__uint32_t iolimit_1;
+
+	__uint8_t irqnr;
+	__uint8_t intpin;
+
+	__uint16_t bridge_ctl;
+
+	__uint16_t subsys_dev;
+	__uint16_t subsys_vendor;
+
+	__uint32_t lagency_base;
+} __attribute__ ((packed));
+
+
 #endif
 

+ 4 - 0
include/kernel/sched.h

@@ -256,6 +256,9 @@ struct registers {
 	__uint64_t rbx, rdx, rcx, rax;
 
 	/* user task will push this when interrupt happens (for iretq) */
+	/* and iretq need these, so this.rip should be the entry point of the user
+	 * task (a user function), not the ret_intr. 
+	 * */
 	__uint64_t rip;
 	__uint64_t cs;
 
@@ -272,6 +275,7 @@ struct registers {
 /* kernel/sched.c */
 void ktask_entry(void);
 int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t);
+int utask_create(__uint64_t (* entry) (__uint64_t), __uint64_t);
 int do_fork(struct registers *, __uint64_t);
 
 

+ 0 - 88
include/modules/ide.h

@@ -1,88 +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 _MOD_IDE_H
-#define _MOD_IDE_H
-
-
-#include <kernel/types.h>
-#include <kernel/errno.h>
-
-errno_t ide_init(void);
-extern void do_ide_intr(void);
-void unexcepted_ide_intr(void);
-void ide_stat(void);
-
-
-#define IDE_CMD_ID 	0xec
-#define IDE_CMD_RD 	0x20
-#define IDE_CMD_RD48 	0x24
-#define IDE_CMD_WR 	0x30
-#define IDE_CMD_WR48 	0x34
-
-#define DISK0_DATA 	0x1f0
-#define DISK0_ERR	0x1f1
-#define DISK0_SECTORS 	0x1f2
-#define DISK0_ADDR_LOW	0x1f3
-#define DISK0_ADDR_MID	0x1f4
-#define DISK0_ADDR_HIGH 0x1f5
-#define DISK0_MODE 	0x1f6
-#define DISK0_CMD 	0x1f7
-
-#define DISK0_CTL_STAT	0x3f6
-
-#define DISK1_DATA 	0x170
-#define DISK1_ERR 	0x171
-#define DISK1_SECTORS 	0x172
-#define DISK1_ADDR_LOW 	0x173
-#define DISK1_ADDR_MID 	0x174
-#define DISK1_ADDR_HIGH 0x175
-#define DISK1_MODE	0x176
-#define DISK1_CMD	0x177
-
-#define DISK1_CTL_STAT 	0x376
-
-#define STAT_BUSY 	0x80
-#define STAT_READY 	0x40
-#define STAT_REQ 	0x08
-#define STAT_ERR 	0x01
-
-/* enable the irq...but 0 is enable, 1 is disable */
-#define CTL_IRQ		0x02
-#define CTL_RESET 	0x04
-
-/* irq 14 and 15 */
-#define DISK0_INT_NR 	0x2e
-#define DISK1_INT_NR 	0x2f
-
-#define DISK0_IRQ_NR 	14
-#define DISK1_IRQ_NR 	15
-
-#define MODE_DISK1 	0x10
-#define MODE_LBA 	0x40
-
-#endif
-

+ 7 - 1
init/main.c

@@ -28,6 +28,8 @@
 #include <kernel/types.h>
 
 #include <modules/tty.h>
+#include <modules/ide.h>
+
 #include <kernel/vsprintf.h>
 #include <kernel/schk.h>
 
@@ -66,10 +68,12 @@ int init(void) {
 	int_init();
 	mm_init();
 
+	sti();
+
 	task_init();
 	kbd_init();
 
-	sti();
+	ide_init();
 
 	printk(white, "total %d devices. \n", pci_lspci());
 
@@ -81,6 +85,8 @@ int init(void) {
 	else 
 		printk(white, "floppy / flash memory / cd-rom %b. \n", bootarg->bootdev);
 
+	pci_infodev(0, 0x1f, 2);
+
 	for (;;) 
 		put_keycode();
 

+ 26 - 0
kernel/int.c

@@ -313,6 +313,32 @@ void do_gp(const __uint64_t *rsp, __uint64_t errcode) {
 void do_page_fault(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
+	printk(red, "error code: %q\n", errcode);
+
+	if (errcode & 1) 
+		print(red, "page protection. \n");
+	else 
+		print(red, "page not exists. \n");
+
+	if (errcode & 2) 
+		print(red, "wr ");
+	else 
+		print(red, "rd ");
+
+	if (errcode & 4) 
+		print(red, "u ");
+	else 
+		print(red, "k ");
+
+	if (errcode & 8) 
+		print(red, "rsvd set ");
+
+	if (errcode & 16) 
+		print(red, "instruction");
+
+	putc('\n', red);
+
+
 	die("page fault", rsp, 14);
 
 	schk_leave();

+ 3 - 1
kernel/keyboard.c

@@ -114,7 +114,9 @@ void put_keycode(void) {
 	i = fifo_get(&kbd_fifo);
 
 	if (!i) 
-		return;
+		while (!kbd_fifo.need_read)
+			hlt();
+	
 
 	if (i == EXT) {
 		ext = 1;

+ 23 - 4
kernel/mm.c

@@ -45,7 +45,7 @@
 
 struct sysmm *mm;
 
-struct mempool pool;
+struct mempool pool, upool;
 
 
 void mm_init(void) {
@@ -163,7 +163,22 @@ void mm_init(void) {
 
 	pool.page = alloc_page();
 
-	pool.next = NULL;
+	upool.size = 0x200000;
+	upool.used = 0;
+
+	upool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
+
+	upool.bitmap = (__uint8_t *) kmalloc(&pool, 4096);
+
+	if (!upool.bitmap) 
+		panic("mm_init: fatal allocate space for upool. ");
+
+	memset(upool.bitmap, 0, 0x200000 / 64 / 8);
+
+	upool.page = alloc_page_att(PATT_USER);
+
+	upool.next = NULL;
+	pool.next = &upool;
 
 	return;
 
@@ -192,8 +207,12 @@ void *mapping(void *addr, __uint64_t attr) {
 }
 
 
-
 void *alloc_page(void) {
+	return alloc_page_att(PATT_KERNEL);
+}
+
+
+void *alloc_page_att(__uint64_t attr) {
 	__uint64_t i;
 
 	for (i = 0; bitmap_read(mm->bitmap, i); i++) {
@@ -205,7 +224,7 @@ void *alloc_page(void) {
 
 	/* we must map it to page-table */
 
-	void *addr = mapping((void *) (i << PAGE_SHIFT), PATT_KERNEL);
+	void *addr = mapping((void *) (i << PAGE_SHIFT), attr);
 
 	if (!addr) {
 		bitmap_set(mm->bitmap, i, 0);

+ 45 - 0
kernel/pci.c

@@ -27,7 +27,10 @@
 #include <kernel/init.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
+
 #include <kernel/int.h>
+#include <kernel/pci.h>
+#include <lib/string.h>
 
 #include <arch/x86/int.h>
 #include <arch/io.h>
@@ -67,3 +70,45 @@ __uint32_t pci_lspci(void) {
 
 	return devs;
 }
+
+
+void pci_infodev(__uint8_t bus, __uint8_t slot, __uint8_t func) {
+	__uint32_t buf[256];
+	struct pci_header header;
+	int i;
+
+	for (i = 0; i < 256 / 4; i++) 
+		buf[i] = pci_read_config(bus, slot, func, i << 2);
+
+	memcpy((char *) &header, (char *) buf, sizeof(struct pci_header));
+
+	/* put the info of this device on the screen */
+	printk(yellow, "vendor id: %w\n", header.vendor);
+	printk(yellow, "device id: %w\n", header.dev);
+
+	printk(yellow, "revision: %b\n", header.revision);
+	printk(yellow, "sub class id: %b\n", header.subclass);
+	printk(yellow, "class code: %b\n", header.class_code);
+
+	printk(yellow, "cache line size: %b\n", header.cache_size);
+	printk(yellow, "latency: %b\n", header.latency);
+
+	printk(yellow, "header type: %b\n", header.hdr_type);
+
+	if (header.hdr_type == 0) {
+		struct pci_header0 *hdr = (struct pci_header0 *) buf;
+
+		printk(yellow, "bar0: %x  ", hdr->bar0);
+		printk(yellow, "bar1: %x\n", hdr->bar1);
+
+		printk(yellow, "bar2: %x  ", hdr->bar2);
+		printk(yellow, "bar3: %x\n", hdr->bar3);
+
+		printk(yellow, "bar4: %x  ", hdr->bar4);
+		printk(yellow, "bar5: %x\n", hdr->bar5);
+
+		printk(yellow, "irq: %b\n", hdr->irqnr);
+	}
+
+	return;
+}

+ 38 - 3
kernel/sched.c

@@ -64,6 +64,10 @@ int do_exit(__uint64_t exitcode) {
 	for (;;);
 }
 
+__uint64_t utask_a(__uint64_t arg) {
+	for (;;);
+}
+
 
 int task_init(void) {
 	/* initial the init-tss */
@@ -77,6 +81,18 @@ int task_init(void) {
 
 	current = &init_task;
 
+	/* this page (what place what we are) is k-only, so allocate a
+	 * new page for user. 
+	 * */
+
+	void *p = alloc_page_att(PATT_USER);
+	memcpy(p, (char *) utask_a, 1024);
+
+	printk(white, "new page for utask_a: %q\n", p);
+
+	utask_create(p, 0x12345678);
+	switch_to(tasks[0]);
+
 	return 0;
 }
 
@@ -153,6 +169,18 @@ int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
 	return do_fork(&regs, 0);
 }
 
+int utask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
+	struct registers regs;
+
+	memset(&regs, 0x00, sizeof(regs));
+
+	regs.rip = (__uint64_t) entry;
+	regs.rdi = arg;
+
+	return do_fork(&regs, 3);
+}
+
+
 
 static __uint32_t find_free_pid(void) {
 	__uint32_t freepid;
@@ -179,8 +207,14 @@ static __uint32_t find_free_pid(void) {
  * */
 
 int do_fork(struct registers *regs, __uint64_t dpl) {
-	__uint64_t start_stack = (__uint64_t) kmalloc(&pool, 65536);
-	
+	__uint64_t start_stack;
+
+	if (!dpl) 
+		start_stack = (__uint64_t) kmalloc(&pool, 65536);
+
+	else 
+		start_stack = (__uint64_t) kmalloc(&upool, 65536);
+
 	if (!start_stack) 
 		return -ENOMEM;
 
@@ -261,7 +295,8 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 		regs->ss = regs->fs = regs->es = regs->ds = regs->gs = USER_DS;
 		regs->cs = USER_CS;
 		regs->rflags = 1 << 9;
-		regs->rip = (__uint64_t) ret_intr;
+		/* set from the utask_create */
+//		regs->rip = ;
 		regs->rsp = start_stack - sizeof(struct registers);
 	}
 

+ 1 - 1
modules/Makefile

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