kern

changeset 0:662ff2170531

starting the kernel
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 01 Dec 2010 22:02:42 +0200
parents
children ebe5e0e44a9d
files Makefile src/klibc/inttypes.h src/klibc/stdlib.h src/klibc/string.c src/klibc/string.h src/main.c src/mboot.S src/vid.c src/vid.h
diffstat 9 files changed, 141 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Wed Dec 01 22:02:42 2010 +0200
     1.3 @@ -0,0 +1,25 @@
     1.4 +# collect all of our C and assembly source files
     1.5 +csrc = $(wildcard src/*.c) $(wildcard src/klibc/*.c)
     1.6 +asmsrc = $(wildcard src/*.S) $(wildcard src/klibc/*.S)
     1.7 +
     1.8 +# each source file will generate one object file
     1.9 +obj = $(csrc:.c=.o) $(asmsrc:.S=.o)
    1.10 +
    1.11 +CC = gcc
    1.12 +
    1.13 +# -nostdinc instructs the compiler to ignore standard include directories
    1.14 +# -m32 instructs the compiler to produce 32bit code (in case we have a 64bit compiler)
    1.15 +CFLAGS = -m32 -Wall -g -nostdinc -fno-builtin -Isrc -Isrc/klibc
    1.16 +ASFLAGS = -m32 -g -nostdinc -fno-builtin -Isrc -Isrc/klibc
    1.17 +
    1.18 +bin = kernel.elf
    1.19 +
    1.20 +# default target: make an ELF binary by linking the object files
    1.21 +# we need to specify where to assume the text segment (code) is going
    1.22 +# in memory, as well as the kernel entry point (kstart).
    1.23 +$(bin): $(obj)
    1.24 +	ld -melf_i386 -o $@ -Ttext 0x200000 -e _start $(obj)
    1.25 +
    1.26 +.PHONY: clean
    1.27 +clean:
    1.28 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/klibc/inttypes.h	Wed Dec 01 22:02:42 2010 +0200
     2.3 @@ -0,0 +1,12 @@
     2.4 +#ifndef INTTYPES_H_
     2.5 +#define INTTYPES_H_
     2.6 +
     2.7 +typedef char int8_t;
     2.8 +typedef short int16_t;
     2.9 +typedef int int32_t;
    2.10 +
    2.11 +typedef unsigned char uint8_t;
    2.12 +typedef unsigned short uint16_t;
    2.13 +typedef unsigned int uint32_t;
    2.14 +
    2.15 +#endif	/* INTTYPES_H_ */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/klibc/stdlib.h	Wed Dec 01 22:02:42 2010 +0200
     3.3 @@ -0,0 +1,9 @@
     3.4 +#ifndef STDLIB_H_
     3.5 +#define STDLIB_H_
     3.6 +
     3.7 +#include <inttypes.h>
     3.8 +
     3.9 +typedef int32_t ssize_t;
    3.10 +typedef uint32_t size_t;
    3.11 +
    3.12 +#endif	/* STDLIB_H_ */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/klibc/string.c	Wed Dec 01 22:02:42 2010 +0200
     4.3 @@ -0,0 +1,11 @@
     4.4 +#include <string.h>
     4.5 +
     4.6 +void memset(void *s, int c, size_t sz)
     4.7 +{
     4.8 +	int i;
     4.9 +	char *ptr = s;
    4.10 +
    4.11 +	for(i=0; i<sz; i++) {
    4.12 +		*ptr++ = c;
    4.13 +	}
    4.14 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/klibc/string.h	Wed Dec 01 22:02:42 2010 +0200
     5.3 @@ -0,0 +1,8 @@
     5.4 +#ifndef STRING_H_
     5.5 +#define STRING_H_
     5.6 +
     5.7 +#include <stdlib.h>
     5.8 +
     5.9 +void memset(void *s, int c, size_t sz);
    5.10 +
    5.11 +#endif	/* STRING_H_ */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/main.c	Wed Dec 01 22:02:42 2010 +0200
     6.3 @@ -0,0 +1,10 @@
     6.4 +/*#include <stdio.h>*/
     6.5 +#include "vid.h"
     6.6 +
     6.7 +void kmain(void)
     6.8 +{
     6.9 +	clear_scr();
    6.10 +	put_char('a', 0, 0, 4, 0);
    6.11 +	put_char('b', 40, 12, 1, 7);
    6.12 +	/*printf("Hello world from kernel space\n");*/
    6.13 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/mboot.S	Wed Dec 01 22:02:42 2010 +0200
     7.3 @@ -0,0 +1,32 @@
     7.4 +#define MAGIC		0x1badb002
     7.5 +#define FLAGS		0
     7.6 +#define STACK_SIZE	0x4000
     7.7 +
     7.8 +	.text
     7.9 +	.globl _start
    7.10 +_start:
    7.11 +	/* jump over the multiboot header onto the startup code */
    7.12 +	jmp kentry
    7.13 +
    7.14 +	.align 4
    7.15 +	/* multiboot header */
    7.16 +mboot_hdr:
    7.17 +	.long MAGIC
    7.18 +	.long FLAGS
    7.19 +	.long -(MAGIC + FLAGS)	/* checksum */
    7.20 +	.fill 5, 4	/* fill out the rest with zeroes */
    7.21 +
    7.22 +kentry:
    7.23 +	/* setup a temporary kernel stack */
    7.24 +	movl $(stack + STACK_SIZE), %esp
    7.25 +	/* reset eflags register */
    7.26 +	pushl $0
    7.27 +	popf
    7.28 +	/* call the kernel main function */
    7.29 +	call kmain
    7.30 +	/* we dropped out of main, halt the CPU */
    7.31 +	cli
    7.32 +	hlt
    7.33 +
    7.34 +	/* space for the temporary kernel stack */
    7.35 +	.comm stack, STACK_SIZE
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/vid.c	Wed Dec 01 22:02:42 2010 +0200
     8.3 @@ -0,0 +1,26 @@
     8.4 +#include <string.h>
     8.5 +#include "vid.h"
     8.6 +
     8.7 +#define WIDTH	80
     8.8 +#define HEIGHT	25
     8.9 +static uint16_t *vmem = (uint16_t*)0xb8000;
    8.10 +
    8.11 +void clear_scr(void)
    8.12 +{
    8.13 +	memset(vmem, 0, WIDTH * HEIGHT * 2);
    8.14 +}
    8.15 +
    8.16 +void set_cursor(int x, int y)
    8.17 +{
    8.18 +	if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
    8.19 +		/* disable cursor */
    8.20 +		return;
    8.21 +	}
    8.22 +	/* set cursor position */
    8.23 +}
    8.24 +
    8.25 +void put_char(char c, int x, int y, int fg, int bg)
    8.26 +{
    8.27 +	uint16_t attr = (fg & 0xf) | ((bg & 7) << 4);
    8.28 +	vmem[y * 80 + x] = (uint16_t)c | (attr << 8);
    8.29 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/vid.h	Wed Dec 01 22:02:42 2010 +0200
     9.3 @@ -0,0 +1,8 @@
     9.4 +#ifndef VID_H_
     9.5 +#define VID_H_
     9.6 +
     9.7 +void clear_scr(void);
     9.8 +void set_cursor(int x, int y);
     9.9 +void put_char(char c, int x, int y, int fg, int bg);
    9.10 +
    9.11 +#endif	/* VID_H_ */