diff --git a/LINES b/LINES index 13b49a6f2fda8f2dc8ca1dbe50f8672a39b22a09..da28e3865a60695fa285886a3681918b5fbc332b 100644 --- a/LINES +++ b/LINES @@ -24,3 +24,5 @@ 2021-2-25 0.790: 790 lines, 10 files, rain +2021-3-12 1.384: 1384 lines, 20 files, rain + diff --git a/Makefile b/Makefile index d011cb27613ff34896a8aa51666fa2c7acdedfa2..5df96e7add66b67bb7925373761189d2314bc15f 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,14 @@ all: make cunix.img clear: - rm *.bin boot/x86/*.bin kernel/*.o kernel/*.bin kernel/init/*.o kernel/hal/*.o cunix.img -rf + make -C kernel/ clear + make -C boot/x86/ clear + rm cunix.img boot.bin + lines: - wc boot/x86/boot.asm boot/x86/loader.asm kernel/init/*.c kernel/hal/*.c include/arch/x86/* include/kernel/* -l + wc boot/x86/boot.asm boot/x86/loader.asm kernel/init/*.c kernel/lib/*.c kernel/hal/vgatext/*.c include/arch/x86/* include/arch/*.h include/kernel/* include/lib/* -l cunix.img: boot/x86/bootloader.bin kernel/kernel.bin diff --git a/README.md b/README.md index bdd26167f31c34d23731a1dba6b28932ca049bd2..1582b5b4743d565f9952b439c7c5eac5b8204733 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Cunix kernel     it uses `2MB` page to make 0-12 MB memory to `0x0000 - 0xc0000`.
-`0.385s-boot`: +`0.385s-boot`:     rebuild `boot` and `loader`.
@@ -36,21 +36,25 @@ Cunix kernel     protect-mode.
-`0.484s-boot`: +`0.484s-boot`:     let `readdisk.inc` smaller, but we just can use CHS or LBA, not both.
-`0.509s-kernel`: +`0.509s-kernel`:     load kernel to 0x8200-0x9200 for 4KB, kernel.asm display 'K' to screen.
-`0.556s-int`: +`0.556s-int`:     init IDT at 0x0000-0x1000, and set all handler at `ignore_int`, but it do nothing.
-`0.619s-boot`: +`0.619s-boot`:     load 180KB of disk, not 4KB
-`0.566s-kernel`: +`0.566s-kernel`:     write in C.
-`0.790s-kernel`: +`0.790s-kernel`:     make some structs
+ +`1.384s-hal`: +    use text VGA mode, and make a file-operations of it. + diff --git a/boot/x86/Makefile b/boot/x86/Makefile index ae5a8424b253a550fc0d240b8aa1fafc67395ca0..982bf23617beb6ea988d2a45dfb0a2a82866c153 100644 --- a/boot/x86/Makefile +++ b/boot/x86/Makefile @@ -18,3 +18,4 @@ bootloader.bin: boot.bin loader.bin clear: rm *.bin + diff --git a/boot/x86/loader.asm b/boot/x86/loader.asm index 070d17d9ea34e495e465044d425529de941bae54..a960ce4eeef435c34942980c9d1f198bc402026c 100644 --- a/boot/x86/loader.asm +++ b/boot/x86/loader.asm @@ -28,8 +28,6 @@ jmp _start bits 16 _start: - call set_vga_mode - ; enable A20 address 20 line .open_a20: in al, 0x92 @@ -65,12 +63,6 @@ print: .do_ret: ret -set_vga_mode: - mov al, 0x13 - mov ah, 0x00 - int 0x10 - - ret bits 32 diff --git a/include/arch/io.h b/include/arch/io.h new file mode 100644 index 0000000000000000000000000000000000000000..0e1208359e4460b4bed75a9943d8eefa32f9e30a --- /dev/null +++ b/include/arch/io.h @@ -0,0 +1,64 @@ +/* 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_IO_H +#define INCLUDED_IO_H + +#include + + +typedef __uint16_t port_t; + + +#define inb(port) ({ \ + __uint8_t _r; \ + __asm__ volatile ("inb %%dx, %%al" : "=a" (_r) : "d" (port)); \ + _r; \ +}) + +#define inw(port) ({ \ + __uint16_t _r; \ + __asm__ volatile ("inw %%dx, %%ax" : "=a" (_r) : "d" (port)); \ + _r; \ +}) + +#define inl(port) ({ \ + __uint32_t _r; \ + __asm__ volatile ("inl %%dx, %%eax" : "=a" (_r) : "d" (port)); \ + _r; \ +}) + + +#define outb(value, port) \ + __asm__ volatile ("outb %%al, %%dx" :: "a" (value), "d" (port)); + +#define outw(value, port) \ + __asm__ volatile ("outw %%ax, %%dx" :: "a" (value), "d" (port)); + +#define outl(value, port) \ + __asm__ volatile ("outl %%eax, %%dx" :: "a" (value), "d" (port)); + + +#endif diff --git a/include/kernel/errno.h b/include/kernel/errno.h index 9d00f3b507c6d1ad3cf0f9fca32431f3f29cb9fb..f5fa21b64e84740fc6d2cdd417f9cdb478be10fc 100644 --- a/include/kernel/errno.h +++ b/include/kernel/errno.h @@ -24,6 +24,9 @@ +#ifndef INCLUDED_ERRNO_H +#define INCLUDED_ERRNO_H + typedef __uint32_t errno_t; @@ -31,36 +34,45 @@ typedef __uint32_t errno_t; #define E_KERROR 0x20000000 /* error io port */ -#define E_IOERROR 0x00000001 +#define E_IOERROR 0x00000001 | E_KERROR /* no enough memory for this call */ -#define E_NOMEM 0x00000002 +#define E_NOMEM 0x00000002 | E_KERROR /* call interrupted by signal */ -#define E_SIGNAL 0x00000003 +#define E_SIGNAL 0x00000003 | E_KERROR + +/* kernel not supported */ +#define E_NOSUPP 0x00000004 | E_KERROR /* no permision */ #define E_DENIED 0x40000000 /* need root to call this */ -#define E_NDROOT 0x00000001 +#define E_NDROOT 0x00000001 | E_DENIED /* no io passport */ -#define E_IODND 0x00000002 +#define E_IODND 0x00000002 | E_DENIED /* need hardware super 0 */ -#define E_HARDSP 0x00000003 +#define E_HARDSP 0x00000003 | E_DENIED + +/* hardware no support */ +#define E_HNOSUP 0x00000004 | E_DENIED /* user error (caller) */ -#define E_UERROR 0x80000000 +#define E_UERROR 0x80000000 | E_UERROR + /* syntax error for this call (invalid argument) */ -#define E_SYNERR 0x00000001 +#define E_SYNERR 0x00000001 | E_UERROR /* error because address is invalid */ -#define E_ADDRE 0x00000002 +#define E_ADDRE 0x00000002 | E_UERROR /* call out of range */ -#define E_OUTRAG 0x000000003 +#define E_OUTRAG 0x00000003 | E_UERROR + +#endif diff --git a/include/kernel/init.h b/include/kernel/init.h index 1073c38be08741b4006c5b8249ff51a03b3f56a0..cb2130bb0a09811f73a5f56a3b3af656fdbacd32 100644 --- a/include/kernel/init.h +++ b/include/kernel/init.h @@ -24,19 +24,13 @@ -#include -#include +#ifndef INCLUDED_INIT_H +#define INCLUDED_INIT_H +#include +#include -#define MAKE_MODULE(name) struct file_operations name##_fop = { \ - .open = name##_open; \ - .creat = name##_create; \ - .read = name##_read; \ - .write = name##_write; \ - .lseek = name##_lseek; \ - .close = name##_close; \ -}; struct inode_desc { @@ -48,7 +42,7 @@ struct inode_desc { /* file descriptor */ __uint32_t fd; - /* this is optional */ + /* this is optional (offset) */ __uint32_t off; /* this is optional too */ @@ -56,16 +50,18 @@ struct inode_desc { }; -struct file_oprerations { +struct file_operations { /* open a file */ struct inode_desc * (* open) (char *name, __uint32_t type, __uint32_t mode, __uint32_t *errno); - struct inode_desc * (* open) (__uint32_t type, __uint32_t mode, __uint32_t *errno); /* or create one */ /* `creat` doesn't return file-descriptor, it returns errno */ errno_t (* creat) (char *name, __uint32_t type, __uint32_t mode); - errno_t (* creat) (__uint32_t type, __uint32_t mode); + + /* `fill` fills a inode descriptor */ + errno_t (* fill) (char *name, __uint32_t type, __uint32_t mode, struct inode_desc *i); + /* read from file-descriptor */ errno_t (* read) (struct inode_desc *i, char *buffer, __uint64_t l); @@ -76,6 +72,11 @@ struct file_oprerations { /* change position */ /* 'seg' = whence */ + +#define SEEK_SET 0x0001 +#define SEEK_CUR 0x0002 +#define SEEK_END 0x0003 + __uint32_t (* lseek) (struct inode_desc *i, __uint32_t off, __uint32_t seg); /* close a file-descriptor */ @@ -83,3 +84,5 @@ struct file_oprerations { }; +#endif + diff --git a/include/kernel/module.h b/include/kernel/module.h new file mode 100644 index 0000000000000000000000000000000000000000..2705de4c1f8f413fbc93bbd435e13e36606f42b5 --- /dev/null +++ b/include/kernel/module.h @@ -0,0 +1,46 @@ +/* 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_MODULE_H +#define INCLUDED_MODULE_H + +#include +#include +#include + + +#define INIT_MODULE(modn) { \ + .open = modn##_open, \ + .creat = modn##_creat, \ + .fill = modn##_fill, \ + .read = modn##_read, \ + .write = modn##_write, \ + .lseek = modn##_lseek, \ + .close = modn##_close \ +}; + + +#endif diff --git a/include/kernel/print.h b/include/kernel/print.h new file mode 100644 index 0000000000000000000000000000000000000000..90a21480133c11baf7ca2a6c0ccc758fb20b36e3 --- /dev/null +++ b/include/kernel/print.h @@ -0,0 +1,39 @@ +/* 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_PRINT_H +#define INCLUDED_PRINT_H + + +#include +#include + +#include + +__uint32_t print(struct vga_inode *inode, char *s); + +#endif + diff --git a/include/kernel/types.h b/include/kernel/types.h index a489f2e4b0fd3dc995220cdaba780950a0e545fc..e8b154e297609926107348ba98b397d668062114 100644 --- a/include/kernel/types.h +++ b/include/kernel/types.h @@ -24,6 +24,9 @@ +#ifndef INCLUDED_TYPES_H +#define INCLUDED_TYPES_H + #define NULL (void *) 0 @@ -45,3 +48,5 @@ typedef __uint_t __uint32_t; typedef __ulong_t __uint64_t; +#endif + diff --git a/include/kernel/vgatext.h b/include/kernel/vgatext.h new file mode 100644 index 0000000000000000000000000000000000000000..18473aec15029f64516ee38c82f49f53a7a925aa --- /dev/null +++ b/include/kernel/vgatext.h @@ -0,0 +1,59 @@ +/* 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_VGA_H +#define INCLUDED_VGA_H + + +#include +#include +#include +#include + + +#define TEXT_VGA_X 80 + + + +extern struct file_operations vga; + +struct vga_inode { + struct inode_desc inode; + + /* frame buffer start address */ + /* frame buffer in text mode: + char ASCII | char COLOR + */ + + /* but use char is easier */ + + __uint8_t *fb; + + __uint32_t x, y; +}; + +#endif + diff --git a/include/lib/bitman.h b/include/lib/bitman.h new file mode 100644 index 0000000000000000000000000000000000000000..1f6721404c2453af0c9b4f980d6d88f6e8d50a92 --- /dev/null +++ b/include/lib/bitman.h @@ -0,0 +1,33 @@ +/* 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 . +*/ + + + + +struct bitmap_manager { + unsigned char *bitmap; + unsigned int next_free; +}; + +unsigned int bitman_alloc(struct bitmap_manager *bm); +void bitman_free(struct bitmap_manager *bm, unsigned int pos); diff --git a/include/lib/bitmap.h b/include/lib/bitmap.h new file mode 100644 index 0000000000000000000000000000000000000000..f93c39f7df5b1ab30ba3ecb11439f6aea6990bbd --- /dev/null +++ b/include/lib/bitmap.h @@ -0,0 +1,30 @@ +/* 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 . +*/ + + + + +extern char *bitmap_init(unsigned int size); +extern void bitmap_set(unsigned char *bitmap, unsigned int pos, unsigned char value); +extern char bitmap_read(unsigned char *bitmap, unsigned int pos); + diff --git a/kernel/hal/terminal.c b/include/lib/rlib.h similarity index 93% rename from kernel/hal/terminal.c rename to include/lib/rlib.h index b16692e427695362e97c63b529e0f1ef8ede48b2..bbd0c4ee0dc338e58c077b2a9c69b32a787600b2 100644 --- a/kernel/hal/terminal.c +++ b/include/lib/rlib.h @@ -24,3 +24,7 @@ +#include +#include + + diff --git a/kernel/Makefile b/kernel/Makefile index dd57bd44fad27c57f2e879cddd4f86244784e3c2..210d2da65f798d9ab3853c2aa1ea8510e873c01b 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,13 +1,19 @@ default: make -C init/ make -C hal/ + make -C lib/ make kernel.bin -kernel.o: init/main.o hal/terminal.o - ld -b elf64-x86-64 -o kernel.o init/main.o hal/terminal.o -T kernel.lds +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 objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary kernel.o kernel.bin +clear: + make -C init/ clear + make -C hal/ clear + make -C lib/ clear + rm kernel.o kernel.bin diff --git a/kernel/hal/Makefile b/kernel/hal/Makefile index 5e6f7414b0742be65b7e3d7dc7ec53e23fc028ed..2df04c88dc0f9632daf39b08ad38ea131bcc2f12 100644 --- a/kernel/hal/Makefile +++ b/kernel/hal/Makefile @@ -1,8 +1,6 @@ -C_FLAGS = -mcmodel=large -fno-builtin -m64 - -all: terminal.c - -%.o: %.c - gcc -c $*.c $(C_FLAGS) +all: vgatext/vgatext.c + make -C vgatext/ +clear: + make -C vgatext/ clear diff --git a/kernel/hal/vgatext/Makefile b/kernel/hal/vgatext/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..60e6a9943c7641fd87ebed5a46c6ecce2de332bb --- /dev/null +++ b/kernel/hal/vgatext/Makefile @@ -0,0 +1,13 @@ +C_FLAGS = -mcmodel=large -fno-builtin -m64 -I ../../../include + + +all: vgatext.o + + +%.o: %.c + gcc -c *$.c $(C_FLAGS) + + +clear: + rm *.o + diff --git a/kernel/hal/vgatext/vgatext.c b/kernel/hal/vgatext/vgatext.c new file mode 100644 index 0000000000000000000000000000000000000000..631ce1c3fbd85c35af7827fdc9e2822a97b010c8 --- /dev/null +++ b/kernel/hal/vgatext/vgatext.c @@ -0,0 +1,155 @@ +/* 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 . +*/ + + + + +#include +#include +#include +#include + +#include +#include + +#include + + + + +__uint32_t next_fd = 1; + +struct inode_desc *vga_open(char *name, __uint32_t type, __uint32_t mode, errno_t *errno) { + *errno = -E_NOSUPP; +} + + +errno_t vga_creat(char *name, __uint32_t type, __uint32_t mode) { + return -E_NOSUPP; +} + + +errno_t vga_read(struct inode_desc *inode, char *buffer, __uint64_t l) { + return -E_HNOSUP; +} + + +void update_cursor(__uint32_t x, __uint32_t y) { + __uint16_t pos = y * TEXT_VGA_X + x; + + outb(0x0f, 0x03d4); + outb((__uint8_t) (pos & 0xff), 0x03d5); + outb(0x0e, 0x03d4); + outb((__uint8_t) ((pos >> 8) & 0xff), 0x03d5); + + return; +} + +__uint16_t read_cursor(void) { + __uint16_t pos = 0; + + outb(0x0f, 0x03d4); + pos |= inb(0x03d5); + + outb(0x0e, 0x03d4); + pos |= ((__uint16_t) inb(0x03d5)) << 8; + + return pos; +} + + +errno_t vga_fill(char *name, __uint32_t type, __uint32_t mode, struct vga_inode *inode) { + inode->inode.size = sizeof(struct vga_inode); + inode->inode.fd = next_fd; + + inode->inode.off = 1 * TEXT_VGA_X + 0; + + inode->fb = (__uint8_t *) 0xb8000; + inode->x = 0; + + /* first line is in use */ + inode->y = 1; + + next_fd++; + + update_cursor(0, 0); + + return 0; +} + + +errno_t vga_write(struct vga_inode *inode, char *buffer, __uint64_t l) { + /* for safety, we re-calc offset */ + inode->inode.off = (inode->y * TEXT_VGA_X + inode->x) * 2; + + if (*buffer == '\n') { + /* we use LF to line feed, not CRLF in Windows */ + inode->x = 0; + inode->y++; + update_cursor(inode->x, inode->y); + + return 0; + } + + if (*buffer == '\r') { + inode->x = 0; + update_cursor(inode->x, inode->y); + } + + + /* put it */ + + inode->fb[inode->inode.off] = *buffer; + /* color white */ + inode->fb[inode->inode.off + 1] = 0x0f; + + inode->inode.off += 2; + + inode->x++; + if (inode->x == TEXT_VGA_X) { + inode->x = 0; + inode->y++; + } + + update_cursor(inode->x, inode->y); + + return 0; +} + + +errno_t vga_lseek(struct inode_desc *inode, __uint32_t off, __uint32_t seg) { + return -E_HNOSUP; +} + + +void vga_close(struct inode_desc *inode) { + /* disable cursor */ + outb(0x3d4, 0x0a); + outb(0x3d5, 0x20); + + return; +} + + +struct file_operations vga = INIT_MODULE(vga); + diff --git a/kernel/init/Makefile b/kernel/init/Makefile index 42d6028b450c4b8e17cf1eb9ec98484e364875b3..f6c63cb9256eb632538390121acbf3b210c1a346 100644 --- a/kernel/init/Makefile +++ b/kernel/init/Makefile @@ -1,8 +1,11 @@ -C_FLAGS = -mcmodel=large -fno-builtin -m64 +C_FLAGS = -mcmodel=large -fno-builtin -m64 -I ../../include/ -all: main.o +all: main.o print.o %.o: %.c gcc -c $*.c $(C_FLAGS) +clear: + rm *.o + diff --git a/kernel/init/main.c b/kernel/init/main.c index 2c2824ffe3c03afe2ddc0d794b0fc39c36b75c2b..e30578b52caa167ebf5fb33adc85e809bdc663c7 100644 --- a/kernel/init/main.c +++ b/kernel/init/main.c @@ -24,12 +24,17 @@ -void init(void) { - unsigned long i; +#include +#include +#include + +#include +#include - for (i = 0xa0000; i < 0xaffff; i++) { - *((char *) i) = 0x0f; - } +void init(void) { + struct vga_inode vi; + vga.fill(NULL, 0, 0, &vi); + print(&vi, "kernel starting\n"); for (;;); } diff --git a/kernel/init/print.c b/kernel/init/print.c new file mode 100644 index 0000000000000000000000000000000000000000..d65241adee31bbf6c47de05ec15e46fe78c04617 --- /dev/null +++ b/kernel/init/print.c @@ -0,0 +1,40 @@ +/* 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 . +*/ + + + + +#include +#include +#include + +#include + + +errno_t print(struct vga_inode *inode, char *s) { + for (; *s; s++) { + vga.write(inode, s, 1); + } + + return 0; +} diff --git a/kernel/lib/Makefile b/kernel/lib/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..87908e5738e5424a9415b23f0683edf665f17749 --- /dev/null +++ b/kernel/lib/Makefile @@ -0,0 +1,11 @@ +C_FLAGS = -mcmodel=large -m64 -fno-builtin -nostdlib -I ../../include/ + +all: bitmap.c bitman.c bitmap.o bitman.o + +%.o: %.c + gcc -c $*.c $(C_FLAGS) + + +clear: + rm *.o + diff --git a/kernel/lib/bitman.c b/kernel/lib/bitman.c new file mode 100644 index 0000000000000000000000000000000000000000..73e751a290fe63772a0e838902f91ff4ff140a2b --- /dev/null +++ b/kernel/lib/bitman.c @@ -0,0 +1,63 @@ +/* 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 . +*/ + + + + +#include + + +/* + find a new bit in bm->bitmap. + + STEPS: + 1. save free position. + 2. find a new free position. + 3. save new free position. +*/ +unsigned int bitman_alloc(struct bitmap_manager *bm) { + unsigned int p = bm->next_free; + bitmap_set(bm->bitmap, bm->next_free, 1); + + for (; bitmap_read(bm->bitmap, bm->next_free) == 1; bm->next_free++); + + return p; +} + + +/* + free a bit. + + STEPS: + 1. free bit pos. + 2. if pos is lowwer than bm->next_free + 2a. set bm->next_free = pos. +*/ +void bitman_free(struct bitmap_manager *bm, unsigned int pos) { + bitmap_set(bm->bitmap, pos, 0); + + if (pos < bm->next_free) { + bm->next_free = pos; + } + return; +} diff --git a/kernel/lib/bitmap.c b/kernel/lib/bitmap.c new file mode 100644 index 0000000000000000000000000000000000000000..4d3d32dee2cca1bd18dec94088e1e7bf2d750ee3 --- /dev/null +++ b/kernel/lib/bitmap.c @@ -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 . +*/ + + + + +#include + +void bitmap_set(unsigned char *bitmap, unsigned int pos, unsigned char value) { + unsigned char mask = 0x80 >> (pos & 0x7); + if (value) { + bitmap[pos >> 3] |= mask; + } + else { + bitmap[pos >> 3] &= ~mask; + } +} + +char bitmap_read(unsigned char *bitmap, unsigned int pos) { + unsigned char mask = 0x80 >> (pos & 0x7); + + return (mask & bitmap[pos >> 3]) == mask ? 1 : 0; +} +