From: acevest Date: Tue, 26 Sep 2023 15:08:17 +0000 (+0800) Subject: 添加对串口输出的能力 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=9a5832659f2bcbd56363f9b49e6a671d364638ed;p=kernel.git 添加对串口输出的能力 --- diff --git a/Makefile b/Makefile index be70137..2779a38 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ OBJS := $(patsubst %,%.o,$(SOURCE_FILES)) ${KERNELBIN}: ${OBJS} ${LD} -z noexecstack -m elf_i386 -M -T$(LINKSCRIPT) $(OBJS) -o $@ > $(SYSTEMMAP) nm -a $@ > kernel.sym - #rm kernel/setup.c.o + rm kernel/setup.c.o %.S.o: %.S ${HEADER_FILES} ${CC} ${CFLAGS} $< -o $@ diff --git a/boot/boot.c b/boot/boot.c index be40f97..339d744 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -47,6 +47,7 @@ void boot_delay(int ticks) { #endif } +void init_serial(); void init_ttys(); void setup_gdt(); void setup_idt(); @@ -56,6 +57,8 @@ void setup_i8253(uint16_t); void setup_boot_irqs(); void check_kernel(unsigned long addr, unsigned long magic) { + init_serial(); + init_ttys(); printk("setup gdt\n"); diff --git a/drivers/ide.c b/drivers/ide.c index 1ea6430..85284ef 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -109,7 +109,7 @@ void init_pci_controller(unsigned int classcode) { } assert(pci->intr_line < 16); if (pci != 0 && pci->intr_line < 16) { - printk("found pci %03d:%02d.%d #%02d %04X:%04X ProgIF %02x %s\n", pci->bus, pci->dev, pci->devfn, + printk("found pci %03d:%02d.%d #%02d %04X:%04X progif %02x %s\n", pci->bus, pci->dev, pci->devfn, pci->intr_line, pci->vendor, pci->device, pci->progif, pci_get_info(pci->classcode, pci->progif)); // printk("found pci vendor %04x device %04x class %04x intr %d progif: %x\n", pci->vendor, pci->device, // pci->classcode, pci->intr_line, pci->progif); diff --git a/drivers/serial.c b/drivers/serial.c new file mode 100644 index 0000000..b848681 --- /dev/null +++ b/drivers/serial.c @@ -0,0 +1,45 @@ +/* + * ------------------------------------------------------------------------ + * File Name: serial.c + * Author: Zhao Yanbai + * 2023-09-26 21:03:27 Tuesday CST + * Description: none + * ------------------------------------------------------------------------ + */ + +#include "io.h" + +#define COMM1_PORT 0x3F8 +#define COMM2_PORT 0x2F8 +#define COMM3_PORT 0x3E8 +#define COMM4_PORT 0x2F8 +#define COMM5_PORT 0x5F8 +#define COMM6_PORT 0x4F8 +#define COMM7_PORT 0x5E8 +#define COMM8_PORT 0x4E8 + +#define SERIAL_PORT COMM1_PORT + +void init_serial() { + uint32_t port = SERIAL_PORT; + outb(0x00, port + 1); // 禁用中断 + outb(0x80, port + 3); + outb(115200 / 9600, port + 0); + outb(0x00, port + 1); + outb(0x03, port + 3); + outb(0xC7, port + 2); + outb(0x0B, port + 4); +} + +void serial_putc(char c) { + while ((inb(SERIAL_PORT + 5) & 0x20) == 0) { + } + outb(c, SERIAL_PORT); +} + +void serial_write(const char *buf, size_t size) { + // return 0; + for (size_t i = 0; i < size; i++) { + serial_putc(buf[i]); + } +} diff --git a/gdbscript b/gdbscript index 238cb70..9dd6925 100644 --- a/gdbscript +++ b/gdbscript @@ -13,10 +13,12 @@ target remote localhost:1234 set pagination off -b init_system_info +#b init_serial + +#b init_system_info # finish 可以执行到当前函数返回处, 缩写 fin -b setup_pci +#b setup_pci #b setup_kernel #b e820_init_bootmem_data diff --git a/kernel/printk.c b/kernel/printk.c index d2c5d1a..6a0904d 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -20,6 +20,8 @@ #include int vsprintf(char *buf, const char *fmt, char *args); +void serial_write(const char *buf, size_t size); + char pkbuf[1024]; extern tty_t *const default_tty; int printk(const char *fmtstr, ...) { @@ -28,6 +30,7 @@ int printk(const char *fmtstr, ...) { char *args = (char *)(((char *)&fmtstr) + 4); int size = vsprintf(pkbuf, fmtstr, args); tty_write(default_tty, pkbuf, (size_t)size); + serial_write(pkbuf, (size_t)size); irq_restore(iflags); return 0; } @@ -40,6 +43,7 @@ int printd(const char *fmtstr, ...) { char *args = (char *)(((char *)&fmtstr) + 4); int size = vsprintf(pdbuf, fmtstr, args); tty_write(debug_tty, pdbuf, (size_t)size); + serial_write(pdbuf, (size_t)size); irq_restore(iflags); return 0; } diff --git a/qemu.sh b/qemu.sh index ac37fed..507dba6 100755 --- a/qemu.sh +++ b/qemu.sh @@ -9,6 +9,7 @@ qemu-system-i386 \ -device ich9-ahci,id=ahci \ & +# -serial tcp::8888,server,nowait \ # -device ich9-ahci,id=ahci \ # -machine accel=tcg \ # -serial stdio \