Skip to content

Commit 795309d

Browse files
committed
Add base source code
1 parent 6b8691b commit 795309d

File tree

465 files changed

+32385
-1
lines changed

Some content is hidden

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

465 files changed

+32385
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ How to Make a Computer Operating System
1313

1414
The goal is to build a very simple UNIX-based operating system in C++, but the goal is not to just build a "proof-of-concept". The OS should be able to boot, start an userland shell and be extensible.
1515

16+
[![Screen](https://raw.github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/master/preview.png)](https://raw.github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/master/preview.png)
17+
1618
### Summary
1719

1820
#### [Chapter 1: Introduction about the x86 architecture and about our OS](Chapter-1/README.md)

preview.png

107 KB
Loading

src/Makefile

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
SDKDIR=./sdk
2+
3+
help:
4+
@echo "Makefile for Building Dev Operating System."
5+
@echo "Usage: make [ all | clean | help | build | run] "
6+
@echo ""
7+
@echo
8+
9+
all:
10+
@echo "Building Kernel"
11+
make -C ./kernel
12+
@echo "Building SDK"
13+
make -C ./sdk
14+
@echo "Building Userland"
15+
make -C ./userland
16+
17+
18+
build:
19+
zip -r devos-$(VERSION).zip ./
20+
21+
22+
run:
23+
@echo "Running Dev Operating System."
24+
cd ./sdk && sudo ./diskimage.sh
25+
cd ./sdk && ./qemu.sh
26+
27+
clean:
28+
make -C ./kernel clean
29+
make -C ./userland clean

src/Vagrantfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,4 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
124124
# chef-validator, unless you changed the configuration.
125125
#
126126
# chef.validation_client_name = "ORGNAME-validator"
127-
end
127+
end

src/kernel/Makefile

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
ARCH=x86
2+
KERNEL=kernel.elf
3+
SDKDIR=../sdk
4+
INCDIR= -I ./ -I ./modules -I ./core -I ./arch/$(ARCH)
5+
6+
7+
include ./arch/$(ARCH)/config.make
8+
9+
include ./runtime/Makefile
10+
include ./core/Makefile
11+
include ./modules/Makefile
12+
include ./arch/$(ARCH)/Makefile
13+
14+
FLAG :=$(FLAG) -D__$(ARCH)__
15+
PLATFORMS= `find ./arch/ -type d | sed "s/.*\///" | sort`
16+
17+
18+
all: $(KERNEL)
19+
20+
$(KERNEL): $(OBJS)
21+
$(LD) $(LDFLAG) -o $@ $^
22+
cp $(KERNEL) $(SDKDIR)/bootdisk/
23+
24+
help:
25+
@echo "Makefile for Kernel."
26+
@echo "Please see COPYING for licensing information."
27+
@echo "Output should be: "$(KERNEL)
28+
@echo "Usage: make [ all | clean] "
29+
@echo "Currently supported platforms:"
30+
@echo $(PLATFORMS)
31+
@echo
32+
33+
tosdk:
34+
cp $(KERNEL) $(SDKDIR)/disk/
35+
36+
install:
37+
sudo cp $(KERNEL) /boot/
38+
39+
debug:
40+
$(NM) -n $(KERNEL)
41+
42+
43+
hinfo:
44+
$(OBJDUMP) -f $(KERNEL)
45+
46+
dasm:
47+
$(OBJDUMP) -d $(KERNEL) > dasm.txt
48+
49+
50+
run:
51+
cd $(SDKDIR) && sh ./diskimage.sh
52+
cd $(SDKDIR) && sh ./qemu.sh
53+
54+
geniso:
55+
cd $(SDKDIR) && sh ./cdrom.sh
56+
57+
%.o: %.cc
58+
$(SC) $(FLAG) -c $< -o $@
59+
60+
%.o: %.S
61+
$(SC) $(FLAG) -c $< -o $@
62+
63+
%.o: %.asm
64+
$(ASM) $(ASMFLAG) -c $< -o $@
65+
66+
67+
clean:
68+
rm -f $(OBJS) $(KERNEL) dasm.txt
69+
70+
71+

src/kernel/arch/x86/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
OBJS:= arch/$(ARCH)/start.o $(OBJS) arch/$(ARCH)/alloc.o arch/$(ARCH)/architecture.o \
2+
arch/$(ARCH)/io.o arch/$(ARCH)/vmm.o arch/$(ARCH)/x86.o arch/$(ARCH)/switch.o arch/$(ARCH)/x86int.o

src/kernel/arch/x86/alloc.cc

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
2+
#include <os.h>
3+
4+
5+
extern "C" {
6+
7+
/* change size of a memory segment */
8+
void *ksbrk(int n)
9+
{
10+
struct kmalloc_header *chunk;
11+
char *p_addr;
12+
int i;
13+
14+
if ((kern_heap + (n * PAGESIZE)) > (char *) KERN_HEAP_LIM) {
15+
io.print
16+
("PANIC: ksbrk(): no virtual memory left for kernel heap !\n");
17+
return (char *) -1;
18+
}
19+
20+
chunk = (struct kmalloc_header *) kern_heap;
21+
22+
/* Allocation d'une page libre */
23+
for (i = 0; i < n; i++) {
24+
p_addr = get_page_frame();
25+
if ((int)(p_addr) < 0) {
26+
io.print
27+
("PANIC: ksbrk(): no free page frame available !\n");
28+
return (char *) -1;
29+
}
30+
31+
/* Ajout dans le repertoire de pages */
32+
pd0_add_page(kern_heap, p_addr, 0);
33+
34+
kern_heap += PAGESIZE;
35+
}
36+
37+
/* Marquage pour kmalloc */
38+
chunk->size = PAGESIZE * n;
39+
chunk->used = 0;
40+
41+
return chunk;
42+
}
43+
44+
/* allocate memory block */
45+
void *kmalloc(unsigned long size)
46+
{
47+
if (size==0)
48+
return 0;
49+
50+
unsigned long realsize; /* taille totale de l'enregistrement */
51+
struct kmalloc_header *chunk, *other;
52+
53+
if ((realsize =
54+
sizeof(struct kmalloc_header) + size) < KMALLOC_MINSIZE)
55+
realsize = KMALLOC_MINSIZE;
56+
57+
/*
58+
* On recherche un bloc libre de 'size' octets en parcourant le HEAP
59+
* kernel a partir du debut
60+
*/
61+
chunk = (struct kmalloc_header *) KERN_HEAP;
62+
while (chunk->used || chunk->size < realsize) {
63+
if (chunk->size == 0) {
64+
io.print
65+
("\nPANIC: kmalloc(): corrupted chunk on %x with null size (heap %x) !\nSystem halted\n",
66+
chunk, kern_heap);
67+
//error
68+
asm("hlt");
69+
return 0;
70+
}
71+
72+
chunk =
73+
(struct kmalloc_header *) ((char *) chunk +
74+
chunk->size);
75+
76+
if (chunk == (struct kmalloc_header *) kern_heap) {
77+
if ((int)(ksbrk((realsize / PAGESIZE) + 1)) < 0) {
78+
io.print
79+
("\nPANIC: kmalloc(): no memory left for kernel !\nSystem halted\n");
80+
asm("hlt");
81+
return 0;
82+
}
83+
} else if (chunk > (struct kmalloc_header *) kern_heap) {
84+
io.print
85+
("\nPANIC: kmalloc(): chunk on %x while heap limit is on %x !\nSystem halted\n",
86+
chunk, kern_heap);
87+
asm("hlt");
88+
return 0;
89+
}
90+
}
91+
92+
/*
93+
* On a trouve un bloc libre dont la taille est >= 'size'
94+
* On fait de sorte que chaque bloc est une taille minimale
95+
*/
96+
if (chunk->size - realsize < KMALLOC_MINSIZE)
97+
chunk->used = 1;
98+
else {
99+
other =
100+
(struct kmalloc_header *) ((char *) chunk + realsize);
101+
other->size = chunk->size - realsize;
102+
other->used = 0;
103+
104+
chunk->size = realsize;
105+
chunk->used = 1;
106+
}
107+
108+
kmalloc_used += realsize;
109+
110+
/* retourne un pointeur sur la zone de donnees */
111+
return (char *) chunk + sizeof(struct kmalloc_header);
112+
}
113+
114+
/* free memory block */
115+
void kfree(void *v_addr)
116+
{
117+
if (v_addr==(void*)0)
118+
return;
119+
120+
struct kmalloc_header *chunk, *other;
121+
122+
/* On libere le bloc alloue */
123+
chunk =
124+
(struct kmalloc_header *) ((u32)v_addr -
125+
sizeof(struct kmalloc_header));
126+
chunk->used = 0;
127+
128+
kmalloc_used -= chunk->size;
129+
130+
/*
131+
* On merge le bloc nouvellement libere avec le bloc suivant ci celui-ci
132+
* est aussi libre
133+
*/
134+
while ((other =
135+
(struct kmalloc_header *) ((char *) chunk + chunk->size))
136+
&& other < (struct kmalloc_header *) kern_heap
137+
&& other->used == 0)
138+
chunk->size += other->size;
139+
}
140+
141+
142+
143+
}

src/kernel/arch/x86/alloc.o

7.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)