Skip to content

Commit e2adcd3

Browse files
committed
Initial code
1 parent 635cc8c commit e2adcd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2160
-1
lines changed

README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
1-
# kernel_basic_system
1+
Kernel Basic System (C codes).
2+
================================================================================
3+
4+
The README is used to introduce the tool and provide instructions on
5+
how to install the tool, any machine dependencies it may have (for
6+
example C compilers and installed libraries) and any other information
7+
that should be provided before the tool is installed.
8+
9+
INSTALLATION
10+
11+
To install this tool type the following:
12+
13+
* Generate iso image file
14+
* Generate new Virtual Machine (by VirtualBox) and add iso file
15+
* Type command make run (testing)
16+
17+
and follow instructions.
18+
19+
DEPENDENCIES
20+
21+
This tool requires these other modules and libraries:
22+
23+
None
24+
25+
COPYRIGHT AND LICENCE
26+
27+
Copyright (C) 2017 by https://github.com/vroncevic/kernel_basic_system
28+
29+
This tool is free software; you can redistribute it and/or modify it.
30+
31+
![alt tag](https://raw.githubusercontent.com/vroncevic/kernel_basic_system/master/cc++_logo.jpg)
32+
![alt tag](https://raw.githubusercontent.com/vroncevic/kernel_basic_system/master/linux_logo.jpg)
33+

cc++_logo.jpg

30.1 KB
Loading

linux_logo.jpg

36.4 KB
Loading

src/.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*]
2+
indent_style = tab
3+
tab_width = 4

src/Makefile

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#
2+
# @brief Main entry point of make process
3+
# @version ver.1.0
4+
# @date Sun May 21 22:11:00 CEST 2017
5+
# @company None, free software to use 2017
6+
# @author Vladimir Roncevic <elektron.ronca@gmail.com>
7+
#
8+
9+
include compiler.mk
10+
include assembler.mk
11+
include linker.mk
12+
include objects.mk
13+
14+
all: $(OBJECTS) kernel.bin kernel.iso
15+
16+
%.o: %.c
17+
@echo
18+
@echo Compiling module $<
19+
gcc $(GCC_FLAGS) -o $@ -c $<
20+
21+
loader.o: loader.asm
22+
@echo
23+
@echo Compiling module $<
24+
nasm $(NASM_FLAGS) -o $@ $<
25+
26+
kernel.bin: link.ld
27+
@echo
28+
@echo Linking modules
29+
ld $(LD_FLAGS) -T $< -o $@ $(OBJECTS)
30+
31+
kernel.iso: kernel.bin
32+
@echo
33+
@mkdir iso
34+
@mkdir iso/boot
35+
@mkdir iso/boot/grub
36+
@cp $< iso/boot/$<
37+
@echo 'set timeout=0' > iso/boot/grub/grub.cfg
38+
@echo 'set default=0' >> iso/boot/grub/grub.cfg
39+
@echo '' >> iso/boot/grub/grub.cfg
40+
@echo 'menuentry "MyOS" {' >> iso/boot/grub/grub.cfg
41+
@echo ' multiboot /boot/kernel.bin' >> iso/boot/grub/grub.cfg
42+
@echo ' boot' >> iso/boot/grub/grub.cfg
43+
@echo '}' >> iso/boot/grub/grub.cfg
44+
grub-mkrescue --output=$@ iso
45+
@chmod 755 $@
46+
@rm -rf iso
47+
48+
run: kernel.iso
49+
@echo
50+
(killall VirtualBox && sleep 1) || true
51+
VirtualBox --startvm "MyOS" &
52+
@echo
53+
54+
clean:
55+
@echo
56+
rm -f $(OBJECTS) *.bin *.iso
57+
@echo
58+

src/assembler.mk

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# @brief Assembly flags
3+
# @version ver.1.0
4+
# @date Sun May 21 22:11:00 CEST 2017
5+
# @company None, free software to use 2017
6+
# @author Vladimir Roncevic <elektron.ronca@gmail.com>
7+
#
8+
9+
NASM_FLAGS = -f elf32
10+

src/compiler.mk

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# @brief Compiler flags
3+
# @version ver.1.0
4+
# @date Sun May 21 22:11:00 CEST 2017
5+
# @company None, free software to use 2017
6+
# @author Vladimir Roncevic <elektron.ronca@gmail.com>
7+
#
8+
9+
GCC_FLAGS = \
10+
-m32 \
11+
-Wall \
12+
-O \
13+
-fleading-underscore \
14+
-fstrength-reduce \
15+
-fomit-frame-pointer \
16+
-finline-functions \
17+
-nostdinc \
18+
-fno-builtin
19+

src/gdt.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2+
/*
3+
* gdt.h
4+
* Copyright (C) 2017 Vladimir Roncevic <elektron.ronca@gmail.com>
5+
*
6+
* kernel is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the
8+
* Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* kernel is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#ifndef __GDT_H
20+
#define __GDT_H
21+
22+
#include "types.h"
23+
24+
struct gdt_entry {
25+
uint16_t limit_low;
26+
uint16_t base_low;
27+
uint8_t base_middle;
28+
uint8_t access;
29+
uint8_t granularity;
30+
uint8_t base_high;
31+
} __attribute__((packed));
32+
33+
struct gdt_ptr {
34+
uint16_t limit;
35+
uint32_t base;
36+
} __attribute__((packed));
37+
38+
extern struct gdt_entry gdt[3];
39+
extern struct gdt_ptr gp;
40+
41+
extern void gdt_set_gate(int32_t num, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran);
42+
extern void gdt_install(void);
43+
extern void gdt_flush();
44+
45+
#endif
46+

src/gdt_install.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2+
/*
3+
* gdt_install.c
4+
* Copyright (C) 2017 Vladimir Roncevic <elektron.ronca@gmail.com>
5+
*
6+
* kernel is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the
8+
* Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* kernel is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include "gdt.h"
20+
21+
void gdt_install(void) {
22+
gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
23+
gp.base = (uint32_t)&gdt;
24+
gdt_set_gate(0, 0, 0, 0, 0);
25+
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
26+
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
27+
gdt_flush();
28+
}
29+

src/gdt_set_gate.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2+
/*
3+
* gdt_set_gate.c
4+
* Copyright (C) 2017 Vladimir Roncevic <elektron.ronca@gmail.com>
5+
*
6+
* kernel is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the
8+
* Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* kernel is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include "gdt.h"
20+
21+
struct gdt_entry gdt[3];
22+
struct gdt_ptr gp;
23+
24+
void gdt_set_gate(int32_t num, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran) {
25+
gdt[num].base_low = (base & 0xFFFF);
26+
gdt[num].base_middle = (base >> 16) & 0xFF;
27+
gdt[num].base_high = (base >> 24) & 0xFF;
28+
gdt[num].limit_low = (limit & 0xFFFF);
29+
gdt[num].granularity = ((limit >> 16) & 0x0F);
30+
gdt[num].granularity |= (gran & 0xF0);
31+
gdt[num].access = access;
32+
}
33+

src/idt.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2+
/*
3+
* idt.h
4+
* Copyright (C) 2017 Vladimir Roncevic <elektron.ronca@gmail.com>
5+
*
6+
* kernel is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the
8+
* Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* kernel is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#ifndef __IDT_H
20+
#define __IDT_H
21+
22+
#include "types.h"
23+
24+
struct idt_entry {
25+
uint16_t base_lo;
26+
uint16_t sel;
27+
uint8_t always0;
28+
uint8_t flags;
29+
uint16_t base_hi;
30+
} __attribute__((packed));
31+
32+
struct idt_ptr {
33+
uint16_t limit;
34+
uint32_t base;
35+
} __attribute__((packed));
36+
37+
extern struct idt_entry idt[256];
38+
extern struct idt_ptr idtp;
39+
40+
extern void idt_set_gate(
41+
uint8_t num, uint64_t base, uint16_t sel, uint8_t flags
42+
);
43+
extern void idt_load(void);
44+
extern void idt_install(void);
45+
46+
#endif
47+

src/idt_install.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2+
/*
3+
* idt_install.c
4+
* Copyright (C) 2017 Vladimir Roncevic <elektron.ronca@gmail.com>
5+
*
6+
* kernel is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the
8+
* Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* kernel is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include "idt.h"
20+
#include "memory.h"
21+
22+
struct idt_entry idt[256];
23+
struct idt_ptr idtp;
24+
25+
void idt_install(void) {
26+
idtp.limit = (sizeof(struct idt_entry) * 256) - 1;
27+
idtp.base = (uint32_t)&idt;
28+
memset(&idt, 0, sizeof(struct idt_entry) * 256);
29+
idt_load();
30+
}
31+

src/idt_set_gate.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2+
/*
3+
* idt_set_gate.c
4+
* Copyright (C) 2017 Vladimir Roncevic <elektron.ronca@gmail.com>
5+
*
6+
* kernel is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the
8+
* Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* kernel is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include "idt.h"
20+
21+
void idt_set_gate(uint8_t num, uint64_t base, uint16_t sel, uint8_t flags) {
22+
idt[num].base_lo = (base & 0xFFFF);
23+
idt[num].base_hi = (base >> 16) & 0xFFFF;
24+
idt[num].sel = sel;
25+
idt[num].always0 = 0;
26+
idt[num].flags = flags;
27+
}
28+

src/inportb.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2+
/*
3+
* inportb.c
4+
* Copyright (C) 2017 Vladimir Roncevic <elektron.ronca@gmail.com>
5+
*
6+
* kernel is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the
8+
* Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* kernel is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include "port_io.h"
20+
21+
uint8_t inportb(uint16_t _port) {
22+
uint8_t rv;
23+
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
24+
return rv;
25+
}
26+

0 commit comments

Comments
 (0)