Przeglądaj źródła

1.611s-modules: first stable version: put a message on screen

rain 5 lat temu
rodzic
commit
981cf27221

+ 1 - 0
LINES

@@ -3,4 +3,5 @@
 2021-5-29 0.431: 431 lines, 3 files, rain
 2021-5-29 0.431: 431 lines, 3 files, rain
 2021-5-30 0.427: 427 lines, 4 files, rain
 2021-5-30 0.427: 427 lines, 4 files, rain
 2021-5-30 0.437: 437 lines, 5 files, rain
 2021-5-30 0.437: 437 lines, 5 files, rain
+2021-5-30 1.708: 1611 lines, 18 files, rain
 
 

+ 12 - 8
Makefile

@@ -1,32 +1,36 @@
 OBJS = boot/bootsect 
 OBJS = boot/bootsect 
-KOBJS = init/head.o init/main.o
-KSRCS = init/head.S init/main.c
+KOBJS = init/head.o init/main.o modules/tty.o kernel/tty.o
+KSRCS = init/head.S init/main.c modules/tty.c kernel/tty.S
 
 
 all: Makefile
 all: Makefile
 	make -C boot/
 	make -C boot/
 	make -C init/
 	make -C init/
+	make -C modules/
+	make -C kernel/ 
 	make xnix.img
 	make xnix.img
 
 
-kernel: $(KSRCS) boot/bootsect Makefile
+kbinary: $(KSRCS) boot/bootsect Makefile
 	ld -b elf64-x86-64 -o kernel.o $(KOBJS) -T kernel.lds
 	ld -b elf64-x86-64 -o kernel.o $(KOBJS) -T kernel.lds
-	objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O binary kernel.o kernel
+	objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O binary kernel.o kbinary
 
 
 
 
-xnix.img: boot/bootsect $(KSRCS) Makefile kernel
+xnix.img: boot/bootsect $(KSRCS) Makefile kbinary
 	dd if=/dev/zero of=xnix.img bs=1024 count=1440
 	dd if=/dev/zero of=xnix.img bs=1024 count=1440
 	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
 	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
-	dd if=kernel of=xnix.img conv=notrunc bs=512 seek=2
+	dd if=kbinary of=xnix.img conv=notrunc bs=512 seek=2
 
 
 
 
 lines: Makefile
 lines: Makefile
-	wc -l `find ./ -name *.asm`
+	wc -l `find ./ -name *.asm -o -name *.c -o -name *.h -o -name *.S -o -name *.s`
 
 
 
 
 clear: Makefile
 clear: Makefile
 	make -C boot/ clear
 	make -C boot/ clear
 	make -C init/ clear
 	make -C init/ clear
+	make -C modules/ clear
+	make -C kernel/ clear
 	rm boot/bootsect
 	rm boot/bootsect
-	rm xnix.img kernel kernel.o
+	rm xnix.img kbinary kernel.o
 
 
 
 
 
 

+ 4 - 0
README.md

@@ -30,3 +30,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;and then `head.S` check return value of init, if not zero, it will put a message on screen. 
 &nbsp;&nbsp;&nbsp;&nbsp;and then `head.S` check return value of init, if not zero, it will put a message on screen. 
 &nbsp;&nbsp;&nbsp;&nbsp;if no error, it will halted the CPU. 
 &nbsp;&nbsp;&nbsp;&nbsp;if no error, it will halted the CPU. 
 
 
+1.611s-modules:  
+&nbsp;&nbsp;&nbsp;&nbsp;first stable version! 
+&nbsp;&nbsp;&nbsp;&nbsp;copy some files from Cunix, now XNIX can put a formated message on screen. 
+

+ 64 - 0
include/arch/io.h

@@ -0,0 +1,64 @@
+/* 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 INCLUDED_IO_H
+#define INCLUDED_IO_H
+
+#include <kernel/types.h>
+
+
+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

+ 71 - 0
include/arch/sys.h

@@ -0,0 +1,71 @@
+/* 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 INCLUDED_SYS_H
+#define INCLUDED_SYS_H
+
+#include <kernel/types.h>
+
+
+#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");
+
+
+#define getreg(regname) ({ \
+	__uint64_t res; \
+	__asm__ __volatile__ ("movq %%" regname ", %0" : "=r" (res)); \
+	res; \
+})
+
+#define get_rax() getreg("rax")
+#define get_rcx() getreg("rcx")
+#define get_rdx() getreg("rdx")
+#define get_rbx() getreg("rbx")
+
+#define get_rsp() getreg("rsp")
+#define get_rbp() getreg("rbp")
+#define get_rsi() getreg("rsi")
+#define get_rdi() getreg("rdi")
+
+#define get_r8()  getreg("r8")
+#define get_r9()  getreg("r9")
+#define get_r10() getreg("r10")
+#define get_r11() getreg("r11")
+
+#define get_r12() getreg("r12")
+#define get_r13() getreg("r13")
+#define get_r14() getreg("r14")
+#define get_r15() getreg("r15")
+
+
+#endif

+ 62 - 0
include/arch/x86/paging.h

@@ -0,0 +1,62 @@
+/* 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 INCLUDED_PAGING_H
+#define INCLUDED_PAING_H
+
+
+#define PAGE_PRESENT  0x0001
+
+#define PAGE_RDWR     0x0002
+
+#define PAGE_USER     0x0004
+
+#define PAGE_NWRBACK  0x0008
+
+#define PAGE_NOCACHE  0x0010
+
+#define PAGE_ACCESSED 0x0020
+
+#define PAGE_DIRTY    0x0040
+
+#define PAGE_PAT      0x0080
+
+#define PAGE_GLOBAL   0x0100
+
+
+/* 0x83 */
+#define PATT_KERNEL (PAGE_PRESENT | PAGE_RDWR | PAGE_PAT)
+
+/* 0x07 */
+#define PATT_USER   (PAGE_PRESENT | PAGE_RDWR | PAGE_USER)
+
+
+#define PML4_ADDR 0x70000
+#define PDPT_ADDR 0x71000
+#define PTE_ADDR  0x72000
+
+#define PAGE_SIZE 0x200000
+
+#endif

+ 78 - 0
include/kernel/errno.h

@@ -0,0 +1,78 @@
+/* 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 INCLUDED_ERRNO_H
+#define INCLUDED_ERRNO_H
+
+typedef __uint32_t errno_t;
+
+
+/* error in kernel */
+#define E_KERROR 0x20000000
+
+/* error io port */
+#define E_IOERROR 0x00000001 | E_KERROR
+
+/* no enough memory for this call */
+#define E_NOMEM	  0x00000002 | E_KERROR
+
+/* call interrupted by signal */
+#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 | E_DENIED
+
+/* no io passport */
+#define E_IODND  0x00000002 | E_DENIED
+
+/* need hardware super 0 */
+#define E_HARDSP 0x00000003 | E_DENIED
+
+/* hardware no support */
+#define E_HNOSUP 0x00000004 | E_DENIED
+
+/* user error (caller) */
+#define E_UERROR 0x80000000
+
+
+/* syntax error for this call (invalid argument) */
+#define E_SYNERR 0x00000001 | E_UERROR
+
+/* error because address is invalid */
+#define E_ADDRE  0x00000002 | E_UERROR
+
+/* call out of range */
+#define E_OUTRAG 0x00000003 | E_UERROR
+
+#endif
+

+ 95 - 0
include/kernel/init.h

@@ -0,0 +1,95 @@
+/* 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 INCLUDED_INIT_H
+#define INCLUDED_INIT_H
+
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+
+
+#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. */
+
+	__uint32_t size;
+
+	/* file descriptor */
+	__uint32_t fd;
+
+	/* this is optional (offset) */
+	__uint32_t off;
+
+	/* this is optional too */
+	char *name;
+
+	/* real inode saves here */
+	void *real_inode;
+};
+
+
+struct file_operations {
+	/* open a file */
+	struct inode_desc * (* open) (char *, __uint32_t, __uint32_t, errno_t *);
+
+	/* or create one */
+
+	/* `creat` doesn't return file-descriptor, it returns errno */
+	errno_t (* creat) (char *, __uint32_t, __uint32_t);
+
+	/* `fill` fills a inode descriptor */
+	errno_t (* fill) (char *, __uint32_t, __uint32_t, struct inode_desc *);
+
+
+	/* read from file-descriptor */
+	errno_t (* read) (struct inode_desc *, char *, __uint64_t);
+	
+	/* write to file-descriptor */
+	errno_t (* write) (struct inode_desc *, char *, __uint64_t);
+
+	/* change position */
+
+	/* 'seg' = whence */
+
+#define SEEK_SET 0x0001
+#define SEEK_CUR 0x0002
+#define SEEK_END 0x0003
+
+	__uint32_t (* lseek) (struct inode_desc *, __uint32_t, __uint32_t);
+
+	/* close a file-descriptor */
+	void (* close) (struct inode_desc *);
+};
+
+
+#endif
+

+ 52 - 0
include/kernel/types.h

@@ -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/>.  
+*/
+
+
+
+
+#ifndef INCLUDED_TYPES_H
+#define INCLUDED_TYPES_H
+
+#define NULL (void *) 0
+
+
+typedef char 		__schar_t;
+typedef unsigned char 	__uchar_t;
+
+typedef short		__sshort_t;
+typedef unsigned short 	__ushort_t;
+
+typedef int 		__sint_t;
+typedef unsigned int	__uint_t;
+
+typedef long		__slong_t;
+typedef unsigned long	__ulong_t;
+
+typedef __uchar_t 	__uint8_t;
+typedef __ushort_t 	__uint16_t;
+typedef __uint_t 	__uint32_t;
+typedef __ulong_t 	__uint64_t;
+
+
+#endif
+

+ 39 - 0
include/lib/bitman.h

@@ -0,0 +1,39 @@
+/* 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 INCLUDED_BITMAN_H
+#define INCLUDED_BITMAN_H
+
+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);
+
+#endif
+

+ 34 - 0
include/lib/bitmap.h

@@ -0,0 +1,34 @@
+/* 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 INCLUDED_BITMAP_H
+#define INCLUDED_BITMAP_H
+
+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);
+
+#endif

+ 33 - 0
include/lib/rlib.h

@@ -0,0 +1,33 @@
+/* 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 INCLUDED_RLIB_H
+#define INCLUDED_RLIB_H
+
+#include <lib/bitmap.h>
+#include <lib/bitman.h>
+
+#endif

+ 90 - 0
include/lib/string.h

@@ -0,0 +1,90 @@
+/* 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 INCLUDED_STRING_H
+#define INCLUDED_STRING_H
+
+
+
+/* copy src to dest for n bytes */
+#define memcpy(dest, src, n) ({ \
+	__asm__ ( \
+		"cld\n\t" \
+		"rep movsb\n\t" \
+		:: "c" (n), "S" (src), "D" (dest) \
+	); \
+	dest; \
+})
+
+
+/* set n to src for n bytes */
+#define memset(dest, c, n) ({ \
+	__asm__ ( \
+		"cld\n\t" \
+		"rep stosb\n\t" \
+		:: "c" (n), "D" (dest), "a" (c) \
+	); \
+	dest; \
+})
+
+
+/* set n to src for n words (n must be power of 2) */
+#define memsetw(dest, c, n) ({ \
+	__asm__ ( \
+		"cld\n\t" \
+		"rep stosw\n\t" \
+		:: "c" (n >> 1), "D" (dest), "a" (c) \
+	); \
+	dest; \
+})
+
+
+/* set n to src for n dwords (n must be power of 4) */
+#define memsetl(dest, c, n) ({ \
+	__asm__ ( \
+		"cld\n\t" \
+		"rep stosl\n\t" \
+		:: "c" (n >> 2), "D" (dest), "a" (c) \
+	); \
+	dest; \
+})
+
+
+/* set n to src for n quads (n must be power of 8) */
+#define memsetq(dest, c, n) ({ \
+	__asm__ ( \
+		"cld\n\t" \
+		"rep stosq\n\t" \
+		:: "c" (n >> 3), "D" (dest), "a" (c) \
+	); \
+	dest; \
+})
+
+
+
+
+#endif
+

+ 74 - 0
include/modules/tty.h

@@ -0,0 +1,74 @@
+/* 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 INCLUDED_TTY_H
+#define INCLUDED_TTY_H
+
+/* index register of cursor */
+#define CURSOR_INDEX 0x03d4
+
+/* data register of cursor */
+#define CURSOR_DATA  0x03d5
+
+/* write to CURSOR_INDEX */
+#define CURSORCTL_POSHI 0x0e
+#define CURSORCTL_POSLO 0x0f
+
+#define TEXT_VGA_X 80
+#define TEXT_VGA_Y 25
+
+#ifndef ASSEMBLY
+
+#include <stdarg.h>
+
+/* kernel/asm/tty.c */
+
+void tty_init(void);
+
+void putc(char c);
+
+/* kernel/modules/tty.c */
+
+/* return value: length of string printed */
+
+/* we use number formats (1B per format), not % */
+__uint64_t printf(const char *fmt, ...);
+
+__uint64_t printint(long, int, int);
+
+__uint16_t read_cursor(void);
+
+__uint64_t printptr(__uint64_t);
+
+void write_cursor(__uint16_t pos);
+
+void clear_line(int line);
+
+
+#endif
+
+
+#endif

+ 1 - 37
init/head.S

@@ -65,40 +65,11 @@ init_die:
 
 
 init_panic:
 init_panic:
 	leaq panic_msg(%rip), %rdi
 	leaq panic_msg(%rip), %rdi
-	callq print
+	callq printf
 
 
 	jmp init_die
 	jmp init_die
 
 
 
 
-.globl print
-
-print:
-	/* C stores the first argument in %rdi */
-	movq %rdi, %rsi
-
-	movq framebuffer(%rip), %rdi
-
-	cld
-
-print_loop:
-	lodsb
-
-	testb %al, %al
-	jz print_ret
-
-	movb $0x07, %ah
-	stosw
-
-	jmp print_loop
-
-print_ret:
-	/* update frame buffer */
-	movq %rdi, framebuffer(%rip)
-
-	ret
-
-
-
 
 
 .section .rodata
 .section .rodata
 .align 8
 .align 8
@@ -110,10 +81,3 @@ panic_msg:
 	.asciz "panic in init: halted"
 	.asciz "panic in init: halted"
 
 
 
 
-.data
-
-.globl framebuffer
-
-framebuffer:
-	.quad 0x00000000000b8000
-

+ 5 - 2
init/main.c

@@ -23,8 +23,11 @@
 
 
 
 
 
 
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
 
 
-extern void print(char *s);
+#include <modules/tty.h>
 
 
 
 
 /* 
 /* 
@@ -35,7 +38,7 @@ extern void print(char *s);
  * */
  * */
 
 
 int init(void) {
 int init(void) {
-	print("XNIX kernel init");
+	printf("XNIX kernel init\n");
 
 
 	return 0;
 	return 0;
 }
 }

+ 9 - 0
kernel/Makefile

@@ -0,0 +1,9 @@
+all: tty.o 
+
+%.o: %.S
+	gcc -E $*.S -I ../include > $*.s
+	as --64 $*.s -o $*.o 
+
+clear:
+	rm *.o *.s
+

+ 83 - 0
kernel/tty.S

@@ -0,0 +1,83 @@
+/* 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/>.  
+*/
+
+
+
+
+#define ASSEMBLY
+#include <modules/tty.h>
+
+
+
+.text
+.align 8
+
+
+
+.globl tty_init
+
+tty_init:
+	pushq %rsi
+	pushq %rdi
+
+	/* set cursor position */
+	movw $0x0000, %di
+	call write_cursor
+
+	popq %rdi
+	popq %rsi
+
+	ret
+	
+
+.globl putc
+
+putc:
+	pushq %rdi
+
+	/* character is in EDI */
+	movl %edi, %eax
+
+	/* rdi = *framebuffer */
+	movq framebuffer(%rip), %rdi
+	movb $0x0f, %ah
+
+	cld
+	stosw
+
+	movq %rdi, framebuffer(%rip)
+	
+	/* return 0 */
+	xorq %rax, %rax
+
+	popq %rdi
+	ret
+
+
+.data
+.align 8
+
+.globl framebuffer
+framebuffer: .quad 0x00000000000b8000
+
+

+ 11 - 0
modules/Makefile

@@ -0,0 +1,11 @@
+OBJS = tty.o
+CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -O3 -fno-stack-protector
+
+all: $(OBJS)
+
+%.o: %.c
+	gcc -c $*.c $(CFLAGS)
+
+clear:
+	rm *.o
+

+ 272 - 0
modules/tty.c

@@ -0,0 +1,272 @@
+/* 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 <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+#include <modules/tty.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+
+#include <lib/string.h>
+
+#include <stdarg.h>
+
+
+
+
+/* variables in .data will initialize by value 0 */
+int x, y;
+
+extern __uint64_t framebuffer;
+
+__uint16_t read_cursor(void) {
+	__uint16_t pos = 0;
+
+	outb(CURSORCTL_POSLO, CURSOR_INDEX);
+	pos |= inb(CURSOR_DATA);
+
+	outb(CURSORCTL_POSHI, CURSOR_INDEX);
+	pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
+
+	return pos;
+}
+
+
+
+void write_cursor(__uint16_t pos) {
+	outb(CURSORCTL_POSLO, CURSOR_INDEX);
+	outb((__uint8_t) (pos & 0xff), CURSOR_DATA);
+
+	outb(CURSORCTL_POSHI, CURSOR_INDEX);
+	outb((__uint8_t) ((pos >> 8) & 0xff), CURSOR_DATA);
+
+	return;
+}
+
+
+
+void update_cursor(void) {
+	__uint16_t pos = y * TEXT_VGA_X + x;
+	write_cursor(pos);
+
+	framebuffer = 0xb8000 + pos * 2;
+
+	return;
+}
+
+
+
+__uint64_t printf(const char *s, ...) {
+	/* l: character puts (no '%') */
+	__uint64_t i, l;
+	/* if prev character is '%', state = '%' */
+	char state, c;
+	/* fmt_s only use in %s, but we cannot define variables in switch-case */
+	char *fmt_s;
+	va_list values;
+
+	int newlines;
+
+	va_start(values, s);
+	state = 0;
+	newlines = 0;
+
+	for (i = 0, l = 0; s[i]; i++) {
+		c = s[i];
+		/* check state */
+		if (!state) {
+			if (c == '%') {
+				state = '%';
+				continue;
+			}
+
+			/* c != '%' (it's not a formater), put it */
+			else {
+				/* if it's \n */
+				if (c == '\n') {
+					/* new line, and reset x */
+					newlines++;
+				}
+
+				else {
+					putc(c);
+					l++;
+				}
+
+				continue;
+			}
+		}
+
+		/* it's a formater */
+		else {
+			if (c == 'd') {
+				l += printint((int) va_arg(values, int), 10, 1);
+			}
+
+			else if (c == 'x') {
+				/* `x` is 64-bits, I think it's */
+				l += printint((long) va_arg(values, long), 16, 0);
+			}
+
+			else if (c == 'p') {
+				/* zeros at high bits should display, not like `%x` or `%d` */
+				l += printptr((__uint64_t) va_arg(values, __uint64_t));
+			}
+
+			else if (c == 's') {
+				fmt_s = (char *) va_arg(values, char *);
+				if (!fmt_s) 
+					fmt_s = "(null)";
+						
+				/* put while end of fmt_s */
+				while (*fmt_s) {
+					putc(*fmt_s);
+					fmt_s++;
+					l++;
+				}
+			}
+			
+			else if (c == 'c') {
+				/* `char` aligned to `int` in GCC */
+				putc((char) va_arg(values, int));
+				l++;
+			}
+
+			else if (c == '%') {
+				/* put % on screen */
+				putc(c);
+				l++;
+			}
+
+			else {
+				/* don't forget to put % */
+				putc('%');
+				putc(c);
+				l++;
+			}
+
+			state = 0;
+		}
+	}
+
+
+	/* update cursor position */
+	x += l;
+	if (x > TEXT_VGA_X) {
+		x = TEXT_VGA_X - x;
+		y++;
+	}
+
+	y += newlines;
+
+	if (newlines) 
+		x = 0;
+
+	if (y > TEXT_VGA_Y) {
+		y = 0;
+		clear_line(0);
+	}
+
+	update_cursor();
+
+	va_end(values);
+
+	return i;
+}
+ 
+
+static char digits[] = {"0123456789abcdef"};
+
+
+__uint64_t printint(long n, int base, int sign) {
+	char buf[32];
+	/* neg: x < 0 && signed interger */
+	int i, neg, j;
+	/* x: real number should put */
+	__uint64_t x;
+
+	if (n < 0 && sign) {
+		neg = 1;
+		x = -n;
+	}
+
+	else {
+		neg = 0;
+		x = n;
+	}
+
+	i = 0;
+	do {
+		/* get string of based-number */
+		/* the first bit of 10-based digit is at buf[15] */
+		buf[i++] = digits[x % base];
+	} while (x /= base);
+
+	if (neg) 
+		buf[i++] = '-';
+
+	j = i;
+	while (--j >= 0) 
+		putc(buf[j]);
+
+	return i;
+}
+
+
+
+__uint64_t printptr(__uint64_t p) {
+	int i;
+
+	for (i = 15; i >= 0; i--) {
+		char c = (p >> (i * 4)) & 0x0f;
+
+		if (c > 9) 
+			c = c - 10 + 'a';
+		
+		else 
+			c += '0';
+
+		putc(c);
+	}
+
+	return 16;
+}
+
+
+#define LINE_ADDR(l) ((char *) (__uint64_t) (0xb8000 + (l) * TEXT_VGA_X * 2))
+
+
+void clear_line(int line) {
+	char *p = LINE_ADDR(line);
+
+	memsetw(p, 0x0720, TEXT_VGA_X * 2);
+
+	return;
+}
+
+