From 9a05fbf384da497feb3b81fedb82c13d43a6f5da Mon Sep 17 00:00:00 2001 From: rain Date: Sat, 13 Mar 2021 18:55:08 +0800 Subject: [PATCH] make assembly macros --- LINES | 2 ++ README.md | 3 +++ include/arch/sys.h | 44 ++++++++++++++++++++++++++++++++++++ include/kernel/init.h | 4 ++++ include/kernel/print.h | 2 +- include/kernel/vgatext.h | 3 +++ kernel/Makefile | 2 +- kernel/hal/vgatext/vgatext.c | 8 ++++--- kernel/init/Makefile | 2 +- kernel/init/main.c | 9 +++++--- kernel/init/print.c | 4 ++-- 11 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 include/arch/sys.h diff --git a/LINES b/LINES index da28e38..f016ac9 100644 --- a/LINES +++ b/LINES @@ -26,3 +26,5 @@ 2021-3-12 1.384: 1384 lines, 20 files, rain +2021-3-13 1.440: 1440 lines, 21 files, rain + diff --git a/README.md b/README.md index 1582b5b..c4da77d 100644 --- a/README.md +++ b/README.md @@ -58,3 +58,6 @@ Cunix kernel `1.384s-hal`:     use text VGA mode, and make a file-operations of it. +`1.440s-lib`: +    make some macros about assembly + diff --git a/include/arch/sys.h b/include/arch/sys.h new file mode 100644 index 0000000..717202f --- /dev/null +++ b/include/arch/sys.h @@ -0,0 +1,44 @@ +/* Copyright (C) 2021 Rain */ + +/* This file is part of Cunix. */ + +/* + Cunix 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. +*/ + +/* + Cunix 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 Cunix. If not, see . +*/ + + + +#ifndef INCLUDED_SYS_H +#define INCLUDED_SYS_H + +#include + + +#define sgdt(addr) __asm__ __volatile__ ("sgdt %0" : "=m" (addr) : :) +#define lgdt(addr) __asm__ __volatile__ ("lgdt %0" : : "m" (addr)) + +#define sidt(addr) __asm__ __volatile__ ("sidt %0" : "=m" (addr) : :) +#define lidt(addr) __asm__ __volatile__ ("lidt %0" : : "m" (addr)) + + +#define pause() __asm__ __volatile__ ("pause"); +#define nop() __asm__ __volatile__ ("nop"); +#define hlt() __asm__ __volatile__ ("hlt"); + + +#endif diff --git a/include/kernel/init.h b/include/kernel/init.h index cb2130b..0f2c2b6 100644 --- a/include/kernel/init.h +++ b/include/kernel/init.h @@ -33,6 +33,10 @@ +#define KERNEL_CS 0x0008 +#define KERNEL_DS 0x0010 + + struct inode_desc { /* this struct is a general descriptor, so we must know * how big is it. */ diff --git a/include/kernel/print.h b/include/kernel/print.h index 90a2148..7c79b98 100644 --- a/include/kernel/print.h +++ b/include/kernel/print.h @@ -33,7 +33,7 @@ #include -__uint32_t print(struct vga_inode *inode, char *s); +__uint32_t print(char *s); #endif diff --git a/include/kernel/vgatext.h b/include/kernel/vgatext.h index 18473ae..d1135c3 100644 --- a/include/kernel/vgatext.h +++ b/include/kernel/vgatext.h @@ -40,6 +40,7 @@ extern struct file_operations vga; + struct vga_inode { struct inode_desc inode; @@ -55,5 +56,7 @@ struct vga_inode { __uint32_t x, y; }; +extern struct vga_inode stdout; + #endif diff --git a/kernel/Makefile b/kernel/Makefile index 210d2da..d3cd489 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -4,7 +4,7 @@ default: make -C lib/ make kernel.bin -kernel.o: init/main.o hal/vgatext/vgatext.o lib/bitmap.o lib/bitman.o init/print.o +kernel.o: init/main.o hal/vgatext/vgatext.o lib/bitmap.o lib/bitman.o init/print.o ld -b elf64-x86-64 -o kernel.o init/main.o init/print.o hal/vgatext/vgatext.o lib/bitmap.o lib/bitman.o -T kernel.lds kernel.bin: kernel.o diff --git a/kernel/hal/vgatext/vgatext.c b/kernel/hal/vgatext/vgatext.c index 631ce1c..0610828 100644 --- a/kernel/hal/vgatext/vgatext.c +++ b/kernel/hal/vgatext/vgatext.c @@ -38,6 +38,8 @@ __uint32_t next_fd = 1; +struct vga_inode stdout; + struct inode_desc *vga_open(char *name, __uint32_t type, __uint32_t mode, errno_t *errno) { *errno = -E_NOSUPP; @@ -137,9 +139,9 @@ errno_t vga_write(struct vga_inode *inode, char *buffer, __uint64_t l) { } -errno_t vga_lseek(struct inode_desc *inode, __uint32_t off, __uint32_t seg) { - return -E_HNOSUP; -} +extern errno_t vga_lseek(struct inode_desc *inode, __uint32_t off, __uint32_t seg); + +__asm__ ("vga_lseek: ret\n"); void vga_close(struct inode_desc *inode) { diff --git a/kernel/init/Makefile b/kernel/init/Makefile index f6c63cb..a5b9966 100644 --- a/kernel/init/Makefile +++ b/kernel/init/Makefile @@ -1,6 +1,6 @@ C_FLAGS = -mcmodel=large -fno-builtin -m64 -I ../../include/ -all: main.o print.o +all: main.o print.o %.o: %.c gcc -c $*.c $(C_FLAGS) diff --git a/kernel/init/main.c b/kernel/init/main.c index e30578b..9ebd896 100644 --- a/kernel/init/main.c +++ b/kernel/init/main.c @@ -28,13 +28,16 @@ #include #include +#include #include #include + + + void init(void) { - struct vga_inode vi; - vga.fill(NULL, 0, 0, &vi); - print(&vi, "kernel starting\n"); + vga.fill(NULL, 0, 0, &stdout); + print("kernel starting\n"); for (;;); } diff --git a/kernel/init/print.c b/kernel/init/print.c index d65241a..b9ad709 100644 --- a/kernel/init/print.c +++ b/kernel/init/print.c @@ -31,9 +31,9 @@ #include -errno_t print(struct vga_inode *inode, char *s) { +errno_t print(char *s) { for (; *s; s++) { - vga.write(inode, s, 1); + vga.write(&stdout, s, 1); } return 0; -- GitLab