--- /dev/null
+{
+ "files.associations": {
+ "string.h": "c",
+ "stdint.h": "c"
+ }
+}
\ No newline at end of file
.section .text
.global start_
- .type start_, @function
+ .type start_, @function /* Optional, this just give the
+ * linker more knowledge about the label
+ */
start_:
movl $stack_top, %esp
/*
3. Enable paging
*/
call _kernel_init
+
+ subl $0x6, %esp
+ movl $_gdt, 2(%esp)
+ movw _gdt_limit, %ax
+ movw %ax, (%esp)
+ lgdt (%esp)
+ addl $0x6, %esp
+ movw $0x10, %cx
+ movw %cx, %es
+ movw %cx, %ds
+ movw %cx, %fs
+ movw %cx, %gs
+ movw %cx, %ss
+
+ pushw $0x08
+ pushl $_after_gdt
+ retf
+
+ _after_gdt:
pushl %ebx
call _kernel_main
--- /dev/null
+#include <lunaix/arch/gdt.h>
+#include <stdint.h>
+
+#define GDT_ENTRY 5
+
+uint64_t _gdt[GDT_ENTRY];
+uint16_t _gdt_limit = sizeof(_gdt);
+
+void _set_gdt_entry(uint32_t index, uint32_t base, uint32_t limit, uint32_t flags) {
+ _gdt[index] = SEG_BASE_H(base) | flags | SEG_LIM_H(limit) | SEG_BASE_M(base);
+ _gdt[index] <<= 32;
+ _gdt[index] |= SEG_BASE_L(base) | SEG_LIM_L(limit);
+}
+//
+void
+_init_gdt() {
+ _set_gdt_entry(0, 0, 0, 0);
+ _set_gdt_entry(1, 0, 0xfffff, SEG_R0_CODE);
+ _set_gdt_entry(2, 0, 0xfffff, SEG_R0_DATA);
+ _set_gdt_entry(3, 0, 0xfffff, SEG_R3_CODE);
+ _set_gdt_entry(4, 0, 0xfffff, SEG_R3_DATA);
+}
\ No newline at end of file
--- /dev/null
+#ifndef _STRING_H
+#define _STRING_H 1
+
+#include <stddef.h>
+
+int
+memcmp(const void*, const void*, size_t);
+
+void*
+memcpy(void* __restrict, const void* __restrict, size_t);
+
+void*
+memmove(void*, const void*, size_t);
+
+void*
+memset(void*, int, size_t);
+
+size_t
+strlen(const char* str);
+
+#endif
\ No newline at end of file
--- /dev/null
+#define SD_TYPE(x) (x << 8)
+#define SD_CODE_DATA(x) (x << 12)
+#define SD_DPL(x) (x << 13)
+#define SD_PRESENT(x) (x << 15)
+#define SD_AVL(x) (x << 20)
+#define SD_64BITS(x) (x << 21)
+#define SD_32BITS(x) (x << 22)
+#define SD_4K_GRAN(x) (x << 23)
+
+#define SEG_LIM_L(x) (x & 0x0ffff)
+#define SEG_LIM_H(x) (x & 0xf0000)
+#define SEG_BASE_L(x) ((x & 0x0000ffff) << 16)
+#define SEG_BASE_M(x) ((x & 0x00ff0000) >> 16)
+#define SEG_BASE_H(x) (x & 0xff000000)
+
+#define SEG_DATA_RD 0x00 // Read-Only
+#define SEG_DATA_RDA 0x01 // Read-Only, accessed
+#define SEG_DATA_RDWR 0x02 // Read/Write
+#define SEG_DATA_RDWRA 0x03 // Read/Write, accessed
+#define SEG_DATA_RDEXPD 0x04 // Read-Only, expand-down
+#define SEG_DATA_RDEXPDA 0x05 // Read-Only, expand-down, accessed
+#define SEG_DATA_RDWREXPD 0x06 // Read/Write, expand-down
+#define SEG_DATA_RDWREXPDA 0x07 // Read/Write, expand-down, accessed
+#define SEG_CODE_EX 0x08 // Execute-Only
+#define SEG_CODE_EXA 0x09 // Execute-Only, accessed
+#define SEG_CODE_EXRD 0x0A // Execute/Read
+#define SEG_CODE_EXRDA 0x0B // Execute/Read, accessed
+#define SEG_CODE_EXC 0x0C // Execute-Only, conforming
+#define SEG_CODE_EXCA 0x0D // Execute-Only, conforming, accessed
+#define SEG_CODE_EXRDC 0x0E // Execute/Read, conforming
+#define SEG_CODE_EXRDCA 0x0F // Execute/Read, conforming, accessed
+
+#define SEG_R0_CODE SD_TYPE(SEG_CODE_EXRD) | SD_CODE_DATA(1) | SD_DPL(0) | \
+ SD_PRESENT(1) | SD_AVL(0) | SD_64BITS(0) | SD_32BITS(1) | \
+ SD_4K_GRAN(1)
+
+#define SEG_R0_DATA SD_TYPE(SEG_DATA_RDWR) | SD_CODE_DATA(1) | SD_DPL(0) | \
+ SD_PRESENT(1) | SD_AVL(0) | SD_64BITS(0) | SD_32BITS(1) | \
+ SD_4K_GRAN(1)
+
+#define SEG_R3_CODE SD_TYPE(SEG_CODE_EXRD) | SD_CODE_DATA(1) | SD_DPL(3) | \
+ SD_PRESENT(1) | SD_AVL(0) | SD_64BITS(0) | SD_32BITS(1) | \
+ SD_4K_GRAN(1)
+
+#define SEG_R3_DATA SD_TYPE(SEG_DATA_RDWR) | SD_CODE_DATA(1) | SD_DPL(3) | \
+ SD_PRESENT(1) | SD_AVL(0) | SD_64BITS(0) | SD_32BITS(1) | \
+ SD_4K_GRAN(1)
+
+
+void
+_init_gdt();
\ No newline at end of file
-typedef unsigned short vga_atrributes;
+typedef unsigned short vga_attribute;
#define VGA_COLOR_BLACK 0
#define VGA_COLOR_BLUE 1
#define VGA_COLOR_LIGHT_BROWN 14
#define VGA_COLOR_WHITE 15
-void tty_set_theme(vga_atrributes fg, vga_atrributes bg);
-void tty_put_char(char chr);
-void tty_put_str(char* str);
-void tty_scroll_up();
-void tty_clear();
\ No newline at end of file
+void
+tty_set_theme(vga_attribute fg, vga_attribute bg);
+
+void
+tty_put_char(char chr);
+
+void
+tty_put_str(char* str);
+
+void
+tty_scroll_up();
+
+void
+tty_clear();
\ No newline at end of file
#include <lunaix/tty/tty.h>
+#include <lunaix/arch/gdt.h>
-void _kernel_init() {
+void
+_kernel_init()
+{
// TODO
+ _init_gdt();
}
-void _kernel_main(void* info_table) {
+void
+_kernel_main(void* info_table)
+{
+ // remove the warning
+ (void)info_table;
// TODO
tty_set_theme(VGA_COLOR_GREEN, VGA_COLOR_BLACK);
tty_put_str("Hello kernel world!\nThis is second line.");
+#include <libc/string.h>
#include <lunaix/tty/tty.h>
#include <stdint.h>
#define TTY_WIDTH 80
#define TTY_HEIGHT 25
-vga_atrributes *buffer = 0xB8000;
+vga_attribute* buffer = (vga_attribute*)0xB8000;
-vga_atrributes theme_color = VGA_COLOR_BLACK;
+vga_attribute theme_color = VGA_COLOR_BLACK;
-uint32_t TTY_COLUMN = 0;
-uint16_t TTY_ROW = 0;
+uint32_t tty_x = 0;
+uint16_t tty_y = 0;
-void tty_set_theme(vga_atrributes fg, vga_atrributes bg) {
+void
+tty_set_theme(vga_attribute fg, vga_attribute bg)
+{
theme_color = (bg << 4 | fg) << 8;
}
-void tty_put_char(char chr) {
- if (chr == '\n') {
- TTY_COLUMN = 0;
- TTY_ROW++;
- }
- else if (chr == '\r') {
- TTY_COLUMN = 0;
- }
- else {
- *(buffer + TTY_COLUMN + TTY_ROW * TTY_WIDTH) = (theme_color | chr);
- TTY_COLUMN++;
- if (TTY_COLUMN >= TTY_WIDTH) {
- TTY_COLUMN = 0;
- TTY_ROW++;
- }
+void
+tty_put_char(char chr)
+{
+ switch (chr) {
+ case '\t':
+ tty_x += 4;
+ break;
+ case '\n':
+ tty_x = 0;
+ tty_y++;
+ break;
+ case '\r':
+ tty_x = 0;
+ break;
+ default:
+ *(buffer + tty_x + tty_y * TTY_WIDTH) = (theme_color | chr);
+ tty_x++;
+ break;
}
- if (TTY_ROW >= TTY_HEIGHT) {
+ if (tty_x >= TTY_WIDTH) {
+ tty_x = 0;
+ tty_y++;
+ }
+ if (tty_y >= TTY_HEIGHT) {
tty_scroll_up();
- TTY_ROW--;
- }
+ }
}
-void tty_put_str(char* str) {
+void
+tty_put_str(char* str)
+{
while (*str != '\0') {
tty_put_char(*str);
str++;
}
}
-void tty_scroll_up() {
- // TODO use memcpy
+void
+tty_scroll_up()
+{
+ size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
+ memcpy(buffer, buffer + TTY_WIDTH, last_line);
+ for (size_t i = 0; i < TTY_WIDTH; i++) {
+ *(buffer + i + last_line) = theme_color;
+ }
+ tty_y = tty_y == 0 ? 0 : tty_y - 1;
}
-void tty_clear() {
- for (uint32_t x = 0; x < TTY_WIDTH; x++) {
- for (uint32_t y = 0; y < TTY_HEIGHT; y++) {
- *(buffer + x + y * TTY_WIDTH) = theme_color;
- }
+void
+tty_clear()
+{
+ for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
+ *(buffer + i) = theme_color;
}
}
\ No newline at end of file
--- /dev/null
+#include <stdint.h>
+#include <libc/string.h>
+
+void*
+memcpy(void* dest, const void* src, size_t num)
+{
+ uint8_t* dest_ptr = (uint8_t*)dest;
+ const uint8_t* src_ptr = (const uint8_t*)src;
+ for (size_t i = 0; i < num; i++) {
+ *(dest_ptr + i) = *(src_ptr + i);
+ }
+ return dest;
+}
+
+void*
+memmove(void* dest, const void* src, size_t num)
+{
+ uint8_t* dest_ptr = (uint8_t*)dest;
+ const uint8_t* src_ptr = (const uint8_t*)src;
+ if (dest_ptr < src_ptr) {
+ for (size_t i = 0; i < num; i++) {
+ *(dest_ptr + i) = *(src_ptr + i);
+ }
+ } else {
+ for (size_t i = num; i != 0; i--) {
+ *(dest_ptr + i - 1) = *(src_ptr + i - 1);
+ }
+ }
+ return dest;
+}
+
+void*
+memset(void* ptr, int value, size_t num)
+{
+ uint8_t* c_ptr = (uint8_t*)ptr;
+ for (size_t i = 0; i < num; i++) {
+ *(c_ptr + i) = (uint8_t)value;
+ }
+ return ptr;
+}
+
+int
+memcmp(const void* ptr1, const void* ptr2, size_t num)
+{
+ uint8_t* p1 = (uint8_t*)ptr1;
+ uint8_t* p2 = (uint8_t*)ptr2;
+ for (size_t i = 0; i < num; i++) {
+ int diff = *(p1 + i) - *(p2 + i);
+ if (diff != 0) {
+ return diff;
+ }
+ }
+ return 0;
+}
\ No newline at end of file
--- /dev/null
+#include <libc/string.h>
+
+size_t
+strlen(const char* str)
+{
+ size_t len = 0;
+ while (str[len])
+ len++;
+ return len;
+}
\ No newline at end of file
CC := i686-elf-gcc
AS := i686-elf-as
-O := -O3
+O := -O2
W := -Wall -Wextra
CFLAGS := -std=gnu99 -ffreestanding $(O) $(W)
LDFLAGS := -ffreestanding $(O) -nostdlib -lgcc
--- /dev/null
+,lxsky,lunaixsky-PC,12.02.2022 15:08,file:///home/lxsky/.config/libreoffice/4;
\ No newline at end of file