selfmade-os

changeset 1:42ba76f77035 tip

the kernel with output and mem alloc
author meillo@marmaro.de
date Sun, 01 Nov 2009 23:50:51 +0100
parents 99db6262c157
children
files io.c io.h kernel.c makefile mem.c mem.h
diffstat 6 files changed, 301 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/io.c	Sun Nov 01 23:50:51 2009 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +#include <stdarg.h>
     1.5 +#include "io.h"
     1.6 +
     1.7 +void prints(const char* str);
     1.8 +
     1.9 +static unsigned short* vidmem = (unsigned short*) 0xb8000;
    1.10 +static unsigned short color = 0x0700;
    1.11 +static int pos = 0;
    1.12 +
    1.13 +void
    1.14 +cls(void)
    1.15 +{
    1.16 +	unsigned short* v;
    1.17 +
    1.18 +	for (v = vidmem; v < vidmem+(80*25); v++) {
    1.19 +		*v = 0;
    1.20 +	}
    1.21 +	pos = 0;
    1.22 +}
    1.23 +
    1.24 +
    1.25 +void
    1.26 +setcolor(int fg, int bg)
    1.27 +{
    1.28 +	color = (char) ((fg<<2) & (bg<<3));
    1.29 +}
    1.30 +
    1.31 +void
    1.32 +setpos(int line, int col)
    1.33 +{
    1.34 +	pos = line * 80 + col;
    1.35 +}
    1.36 +
    1.37 +void
    1.38 +relpos(int move)
    1.39 +{
    1.40 +	pos += move;
    1.41 +}
    1.42 +
    1.43 +
    1.44 +void
    1.45 +printp(unsigned int n)
    1.46 +{
    1.47 +	int i, h;
    1.48 +	char str[11];
    1.49 +	//int n = (int) p;
    1.50 +
    1.51 +	str[0] = '0';
    1.52 +	str[1] = 'x';
    1.53 +	str[10] = '\0';
    1.54 +	for (i=9; i>1; i--) {
    1.55 +		h = n % 16;
    1.56 +		str[i] = ('0' + h);
    1.57 +		if (h > 9) {
    1.58 +			str[i] += 'a' - '9' - 1;
    1.59 +		}
    1.60 +		n /= 16;
    1.61 +	}
    1.62 +	prints(str);
    1.63 +}
    1.64 +
    1.65 +
    1.66 +
    1.67 +
    1.68 +void
    1.69 +printc(char c)
    1.70 +{
    1.71 +	putchar(c);
    1.72 +}
    1.73 +
    1.74 +void
    1.75 +prints(const char* str)
    1.76 +{
    1.77 +	const char* c;
    1.78 +
    1.79 +	for (c = str; *c; c++) {
    1.80 +		printc(*c);
    1.81 +	}
    1.82 +}
    1.83 +
    1.84 +void
    1.85 +printd(int n)
    1.86 +{
    1.87 +	int i;
    1.88 +	char str[32];
    1.89 +
    1.90 +	str[31] = '\0';
    1.91 +	for (i=30; i>0; i--) {
    1.92 +		str[i] = ('0' + n%10);
    1.93 +		if (n == 0) {
    1.94 +			i++;
    1.95 +			break;
    1.96 +		}
    1.97 +		n /= 10;
    1.98 +	}
    1.99 +	prints(&str[i]);
   1.100 +}
   1.101 +
   1.102 +
   1.103 +int
   1.104 +putchar(int c)
   1.105 +{
   1.106 +	if (c == '\n') {
   1.107 +		pos = ((pos/80)+1) * 80;
   1.108 +	} else {
   1.109 +		vidmem[pos++] = (unsigned short)c | color;
   1.110 +	}
   1.111 +	return (unsigned char)c;
   1.112 +}
   1.113 +
   1.114 +int
   1.115 +puts(const char* s)
   1.116 +{
   1.117 +	prints(s);
   1.118 +	putchar('\n');
   1.119 +	return 1;
   1.120 +}
   1.121 +
   1.122 +int
   1.123 +printf(const char* fmt, ...)
   1.124 +{
   1.125 +	va_list ap;
   1.126 +	int i;
   1.127 +
   1.128 +	va_start(ap, fmt);
   1.129 +	for (i=0; fmt[i]; i++) {
   1.130 +		if (fmt[i] != '%') {
   1.131 +			printc(fmt[i]);
   1.132 +			continue;
   1.133 +		}
   1.134 +		switch (fmt[++i]) {
   1.135 +		case '%':
   1.136 +			printc('%');
   1.137 +			break;
   1.138 +		case 'c':
   1.139 +			printc((char)va_arg(ap, int));
   1.140 +			break;
   1.141 +		case 's':
   1.142 +			prints(va_arg(ap, char*));
   1.143 +			break;
   1.144 +		case 'd':
   1.145 +			printd(va_arg(ap, int));
   1.146 +			break;
   1.147 +		case 'p':
   1.148 +			printp(va_arg(ap, unsigned int));
   1.149 +			break;
   1.150 +		}
   1.151 +	}
   1.152 +	va_end(ap);
   1.153 +	return 0;
   1.154 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/io.h	Sun Nov 01 23:50:51 2009 +0100
     2.3 @@ -0,0 +1,9 @@
     2.4 +
     2.5 +void cls(void);
     2.6 +void setcolor(int fg, int bg);
     2.7 +void setpos(int line, int col);
     2.8 +void relpos(int move);
     2.9 +
    2.10 +int putchar(int c);
    2.11 +int puts(const char* s);
    2.12 +int printf(const char* fmt, ...);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/kernel.c	Sun Nov 01 23:50:51 2009 +0100
     3.3 @@ -0,0 +1,31 @@
     3.4 +/* markus schnalke <meillo@marmaro.de> */
     3.5 +
     3.6 +#include "io.h"
     3.7 +#include "mem.h"
     3.8 +
     3.9 +void
    3.10 +kmain(void)
    3.11 +{
    3.12 +	unsigned char* p;
    3.13 +
    3.14 +	cls();
    3.15 +	printf("OS says: Hi!\n\n");
    3.16 +
    3.17 +	initmem(0x00400000);  /* heap starts at 4MB */
    3.18 +
    3.19 +	printf("init done\n");
    3.20 +	printf("\n");
    3.21 +
    3.22 +	p = malloc(28);
    3.23 +	printf("allocated %d bytes at %p\n", 28, p);
    3.24 +	p = malloc(15);
    3.25 +	printf("allocated %d bytes at %p\n", 15, p);
    3.26 +
    3.27 +	printf("aa %d bb %c cc %s\n", 5, '@', "hallo");
    3.28 +	printf("minmem: %p\n", 0x00400000);
    3.29 +
    3.30 +	setpos(24, 0);
    3.31 +	printf("loop\n");
    3.32 +	while (1) {
    3.33 +	}
    3.34 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/makefile	Sun Nov 01 23:50:51 2009 +0100
     4.3 @@ -0,0 +1,28 @@
     4.4 +
     4.5 +CC = i386-elf-gcc
     4.6 +CFLAGS = -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
     4.7 +
     4.8 +LD = i386-elf-ld
     4.9 +LDFLAGS = -T linker.ld
    4.10 +
    4.11 +OBJ =  loader.o kernel.o io.o mem.o
    4.12 +
    4.13 +qemu: os.iso
    4.14 +	qemu -m 16 -boot cd -cdrom os.iso
    4.15 +
    4.16 +os.iso: kernel
    4.17 +	cp kernel os-isofiles/boot/kernel
    4.18 +	genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o os.iso os-isofiles
    4.19 +
    4.20 +loader.o: loader.s
    4.21 +	yasm -f elf -o loader.o loader.s
    4.22 +
    4.23 +kernel: linker.ld $(OBJ)
    4.24 +	i386-elf-ld -T linker.ld -o kernel $(OBJ)
    4.25 +
    4.26 +clean:
    4.27 +	rm -f *.o
    4.28 +	rm -f kernel
    4.29 +
    4.30 +distclean: clean
    4.31 +	rm -f os.iso
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/mem.c	Sun Nov 01 23:50:51 2009 +0100
     5.3 @@ -0,0 +1,74 @@
     5.4 +#include "mem.h"
     5.5 +#include "io.h"
     5.6 +
     5.7 +
     5.8 +void
     5.9 +initmem(unsigned int memstart)
    5.10 +{
    5.11 +	unsigned int* p;
    5.12 +	int step = 2*1024*1024;
    5.13 +	unsigned int* zero = 0x0;
    5.14 +
    5.15 +
    5.16 +	minmem = (unsigned char*)memstart;
    5.17 +
    5.18 +	/* disable cache */
    5.19 +	__asm__ __volatile__ (
    5.20 +		"movl %cr0, %eax\n\t"
    5.21 +		"movl $0x60000000, %ebx\n\t"
    5.22 +		"orl %ebx, %eax\n\t"
    5.23 +		"movl %eax, %cr0"
    5.24 +	);
    5.25 +
    5.26 +	/* test mem size */
    5.27 +	p = (unsigned int*) ((unsigned int)minmem/sizeof(char));
    5.28 +	do {
    5.29 +		p += step;
    5.30 +		*p = 0xf0f0f0f0;
    5.31 +	} while (*p == 0xf0f0f0f0);
    5.32 +	maxmem = (unsigned char*)p;
    5.33 +
    5.34 +	/* test mem mirroring */
    5.35 +	p = 0;
    5.36 +	*p = 0;
    5.37 +	p++;
    5.38 +	while (p < (unsigned int*)minmem) {
    5.39 +		p = (unsigned int*) ((unsigned int)p * 2);
    5.40 +	}
    5.41 +	while (p < (unsigned int*)maxmem) {
    5.42 +		*p = 0xf0f0f0f0;
    5.43 +		if (*zero == 0xf0f0f0f0) {
    5.44 +			maxmem = (unsigned char*)p;
    5.45 +			break;
    5.46 +		}
    5.47 +		p = (unsigned int*) ((unsigned int)p * 2);
    5.48 +	}
    5.49 +
    5.50 +	/* enable cache again */
    5.51 +	__asm__ __volatile__ (
    5.52 +		"movl %cr0, %eax\n\t"
    5.53 +		"movl $0x60000000, %ebx \n\t"
    5.54 +		"negl %ebx\n\t"
    5.55 +		"andl %ebx, %eax\n\t"
    5.56 +		"movl %eax, %cr0"
    5.57 +	);
    5.58 +
    5.59 +	/* min, max */
    5.60 +	printf("mem: start=%p end=%p size=%p\n", minmem, maxmem, (void*)(maxmem-minmem));
    5.61 +
    5.62 +	/* init heap */
    5.63 +	newp = minmem;
    5.64 +}
    5.65 +
    5.66 +
    5.67 +void*
    5.68 +malloc(unsigned int size)
    5.69 +{
    5.70 +	if (newp + size > maxmem) {
    5.71 +		printf("Fatal error: memory exhausted!\n");
    5.72 +		while (1) {
    5.73 +		}
    5.74 +	}
    5.75 +	newp += size;
    5.76 +	return newp - size;
    5.77 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/mem.h	Sun Nov 01 23:50:51 2009 +0100
     6.3 @@ -0,0 +1,8 @@
     6.4 +
     6.5 +unsigned char* newp;
     6.6 +unsigned char* maxmem;
     6.7 +unsigned char* minmem;
     6.8 +
     6.9 +void initmem(unsigned int memstart);
    6.10 +void* malloc(unsigned int size);
    6.11 +