1. Paweł Wieczorek
  2. Impala

Commits

Paweł Wieczorek  committed e8ba63f

  • Participants
  • Branches default

Comments (0)

Files changed (87)

File BUILD

View file
+Zbudowanie systemu wymaga odpowiedniego srodowiska:
+	IMPALA_SRCROOT      katalog glowny ze zrodlami
+	IMPALA_ARCH         architektura (x86)
+	IMPALA_MK           $IMAPAL_SRCROOT/mk
+
+Dodatkowe zmienne:
+	BINUTILS_PREFIX     Domyslnie ""
+	GCC_PREFIX          Domyslnie ""
+
+
+Poprawne definicje sa w conf/DEFS.sh. W przypadku powlok
+kompatybilnych z Bourne Shell mozna posluzyc sie poleceniem
+	$ source conf/DEFS.sh
+w celu zdefiniowania poprawnego srodowiska.
+
+
+

File ChangeLog

View file
+2008-12-27 Pawel Wieczorek <wieczyk@gmail.com> rev14:
+	* Dodano interfejs kthread do watkow po stronie jadra
+	* Dodano scheduling oparty na round-robin
+
+
+2008-12-25 Pawel Wieczorek <wieczyk@gmail.com> rev1:
+	* Przerobiona Impala :)
+

File Makefile

View file
+SUBDIRS= sys
+
+build-image: build
+	cp image/root/boot/impala image/root/boot/impala.old
+	cp sys/kern/impala image/root/boot/
+	cd image && sudo ./`uname`.sh
+
+run: build-image
+	cd image && qemu -fda floppy.img
+
+
+include ${IMPALA_MK}/subdir.mk

File conf/DEFS.sh

View file
+export IMPALA_ARCH=x86
+export IMPALA_SRCROOT=$PWD
+export IMPALA_MK=$IMPALA_SRCROOT/mk

File conf/wieczyk.sh

View file
+export IMPALA_ARCH=x86
+export IMPALA_SRCROOT=$PWD
+export IMPALA_MK=$IMPALA_SRCROOT/mk
+export BINUTILS_PREFIX=/usr/local/cross-i386-elf/bin/i386-elf-
+

File doc/impala.jpg

Added
New image

File doc/logo.txt

View file
+impala.jpg z http://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Black-faced_impala.jpg/240px-Black-faced_impala.jpg
+

File image/FreeBSD.sh

View file
+#!/bin/sh
+
+mdconfig -a -t vnode -f floppy.img -u 4
+mount_msdosfs /dev/md4 fd
+cp root/boot/impala* fd/boot/
+cp root/boot/grub/menu.lst fd/boot/grub/
+umount fd
+mdconfig -d -u 4
+

File image/blank/fat16grub.img

Binary file added.

File image/floppy.img

Binary file added.

File image/impala-usr-local.bochs

View file
+# configuration file generated by Bochs
+config_interface: textconfig
+display_library: x
+megs: 32
+romimage: file="/usr/local/share/bochs/BIOS-bochs-latest"
+vgaromimage: file="/usr/local/share/bochs/VGABIOS-lgpl-latest"
+boot: floppy
+floppy_bootsig_check: disabled=0
+floppya: 1_44="floppy.img", status=inserted
+# no floppyb
+ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
+ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15
+ata2: enabled=0
+ata3: enabled=0
+parport1: enabled=1, file=""
+parport2: enabled=0
+com1: enabled=1, mode=null, dev=""
+com2: enabled=0
+com3: enabled=0
+com4: enabled=0
+usb1: enabled=0
+i440fxsupport: enabled=1
+vga_update_interval: 40000
+vga: extension=vbe
+cpu: count=1:1:1, ips=2000000, quantum=5, reset_on_triple_fault=1
+text_snapshot_check: enabled=0
+private_colormap: enabled=0
+clock: sync=none, time0=local
+# no cmosimage
+ne2k: enabled=0
+pnic: enabled=0
+sb16: enabled=0
+# no loader
+log: -
+logprefix: %t%e%d
+debugger_log: -
+panic: action=ask
+error: action=report
+info: action=report
+debug: action=ignore
+pass: action=fatal
+keyboard_type: mf
+keyboard_serial_delay: 250
+keyboard_paste_delay: 100000
+keyboard_mapping: enabled=0, map=
+user_shortcut: keys=none
+mouse: enabled=0, type=ps2

File image/impala-usr.bochs

View file
+# configuration file generated by Bochs
+config_interface: textconfig
+display_library: x
+megs: 32
+romimage: file="/usr/share/bochs/BIOS-bochs-latest"
+vgaromimage: file="/usr/share/bochs/VGABIOS-lgpl-latest"
+boot: floppy
+floppy_bootsig_check: disabled=0
+floppya: 1_44="floppy.img", status=inserted
+# no floppyb
+ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
+ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15
+ata2: enabled=0
+ata3: enabled=0
+parport1: enabled=1, file=""
+parport2: enabled=0
+com1: enabled=1, mode=null, dev=""
+com2: enabled=0
+com3: enabled=0
+com4: enabled=0
+usb1: enabled=0
+i440fxsupport: enabled=1
+vga_update_interval: 40000
+vga: extension=vbe
+cpu: count=1:1:1, ips=2000000, quantum=5, reset_on_triple_fault=1
+text_snapshot_check: enabled=0
+private_colormap: enabled=0
+clock: sync=none, time0=local
+# no cmosimage
+ne2k: enabled=0
+pnic: enabled=0
+sb16: enabled=0
+# no loader
+log: -
+logprefix: %t%e%d
+debugger_log: -
+panic: action=ask
+error: action=report
+info: action=report
+debug: action=ignore
+pass: action=fatal
+keyboard_type: mf
+keyboard_serial_delay: 250
+keyboard_paste_delay: 100000
+keyboard_mapping: enabled=0, map=
+user_shortcut: keys=none
+mouse: enabled=0, type=ps2

File image/linux.sh

View file
+#!/bin/sh
+
+if [ $UID -ne 0 ]; then
+	echo "Only root can do that..."
+	exit 0
+fi
+
+set -x
+
+mount -o loop floppy.img fd
+mv root/boot/impala root/boot/impala.old
+cp ../kernel/impala root/boot/impala
+cp root/boot/impala* fd/boot/
+cp root/boot/grub/menu.lst fd/boot/grub/
+sync
+umount fd

File image/root/boot/grub/e2fs_stage1_5

Binary file added.

File image/root/boot/grub/fat_stage1_5

Binary file added.

File image/root/boot/grub/ffs_stage1_5

Binary file added.

File image/root/boot/grub/iso9660_stage1_5

Binary file added.

File image/root/boot/grub/jfs_stage1_5

Binary file added.

File image/root/boot/grub/menu.lst

View file
+default 0
+
+title ImpalaOS current kernel
+	root (fd0)
+	kernel /boot/impala
+
+title ImpalaOS previous kernel
+	root (fd0)
+	kernel /boot/impala.old
+

File image/root/boot/grub/minix_stage1_5

Binary file added.

File image/root/boot/grub/reiserfs_stage1_5

Binary file added.

File image/root/boot/grub/stage1

Binary file added.

File image/root/boot/grub/stage2

Binary file added.

File image/root/boot/grub/ufs2_stage1_5

Binary file added.

File image/root/boot/grub/vstafs_stage1_5

Binary file added.

File image/root/boot/grub/xfs_stage1_5

Binary file added.

File image/root/boot/impala

Binary file added.

File image/root/boot/impala.old

Binary file added.

File mk/build.mk

View file
+.SUFFIXES: .c.o .s.o .S.o
+
+include ${IMPALA_MK}/config.mk
+
+_BUILD_BUILD= build
+_BUILD_CLEAN= clean
+_BUILD_DEPEND= depend
+
+OBJS_1= ${SRCS:%.c=%.o}
+OBJS_2= ${OBJS_1:%.s=%.o}
+OBJS= ${OBJS_2}
+
+
+.c.o:
+	@echo "> compiling $@"
+	@${CC} -c ${C_FLAGS} -o $@ $<
+
+.s.o:
+	@echo "> compiling $@"
+	@${AS} ${AS_FLAGS} -o $@ $<
+
+

File mk/config.mk

View file
+.SUFFIXES: .c.o .s.o .S.o
+GCC_PREFIX?=
+BINUTILS_PREFIX?=
+CC=${GCC_PREFIX}gcc
+INCDIR=  -I ${IMPALA_SRCROOT}/sys -I ${IMPALA_SRCROOT}/sys/arch/${IMPALA_ARCH}/
+C_FLAGS_=-m32 -std=c99 -fno-stack-protector -nostdinc -Wall -Wstrict-prototypes -Wmissing-prototypes -Werror ${INCDIR}\
+	 -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow
+C_FLAGS?= ${C_FLAGS_}
+
+AS= ${BINUTILS_PREFIX}as
+AS_FLAGS= -32
+
+AR=${BINUTILS_PREFIX}ar
+
+STRIP=${BINUTILS_PREFIX}strip
+AR=${BINUTILS_PREFIX}ar
+RANLIB=${BINUTILS_PREFIX}ranlib
+
+LD=${BINUTILS_PREFIX}ld
+LIBDIR?=
+LD_FLAGS=-nostdlib -T ${LD_SCRIPT} --cref -Map ${LD_MAP} ${LIBDIR}
+
+
+

File mk/kernel.mk

View file
+_KERNEL_BUILD= build
+_KERNEL_CLEAN= clean
+_KERNEL_DEPEND= depend
+
+C_FLAGS=-D__KERNEL ${C_FLAGS_}
+
+LIBS?=
+KERNEL?= kernel
+LD_MAP?= ${KERNEL}.map
+LD_SCRIPT?= ${IMPALA_SRCROOT}/sys/conf/kernel.ld
+LIBDIR= -L ${IMPALA_SRCROOT}/sys/libkutil -L${IMPALA_SRCROOT}/sys/arch/${IMPALA_ARCH}/
+
+include ${IMPALA_SRCROOT}/mk/build.mk
+${_KERNEL_BUILD}: ${KERNEL}
+
+
+${_KERNEL_CLEAN}:
+	rm -f ${OBJS} ${KERNEL}
+
+
+${KERNEL}: ${LIBDEPS} ${OBJS}
+	@echo "> linking ${KERNEL}"
+	@${LD} ${LD_FLAGS} -o ${KERNEL} ${OBJS} ${LIBS}
+
+
+${_LIB_CLEAN}:
+	rm -f ${OBJS} ${KERNEL}
+

File mk/lib.mk

View file
+_LIB_BUILD= build
+_LIB_CLEAN= clean
+_LIB_DEPEND= depend
+
+LIBRARY?= libnoname
+
+include ${IMPALA_SRCROOT}/mk/build.mk
+
+${_LIB_BUILD}: ${LIBRARY}.a
+
+${LIBRARY}.a: ${OBJS}
+	@echo "> archiving ${LIBRARY}.a"
+	@${AR} r ${LIBRARY}.a ${OBJS}
+	@${RANLIB} ${LIBRARY}.a
+
+${_LIB_CLEAN}:
+	rm -f ${OBJS} ${LIBRARY}.a
+

File mk/subdir.mk

View file
+
+_SUBDIR_BUILD?= build
+_SUBDIR_CLEAN?= clean
+_SUBDIR_DEPEND?= depend
+
+
+#
+# GNU Make lubi drukowac do jakiego katalogu aktualnie wchodzi i z jakiego wychodzi.
+# Psuje to troche estetycznosc komunikatow, jakie drukuja te skrypty Makefile, zatem
+# ustawiam zmienna MAKELEVEL na "" za kazdym razem aby zmylic GNU Make (uzywa tej
+# zmiennej srodowiskowej do rozpoznawania, czy jest procesem 'wchodzacym' gdzies,
+# czy jest uruchomionym przez uzytkownika
+#			-- Pawel Wieczorek
+#
+
+${_SUBDIR_BUILD}:
+	@for d in ${SUBDIRS};\
+		do echo "===> ${DIRPRFX}$$d (build)";\
+		   cd $$d; DIRPRFX="${DIRPRFX}$$d/" MAKELEVEL="" ${MAKE} build; cd ..;\
+	done
+
+${_SUBDIR_CLEAN}:
+	@for d in ${SUBDIRS};\
+		do echo "===> ${DIRPRFX}$$d (clean)";\
+			cd $$d; DIRPRFX="${DIRPRFX}$$d/" MAKELEVEL="" ${MAKE} clean; cd ..;\
+	done
+
+${_SUBDIR_DEPEND}:
+	@for d in ${SUBDIRS};\
+		do echo "===> ${DIRPRFX}$$d (depend)";\
+			cd $$d; DIRPRFX="${DIRPRFX}$$d/" MAKELEVEL="" ${MAKE} depend; cd ..\
+	done
+

File sys/Makefile

View file
+SUBDIRS= arch libkutil dev kern
+
+include ${IMPALA_MK}/subdir.mk
+

File sys/arch/Makefile

View file
+SUBDIRS= ${IMPALA_ARCH}
+
+include ${IMPALA_MK}/subdir.mk

File sys/arch/x86/Makefile

View file
+LIBRARY= libarch
+C_FLAGS=-D__KERNEL ${C_FLAGS_} 
+
+SRCS=\
+	arch/asmstub.s\
+	arch/bootstrap.s\
+	arch/init.c\
+	arch/interrupt.c\
+	arch/isr.s\
+	arch/thread.c\
+    dev/i8254.c\
+	dev/i8259a.c\
+	dev/video.c\
+
+
+
+
+
+
+
+
+include ${IMPALA_MK}/lib.mk

File sys/arch/x86/arch/asmstub.s

View file
+
+.set text_selector, 0x08
+.set data_selector, 0x10
+
+
+.global gdt_load
+.global ldt_load
+.global idt_load
+.global tr_load
+.global tss_save
+.global jmp_sel
+.global thread_context_store
+.global thread_context_load
+
+.macro offset32 name, num
+.equ \name, \num*4
+.endm
+
+
+offset32    CTX_ESP,    0
+offset32    CTX_EBP,    1
+offset32    CTX_EFLAGS, 2
+offset32    CTX_CR3,    3
+
+thread_context_load:
+    enter $0, $0
+    movl 8(%ebp), %edi
+    movl CTX_CR3(%edi), %eax
+    movl %eax, %cr3
+    movl CTX_ESP(%edi), %esp
+    movl CTX_EBP(%edi), %ebp
+    pushl CTX_EFLAGS(%edi)
+    popfl
+    movl $0x0, %eax
+    leave
+    ret
+
+
+thread_context_store:
+    enter $0, $0
+    movl 8(%ebp), %edi
+    movl %ebp, CTX_EBP(%edi)
+    movl %esp, CTX_ESP(%edi)
+    movl %cr3, %eax
+    movl %eax, CTX_CR3(%edi)
+    pushfl
+    popl CTX_EFLAGS(%edi)
+    movl $0x1, %eax
+    leave
+    ret
+
+gdt_load:
+	enter $0,$0
+	movl 8(%ebp), %eax
+	lgdt (%eax)
+	movl $data_selector, %eax
+	movl %eax, %ds
+	movl %eax, %es
+	movl %eax, %fs
+	movl %eax, %gs
+    movl %eax, %ss
+	ljmp $text_selector, $gdt_load.1
+	movl %eax, %cr0
+    or $0x1, %eax
+    movl %cr0, %eax
+gdt_load.1:
+	leave
+	ret
+
+ldt_load:
+	movl 4(%esp), %eax
+	lldt (%eax)
+	ret
+
+idt_load:
+    movl 4(%esp), %eax
+	lidt (%eax)
+	ret
+
+tr_load:
+    ltr 4(%esp)
+    ret
+
+jmp_sel:
+    movl 4(%esp), %eax
+    pushl $0x0
+    pushl %eax
+    ljmp *-4(%esp)
+    addl $8, %esp
+    ret
+
+.global far_copy_in
+# funkcja kopiujaca pomiedzy wybranym selektorem
+# segmentu a obecnie uzywanym selektorem danych
+# sluzy do kopiowania np danych z segmentu uzytkownika
+# do segmentu jadra
+far_copy_in:
+    enter $0, $0
+    pushl %es   
+    pushl %ds
+    pushl %edi
+    pushl %esi
+    movw %ds, %ax
+    movw %ax, %es
+    movl 8(%ebp), %edi  # dst
+    movw 12(%ebp), %ds  # src sel
+    movl 16(%ebp), %esi # src
+    movl 20(%ebp), %ecx # len
+    rep movsb           # skopiuje ECX bajtow z DS:ESI do ES:EDI
+    popl %esi
+    popl %edi
+    popl %ds
+    popl %es
+    leave
+    ret
+

File sys/arch/x86/arch/bootstrap.s

View file
+.global kernel_entrypoint
+
+.set ALIGN,    1<<0       
+.set MEMINFO,  1<<1
+.set FLAGS,    ALIGN
+.set MAGIC,    0x1BADB002
+.set CHECKSUM, -(MAGIC + FLAGS)
+
+.align 4
+.long MAGIC
+.long FLAGS
+.long CHECKSUM
+
+.set STACKSIZE, 0x4000
+.comm stack, STACKSIZE, 32
+
+kernel_entrypoint:
+    movl $(stack + STACKSIZE), %esp
+    push %eax
+    push %ebx
+    call init_x86
+    call kmain
+    hlt
+

File sys/arch/x86/arch/init.c

View file
+#include <sys/types.h>
+#include <machine/descriptor.h>
+#include <machine/tss.h>
+#include <machine/i8259a.h>
+#include <machine/i8254.h>
+#include <machine/interrupt.h>
+#include <machine/cpu.h>
+#include <sys/libkutil.h>
+#include <sys/thread.h>
+
+void init_x86(void);
+void _unhnd_intrpt(void);
+void _intrpt_syscall(void);
+
+extern uintptr_t trap_table[];
+extern uintptr_t irq_table[];
+
+static void setgdt(int, uintptr_t, uint, uint, uint);
+static void setidt(int i, uint selector, uintptr_t offset, uint access);
+static descriptor p_gdt[SEL_MAX];
+static descriptor p_idt[0x100];
+static task_state_segment p_tss0;
+static descriptor_register p_gdtr;
+static descriptor_register p_idtr;
+
+
+void
+init_x86()
+{
+    enum {
+        code = GATE_PRESENT | GATE_TYPE_RX,
+        ucode = code | GATE_DPL3,
+        data = GATE_PRESENT | GATE_TYPE_RW,
+        udata = data | GATE_DPL3,
+        attr = GATE_PAGEGRAN | GATE_OP32,
+        tss0 = GATE_PRESENT | GATE_TYPE_TASK,
+        intrpt_attr = GATE_PRESENT | GATE_TYPE_INTRPT,
+        trap_attr = GATE_PRESENT | GATE_TYPE_TRAP
+    };
+    int i;
+
+    // Ustawienie GDT
+    mem_zero(&p_gdt, sizeof(p_gdt));
+    mem_zero(&p_tss0, sizeof(p_tss0));
+    setgdt(SEL_CODE, 0x0, 0xfffff, code, attr);
+    setgdt(SEL_DATA, 0x0, 0xfffff, data, attr);
+    setgdt(SEL_UCODE, 0x0, 0xffff, ucode, attr);
+    setgdt(SEL_UDATA, 0x0, 0xffff, udata, attr);
+    setgdt(SEL_TSS0, (uintptr_t)&p_tss0, sizeof(p_tss0), tss0, 0);
+
+    mem_zero(&p_gdtr, sizeof(p_gdtr));
+    p_gdtr.base = &p_gdt;
+    p_gdtr.limit = sizeof(p_gdt) -1;
+    gdt_load(&p_gdtr);
+
+    tr_load(SEL_MK(SEL_TSS0, SEL_DPL0));
+
+    // Ustawienie IDT
+    for (i = 0; i < 0x100; i++) {
+        setidt(i, SEL_MK(SEL_CODE, SEL_DPL0), (uintptr_t)&_unhnd_intrpt, intrpt_attr);
+    }
+
+    for (i = 0; i < 0x20; i++) {
+        setidt(i, SEL_MK(SEL_CODE, SEL_DPL0), trap_table[i], trap_attr);
+    }
+
+    for (i = 0; i <= 23; i++) {
+        setidt(i+0x20, SEL_MK(SEL_CODE, SEL_DPL0), irq_table[i], intrpt_attr);
+    }
+
+    setidt(INTRPT_SYSCALL, SEL_MK(SEL_CODE, SEL_DPL3), (uintptr_t)_intrpt_syscall, intrpt_attr);
+
+    mem_zero(&p_idtr, sizeof(p_idtr));
+    p_idtr.base = &p_idt;
+    p_idtr.limit = sizeof(p_idt)-1;
+    idt_load(&p_idtr);
+    i8259a_init();
+    i8254_init();
+    __asm__("sti");
+
+}
+
+void
+setidt(int i, uint selector, uintptr_t offset,
+    uint access)
+{
+    gate_descr *entry = &p_idt[i].gdescr;
+    entry->offset_low = (uint)offset & 0xffff;
+    entry->offset_high = ((uint)offset >> 16);
+    entry->selector = selector;
+    entry->notused = 0;
+    entry->access = access;
+}
+
+
+void
+setgdt(int i, uintptr_t base, uint limit,
+    uint acc, uint atrib)
+{
+    segment_descr *entry = &p_gdt[i].sdescr;
+    entry->base_low = base & 0xffff;
+    entry->base_mid = (base >> 16) & 0xff;
+    entry->base_high = (base >> 24);
+    entry->limit_low = limit & 0xffff;
+    entry->attr = (limit >> 16) & 0xff;
+    entry->attr |= atrib;
+    entry->access = acc;
+}
+

File sys/arch/x86/arch/interrupt.c

View file
+#include <sys/types.h>
+#include <machine/interrupt.h>
+#include <machine/i8259a.h>
+#include <sys/kprintf.h>
+#include <sys/thread.h>
+#include <sys/syscall.h>
+
+void ISR_irq(interrupt_frame f);
+void ISR_syscall(interrupt_frame frame);
+void TRAP_unhandled(void);
+void TRAP_gfault(void);
+
+irq_handler_f *irq_handlers[MAX_IRQ];
+
+void irq_install_handler(int irq, irq_handler_f *f)
+{
+    if (irq <= MAX_IRQ) {
+         irq_handlers[irq] = f;
+         i8259a_irq_enable(irq);
+    }
+}
+
+
+void
+irq_done()
+{
+    __asm__("sti");
+    i8259a_send_eoi();
+}
+
+void
+ISR_irq(interrupt_frame frame)
+{
+    bool eoi = FALSE;
+    if (frame.f_n <= MAX_IRQ) {
+        eoi = irq_handlers[frame.f_n]();
+    }
+    if (!eoi) i8259a_send_eoi();
+}
+
+void
+ISR_syscall(interrupt_frame frame)
+{
+    va_list ap;
+    ap = (va_list) (frame.f_esp+4);
+    curthread->thr_flags |= THREAD_SYSCALL;
+    syscall(curthread, frame.f_eax, ap);
+    curthread->thr_flags ^= THREAD_SYSCALL;
+}
+
+void
+TRAP_unhandled()
+{
+}
+
+void
+TRAP_gfault()
+{
+    kprintf("General protection fault\n");
+    __asm__("hlt");
+}

File sys/arch/x86/arch/isr.s

View file
+.text
+.global _intrpt_syscall
+
+.set text_selector, 0x08
+.set data_selector, 0x10
+
+
+.macro PUSH_FRAME
+    pushl %ds
+    pushl %es
+    pushl %fs
+    pushl %gs
+    pushal
+
+.endm
+
+.macro SET_KERNEL_REGS
+    movl $data_selector, %eax
+    movl %eax, %ds
+    movl %eax, %es
+    movl %eax, %fs
+    movl %eax, %gs
+.endm
+
+.macro POP_FRAME
+    popal
+    pop %gs
+    pop %fs
+    pop %es
+    pop %ds
+.endm
+
+.macro TRAP num, name, handler
+\name:
+    cli
+    push $\num
+    PUSH_FRAME
+    SET_KERNEL_REGS
+    call \handler
+    POP_FRAME
+    add $4, %esp
+    sti
+    iret
+.endm
+
+.macro TRAPNE num, name, handler
+\name:
+    cli
+    push $0
+    push $\num
+    PUSH_FRAME
+    SET_KERNEL_REGS
+    call \handler
+    POP_FRAME
+    add $8, %esp
+    sti
+    iret
+.endm
+
+
+.macro ISR num, name, handler
+    TRAP \num, \name, \handler
+.endm
+
+.global _unhnd_intrpt
+
+
+TRAP 0, _unhnd_intrpt, TRAP_unhandled
+
+TRAP 0, _trap0, TRAP_unhandled
+TRAP 1, _trap1, TRAP_unhandled
+TRAP 2, _trap2, TRAP_unhandled
+TRAP 3, _trap3, TRAP_unhandled
+TRAP 4, _trap4, TRAP_unhandled
+TRAP 5, _trap5, TRAP_unhandled
+TRAP 6, _trap6, TRAP_unhandled
+TRAP 7, _trap7, TRAP_unhandled
+TRAP 8, _trap8, TRAP_unhandled
+TRAP 9, _trap9, TRAP_unhandled
+TRAP 10, _trap10, TRAP_unhandled
+TRAP 11, _trap11, TRAP_unhandled
+TRAP 12, _trap12, TRAP_unhandled
+TRAP 13, _trap13, TRAP_gfault
+TRAP 14, _trap14, TRAP_unhandled
+TRAP 15, _trap15, TRAP_unhandled
+TRAP 16, _trap16, TRAP_unhandled
+
+TRAPNE 80, _intrpt_syscall, ISR_syscall
+
+ISR 0x00, _isr0, ISR_irq
+ISR 0x01, _isr1, ISR_irq
+ISR 0x02, _isr2, ISR_irq
+ISR 0x03, _isr3, ISR_irq
+ISR 0x04, _isr4, ISR_irq
+ISR 0x05, _isr5, ISR_irq
+ISR 0x06, _isr6, ISR_irq
+ISR 0x07, _isr7, ISR_irq
+ISR 0x08, _isr8, ISR_irq
+ISR 0x09, _isr9, ISR_irq
+ISR 0x0a, _isr10, ISR_irq
+ISR 0x0b, _isr11, ISR_irq
+ISR 0x0c, _isr12, ISR_irq
+ISR 0x0d, _isr13, ISR_irq
+ISR 0x0e, _isr14, ISR_irq
+ISR 0x0f, _isr15, ISR_irq
+ISR 0x10, _isr16, ISR_irq
+ISR 0x11, _isr17, ISR_irq
+ISR 0x12, _isr18, ISR_irq
+ISR 0x13, _isr19, ISR_irq
+ISR 0x14, _isr20, ISR_irq
+ISR 0x15, _isr21, ISR_irq
+ISR 0x16, _isr22, ISR_irq
+ISR 0x17, _isr23, ISR_irq
+
+
+.data
+.global irq_table
+.global trap_table
+
+#
+# Tablice wskaznikow uchwytow do przerwan i pulapek, dzieki nim
+# nie trzeba eksportowac symboli do nich.
+#
+
+irq_table:
+    .long _isr0
+    .long _isr1
+    .long _isr2
+    .long _isr3
+    .long _isr4
+    .long _isr5
+    .long _isr6
+    .long _isr7
+    .long _isr8
+    .long _isr9
+    .long _isr10
+    .long _isr11
+    .long _isr12
+    .long _isr13
+    .long _isr14
+    .long _isr15
+    .long _isr16
+    .long _isr17
+    .long _isr18
+    .long _isr19
+    .long _isr20
+    .long _isr21
+    .long _isr22
+    .long _isr23
+    .long 0
+
+trap_table:
+    .long _trap0
+    .long _trap1
+    .long _trap2
+    .long _trap3
+    .long _trap4
+    .long _trap5
+    .long _trap6
+    .long _trap7
+    .long _trap8
+    .long _trap9
+    .long _trap10
+    .long _trap11
+    .long _trap12
+    .long _trap13
+    .long _trap14
+    .long _trap15
+    .long _trap16
+    .long 0
+

File sys/arch/x86/arch/thread.c

View file
+#include <sys/types.h>
+#include <sys/thread.h>
+#include <machine/cpu.h>
+#include <sys/libkutil.h>
+#include <sys/kprintf.h>
+
+
+void
+thread_context_init(thread_context *ctx, int priv, addr_t ustack)
+{
+    mem_zero(ctx, sizeof(thread_context));
+    ctx->c_eflags = EFLAGS_BITS;
+    ctx->c_esp = (uint32_t) ustack + THREAD_STACK_SIZE-8;
+}
+
+void
+thread_switch(thread_t *t_to, thread_t *t_from)
+{
+    if (thread_context_store(&t_from->thr_context)) {
+        // Jestesmy w watku t_from
+        curthread = t_to;
+        if (t_to->thr_flags & THREAD_FRESH) {
+            thread_enter(t_to);
+        } else {
+            thread_context_load(&t_to->thr_context);
+        }
+    } else {
+        // Jestesmy ponownie w watku t_from
+        curthread = t_from;
+    }
+}
+
+void
+thread_enter(thread_t *t_to)
+{
+
+    typedef void (*entry_point)(void*);
+    entry_point entry;
+    t_to->thr_flags &= ~THREAD_FRESH;
+    entry = (entry_point) t_to->thr_entry_point;
+    __asm__ volatile ("movl %%eax, %%esp" : : "a" (t_to->thr_context.c_esp) : "%esp" );
+    entry(t_to->thr_entry_arg);
+    __asm__ volatile (" jmp .");
+}

File sys/arch/x86/dev/i8254.c

View file
+#include <sys/types.h>
+#include <sys/clock.h>
+#include <sys/kprintf.h>
+#include <machine/interrupt.h>
+#include <machine/i8254.h>
+#include <machine/i8259a.h>
+#include <machine/io.h>
+
+static bool i8254_irq0(void);
+
+void
+i8254_init()
+{
+    i8254_set_freq(100);
+    irq_install_handler(IRQ0, i8254_irq0);
+}
+
+bool
+i8254_irq0()
+{
+    clock_hardtick();
+    irq_done();
+    clock_softtick();
+    return TRUE;
+}
+
+void
+i8254_set_freq(uint hz)
+{
+    kprintf("i8254 interrupt timer: %uhz\n", hz);
+    // brakuje chyba wys�ania komendy resetuj�cej chipset
+
+    //TODO jakies ,,zaawansowane obliczenia'' co do HZ
+    io_out8(PIT_CHAN0, 0xff);
+    io_out8(PIT_CHAN0, 0x40);
+}
+

File sys/arch/x86/dev/i8259a.c

View file
+#include <sys/types.h>
+#include <machine/i8259a.h>
+#include <machine/io.h>
+#include <machine/interrupt.h>
+
+enum {
+    PIC_M = 0x20,
+    PIC_S = 0xA0
+};
+
+enum {
+    ICW1_ICW4       = 0x01,
+    ICW1_CASCADE    = 0x02,
+    ICW1_INTERVAL8  = 0x00,
+    ICW1_INTERVAL4  = 0x04,
+    ICW1_TRIG_EDGE  = 0x00,
+    ICW1_TRIG_LEVEL = 0x08,
+    ICW1_RESET      = 0x10
+};
+
+
+enum {
+    ICW4_8086          = 0x01,
+    ICW4_EOI_AUTO      = 0x02,
+    ICW4_EOI_NORMAL    = 0x00,
+    ICW4_BUF_NONE      = 0x00,
+    ICW4_BUF_MASTER    = 0x00,
+    ICW4_BUF_SLAVE     = 0x00,
+    ICW4_NESTED        = 0x80
+};
+
+enum {
+    OCW2_EOI_NORMAL = 0x20
+};
+
+static uchar pic1_mask;
+static uchar pic2_mask;
+
+
+void
+i8259a_init()
+{
+    // ICW1
+    io_out8(PIC_M, ICW1_RESET | ICW1_ICW4);
+    // ICW2
+    io_out8(PIC_M+1, INTERRUPT_VECTOR);   
+    // ICW3
+    io_out8(PIC_M+1, 0x04);
+    // ICW4
+    io_out8(PIC_M+1, ICW4_8086);
+
+    // ICW1
+    io_out8(PIC_S, ICW1_RESET | ICW1_ICW4);
+    // ICW2
+    io_out8(PIC_S+1, INTERRUPT_VECTOR+0x8);   
+    // ICW3
+    io_out8(PIC_S+1, 0x02);
+    // ICW4
+    io_out8(PIC_S+1, ICW4_8086);
+
+    pic1_mask = 0x0;
+    pic2_mask = 0x0;
+    i8259a_reset_mask();
+}
+
+
+void
+i8259a_reset_mask()
+{
+    io_out8(PIC_M+1, ~pic1_mask);
+    io_out8(PIC_S+1, ~pic2_mask);
+}
+
+void
+i8259a_irq_enable(int n)
+{
+    if (n < 0x8)
+        pic1_mask |= 1 << n;
+    else
+        pic2_mask |= 1 << (n-8);
+    i8259a_reset_mask();
+}
+
+void
+i8259a_irq_disable(int n)
+{
+    if (n < 0x8)
+        pic1_mask ^= 1 << n;
+    else
+        pic2_mask ^= 1 << (n-8);
+    i8259a_reset_mask();
+}
+
+void
+i8259a_send_eoi()
+{
+    io_out8(PIC_M, OCW2_EOI_NORMAL);
+    io_out8(PIC_S, OCW2_EOI_NORMAL);
+}
+

File sys/arch/x86/dev/video.c

View file
+// Mateusz Kocielski
+
+
+#include <sys/types.h>
+#include <sys/libkutil.h>
+#include <machine/video.h>
+#include <machine/io.h>
+
+static int8_t forced_attr = 0;
+
+
+void
+textscreen_enable_forced_attr(int8_t f)
+{
+    forced_attr = f;
+}
+
+void
+textscreen_disable_forced_attr()
+{
+    forced_attr = 0;
+}
+
+
+void
+textscreen_putat(struct hw_textscreen *screen, int8_t col, int8_t row, 
+        char c, int8_t attribute)
+{
+    screen->screen_map[TEXTSCREEN_WIDTH*row+col] = (uint16_t)attribute<<8|c;
+    return;
+}
+
+void
+textscreen_put(struct hw_textscreen *screen, char c, int8_t attr)
+{
+    if (forced_attr) attr = forced_attr;
+    textscreen_putat(screen, screen->cursor_x, screen->cursor_y, c, attr);
+
+    /* uaktualniamy pozycje kursora */
+
+    /* sprawdzamy czy jestesmy na koncu wiersza */
+    if ( screen->cursor_x == TEXTSCREEN_WIDTH-1 )
+    {
+        /* sprawdzamy czy mozna przenisc kursor do nastepnej linii */
+        if ( screen->cursor_y < TEXTSCREEN_WIDTH-1 )
+            /* przenosimy kursor do nastepnej linii */
+            textscreen_update_cursor(screen, 0, screen->cursor_y+1);
+        else
+            /* scrollujemy ekran i przenosimy kursor na poczatek linii */
+            textscreen_scroll(screen);
+    }
+    else
+        /* kursor nie jest na koncu wiersza wiec uaktualniamy o jedna pozycje
+         * w prawo */
+        textscreen_update_cursor(screen, screen->cursor_x+1, 
+                screen->cursor_y);
+
+    return;
+}
+
+void
+textscreen_update_cursor(struct hw_textscreen *screen, int8_t col,
+        int8_t row)
+{
+    screen->cursor_y = row;
+    screen->cursor_x = col;
+    return;
+}
+
+void
+textscreen_scroll(struct hw_textscreen *screen)
+{
+    /* przesuwamy ekran o linie w gore */
+    mem_move(screen->screen_map, &screen->screen_map[TEXTSCREEN_WIDTH],
+                24*TEXTSCREEN_WIDTH*sizeof(uint16_t));
+    
+    /* kasujemy ostatnia linie */
+    mem_zero(&screen->screen_map[24*TEXTSCREEN_WIDTH], 
+            TEXTSCREEN_WIDTH*sizeof(uint16_t));
+    
+    /* uaktualniamy kursor */
+    textscreen_update_cursor(screen, 0, screen->cursor_y);
+
+    return;
+}
+
+void
+textscreen_clear(struct hw_textscreen *screen)
+{
+    uint16_t i; /* zmienna pomocnicza */
+
+    /* ustawiamy domyslne atrybuty */
+    for ( i = 0 ; i < TEXTSCREEN_WIDTH*TEXTSCREEN_HEIGHT ; i++ )
+        screen->screen_map[i] = COLOR_WHITE<<8;
+
+    /* ustawiamy kursor na poczatek */
+    textscreen_update_cursor(screen, 0, 0);
+    return;
+}
+
+void
+textscreen_reset(struct hw_textscreen *screen)
+{
+    textscreen_clear(screen);
+    return;
+}
+
+void
+textscreen_draw(struct hw_textscreen *screen)
+{
+    uint16_t cur_pos = (screen->cursor_y) * TEXTSCREEN_WIDTH + 
+        screen->cursor_x; /* cursor position */
+
+    /* kopiowanie screen_map na ekran */
+    mem_move(TEXTSCREEN_VIDEO, screen->screen_map,
+            TEXTSCREEN_WIDTH*TEXTSCREEN_HEIGHT*sizeof(uint16_t));
+
+    /* uaktualnienie pozycji kursora */
+    io_out8(TEXTSCREEN_VIDPORT_IDX, 0x0f); /* mlodszy bajt */
+    io_out8(TEXTSCREEN_VIDPORT_DATA, (uint8_t)cur_pos);
+    io_out8(TEXTSCREEN_VIDPORT_IDX, 0x0e); /* starszy bajt */
+    io_out8(TEXTSCREEN_VIDPORT_DATA, cur_pos>>8);
+
+    return;
+}

File sys/arch/x86/machine/atomic.h

View file
+#ifndef __MACHINE_ATOMIC_H
+#define __MACHINE_ATOMIC_H
+
+// TODO: sprawdzi� czy dobrze zrobi�em te OUTPUT:INPUT:CLOBBERS
+
+
+// MPSAFE
+static inline uint
+atomic_change32(uint *addr, uint x)
+{
+  __asm__ volatile(
+    "xchgl %0, %1"
+    : "=r"(x), "=rg"(*addr)
+    : "r"(x)
+    : "memory"
+  );
+  return x;
+}
+
+// To chyba nie jest MPSAFE
+static inline uint
+atomic_fetch32(uint *addr)
+{
+    return *addr;
+}
+
+#endif
+

File sys/arch/x86/machine/cdefs.h

View file
+#ifndef __MACHINE_CDEFS_H
+#define __MACHINE_CDEFS_H
+
+typedef char *va_list;
+
+#define VA_START(ap, s) ap = (char *) &s
+#define VA_END(ap)
+#define VA_ARG(ap, type) *(type*)(ap += sizeof(type))
+
+
+
+#endif
+

File sys/arch/x86/machine/cpu.h

View file
+#ifndef __MACHINE_CPU_H
+#define __MACHINE_CPU_H
+
+enum CPU_EFLAGS {
+    EFLAGS_CS   = 1,
+    EFLAGS_PF   = 1 << 2,
+    EFLAGS_AF   = 1 << 4,
+    EFLAGS_ZF   = 1 << 6,
+    EFLAGS_SF   = 1 << 7,
+    EFLAGS_TF   = 1 << 8,
+    EFLAGS_IF   = 1 << 9,
+    EFLAGS_DF   = 1 << 10,
+    EFLAGS_OF   = 1 << 11,
+    EFLAGS_IOPL = (1 << 12) | (1 << 13),
+    EFLAGS_NT   = 1 << 14,
+    EFLAGS_RF   = 1 << 16,
+    EFLAGS_VM   = 1 << 17,
+    EFLAGS_AC   = 1 << 18,
+    EFLAGS_VIF  = 1 << 19,
+    EFLAGS_VIP  = 1 << 20,
+    EFLAGS_ID   = 1 << 21
+};
+
+enum CPU_EFLAGS_IOPL {
+    EFLAGS_DPL0 = 0,
+    EFLAGS_DPL1 = 1 << 12,
+    EFLAGS_DPL2 = 2 << 12,
+    EFLAGS_DPL3 = 3 << 12
+};
+#define EFLAGS_BITS 0x2
+
+
+// rejestry kontrolne
+enum CPU_CR0 {
+    CR0_PE = 1 << 0,
+    CR0_MP = 1 << 1,
+    CR0_EM = 1 << 2,
+    CR0_TS = 1 << 3,
+    CR0_ET = 1 << 4,
+    CR0_NE = 1 << 5,
+    CR0_WP = 1 << 16,
+    CR0_AM = 1 << 18,
+    CR0_NW = 1 << 29,
+    CR0_CD = 1 << 30,
+    CR0_PG = 1 << 31
+};
+
+enum CPU_CR3 {
+    CR3_PWT = 1 << 3,
+    CR3_PCD = 1 << 4
+};
+
+enum CPU_CR4 {
+    CR4_VME = 1 << 0,
+    CR4_PVI = 1 << 1,
+    CR4_TSD = 1 << 2,
+    CR4_DE  = 1 << 3,
+    CR4_PSE = 1 << 4,
+    CR4_PAE = 1 << 5,
+    CR4_MCE = 1 << 6,
+    CR4_PGE = 1 << 7,
+    CR4_PCE = 1 << 8,
+    CR4_VMXE= 1 << 13,
+    CR4_SMXE= 1 << 14
+};
+
+
+#endif
+
+
+

File sys/arch/x86/machine/descr.h

View file
+#ifndef __MACHINE_DESCRIPTOR_H
+#define __MACHINE_DESCRIPTOR_H
+
+typedef struct segmnt_descr segmnt_descr;
+struct segment_descr {
+    uint16_t    limit_low;
+    uint16_t    base_low;
+    uint8_t     base_mid;
+    uint8_t     access;
+    uint8_t     attr;
+    uint8_t     base_high;
+} __packed;
+
+typedef struct gate_descr gate_descr;
+struct gate_descr {
+    uint16_t    offset_low;
+    uint16_t    selector;
+    uint8_t     notused;
+    uint8_t     access;
+    uint16_t    offset_high;
+} __packed;
+
+typedef union descriptor descriptor;
+union descriptor {
+    segment_descr   sdescr;
+    gate_descr      gdescr;
+};
+
+
+
+#endif

File sys/arch/x86/machine/descriptor.h

View file
+#ifndef __MACHINE_DESCRIPTOR_H
+#define __MACHINE_DESCRIPTOR_H
+
+typedef struct segment_descr segment_descr;
+struct segment_descr {
+    uint16_t    limit_low;
+    uint16_t    base_low;
+    uint8_t     base_mid;
+    uint8_t     access;
+    uint8_t     attr;
+    uint8_t     base_high;
+} __packed;
+
+typedef struct gate_descr gate_descr;
+struct gate_descr {
+    uint16_t    offset_low;
+    uint16_t    selector;
+    uint8_t     notused;
+    uint8_t     access;
+    uint16_t    offset_high;
+} __packed;
+
+typedef union descriptor descriptor;
+union descriptor {
+    segment_descr   sdescr;
+    gate_descr      gdescr;
+};
+
+enum {
+  SEL_NULL,
+  SEL_CODE,
+  SEL_DATA,
+  SEL_UCODE,
+  SEL_UDATA,
+  SEL_TSS0,
+  SEL_MAX
+};
+
+typedef struct descriptor_register descriptor_register;
+struct descriptor_register {
+  uint16_t  limit;
+  addr_t    base;
+} __packed;
+
+#define SEL_MK(sel, flags) ((sel * 8) | flags)
+
+enum { 
+    SEL_DPL0    = 0x00,
+    SEL_DPL1    = 0x01,
+    SEL_DPL2    = 0x02,
+    SEL_DPL3    = 0x03
+};
+
+#define SEL_DPL0 0x0
+#define SEL_DPL1 0x1
+#define SEL_DPL2 0x2
+#define SEL_DPL3 0x3
+
+enum {
+    GATE_PRESENT    = 0x80,
+    GATE_PAGEGRAN   = 0x80,
+    GATE_OP32       = 0x40
+};
+
+enum {
+    GATE_DPL0   = 0x00,
+    GATE_DPL1   = 0x20,
+    GATE_DPL2   = 0x40,
+    GATE_DPL3   = 0x60
+};
+
+enum {
+    GATE_TYPE_TASK      = 0x09,
+    GATE_TYPE_INTRPT    = 0x0e,
+    GATE_TYPE_TRAP      = 0x0f,
+    GATE_TYPE_DATA      = 0x10,
+    GATE_TYPE_CODE      = 0x18,
+
+    // R - read
+    // W - write
+    // X - exec
+    // C - conform
+    // D - expand down
+    // order: RWXCD
+    GATE_TYPE_X         = GATE_TYPE_CODE,
+    GATE_TYPE_RX        = GATE_TYPE_X  | 0x02,
+    GATE_TYPE_XC        = GATE_TYPE_X  | 0x04,
+    GATE_TYPE_RXC       = GATE_TYPE_RX | 0x04,
+    GATE_TYPE_R         = GATE_TYPE_DATA,
+    GATE_TYPE_RD        = GATE_TYPE_R  | 0x04,
+    GATE_TYPE_RW        = GATE_TYPE_R  | 0x02,
+    GATE_TYPE_RWD       = GATE_TYPE_RW | 0x04
+};
+
+
+#ifdef __KERNEL
+void gdt_load(descriptor_register *r);
+void ldt_load(descriptor_register *r);
+void idt_load(descriptor_register *r);
+void tr_load(uint32_t sel);
+#endif
+
+
+#endif

File sys/arch/x86/machine/i8254.h

View file
+#ifndef __MACHINE_i8254_H
+#define __MACHINE_i8254_H
+
+enum {
+    PIT_CHAN0 = 0x40,
+    PIT_CHAN1,
+    PIT_CHAN2,
+    PIT_MODE
+};
+
+
+#ifdef __KERNEL
+
+void i8254_init(void);
+void i8254_set_freq(uint hz);
+
+#endif
+#endif
+

File sys/arch/x86/machine/i8259a.h

View file
+#ifndef __MACHINE_i8259A_H
+#define __MACHINE_i8259A_H
+
+enum {
+    PIC_IRQ0 = 0x01,
+    PIC_IRQ1 = 0x02,
+    PIC_IRQ2 = 0x04,
+    PIC_IRQ3 = 0x08,
+    PIC_IRQ4 = 0x10,
+    PIC_IRQ5 = 0x20,
+    PIC_IRQ6 = 0x40,
+    PIC_IRQ7 = 0x80
+};
+
+enum {
+    PIC_IRQ8 = 0x01,
+    PIC_IRQ9 = 0x02,
+    PIC_IRQ10 = 0x04,
+    PIC_IRQ11 = 0x08,
+    PIC_IRQ12 = 0x10,
+    PIC_IRQ13 = 0x20,
+    PIC_IRQ14 = 0x40,
+    PIC_IRQ15 = 0x80
+};
+
+#ifdef __KERNEL
+
+void i8259a_init(void);
+void i8259a_send_eoi(void);
+void i8259a_reset_mask(void);
+void i8259a_irq_enable(int n);
+void i8259a_irq_disable(int n);
+
+#endif
+
+#endif

File sys/arch/x86/machine/interrupt.h

View file
+#ifndef __MACHINE_INTERRUPT_H
+#define __MACHINE_INTERRUPT_H
+
+
+enum {
+    INTERRUPT_VECTOR = 0x20,
+    MAX_IRQ = 23,
+    INTRPT_SCHED = 0x70,
+    INTRPT_SYSCALL = 0x80
+};
+
+enum {
+    IRQ0 = 0,
+    IRQ1,
+    IRQ2,
+    IRQ3,
+    IRQ4,
+    IRQ5,
+    IRQ6,
+    IRQ7,
+    IRQ8,
+    IRQ9,
+    IRQ10,
+    IRQ11,
+    IRQ12,
+    IRQ13
+    // ...
+};
+
+
+typedef bool irq_handler_f(void);
+
+typedef struct interrupt_frame interrupt_frame;
+struct interrupt_frame {
+    // pusha
+    uint32_t f_edi;
+    uint32_t f_esi;
+    uint32_t f_ebp;
+    uint32_t f_isp;
+    uint32_t f_ebx;
+    uint32_t f_edx;
+    uint32_t f_ecx;
+    uint32_t f_eax;
+
+    // recznie
+    uint32_t f_gs;
+    uint32_t f_fs;
+    uint32_t f_es;
+    uint32_t f_ds;
+
+    uint32_t f_n;
+    uint32_t f_errno;
+    uint32_t f_eip;
+    uint32_t f_cs;
+
+    uint32_t f_eflags;
+    uint32_t f_esp;
+    uint32_t f_ss;
+} __packed;
+
+#ifdef __KERNEL
+void irq_install_handler(int irq, irq_handler_f *h);
+void irq_done(void);
+#endif
+
+
+#endif

File sys/arch/x86/machine/io.h

View file
+#ifndef __MACHINE_IO_H
+#define __MACHINE_IO_H
+
+#include <sys/types.h>
+
+// Lepiej w funkcjach ni� makrach - kompilator sobie sprawdzi typy,
+// a przy -O1 i tak powinien to zoptymalizowa� do jednej instrukcji.
+
+static inline uint32_t
+io_in32(uint16_t port)
+{
+    uint val;
+    __asm__ volatile("inl %%dx, %%eax":"=a" (val):"d" (port));
+    return val;
+}
+
+static inline void
+io_out32(uint16_t port, uint32_t val)
+{
+    __asm__ volatile("outl %%eax, %%dx"::"a" (val), "d" (port));
+}
+
+static inline uint16_t
+io_in16(uint16_t port)
+{
+    uint16_t val;
+    __asm__ volatile("inw %%dx, %%ax":"=a" (val):"d" (port));
+    return val;
+}
+
+static inline void
+io_out16(ushort port, uint16_t val)
+{
+    __asm__ volatile("outw %%ax, %%dx"::"a" (val), "d" (port));
+}
+
+static inline uint8_t
+io_in8(uint8_t port)
+{
+    uint8_t val;
+    __asm__ volatile("inb %%dx, %%al":"=a" (val):"d" (port));
+    return val;
+}
+
+
+static inline void
+io_out8(ushort port, uint8_t val)
+{
+    __asm__ volatile("outb %%al, %%dx"::"a" (val), "d" (port));
+}
+
+#endif
+
+

File sys/arch/x86/machine/thread.h

View file
+#ifndef __MACHINE_THREAD_H
+#define __MACHINE_THREAD_H
+
+
+typedef struct thread_context thread_context;
+struct thread_context {
+    uint32_t c_esp;
+    uint32_t c_ebp;
+    uint32_t c_eflags;
+    uint32_t c_cr3;
+    //TODO: dodac koprocesor matematyczny
+};
+
+#ifdef __KERNEL
+
+void thread_context_load(thread_context *ctx);
+int thread_context_store(thread_context *ctx);
+void thread_context_init(thread_context *ctx, int priv, addr_t ustack);
+void thread_enter(thread_t *t);
+void thread_switch(thread_t *t_to, thread_t *t_from);
+
+#endif
+
+
+
+#endif

File sys/arch/x86/machine/tss.h

View file
+#ifndef __MACHINE_TSS_H
+#define __MACHINE_TSS_H
+
+typedef struct task_state_segment task_state_segment;
+struct task_state_segment {
+    uint32_t tss_backlink;
+    uint32_t tss_esp0;
+    uint32_t tss_ss0;
+    uint32_t tss_esp1;
+    uint32_t tss_ss1;
+    uint32_t tss_esp2;
+    uint32_t tss_ss2;
+    uint32_t tss_cr3;
+    uint32_t tss_eip;
+    uint32_t tss_eflags;
+    uint32_t tss_eax;
+    uint32_t tss_ecx;
+    uint32_t tss_edx;
+    uint32_t tss_ebx;
+    uint32_t tss_esp;
+    uint32_t tss_ebp;
+    uint32_t tss_esi;
+    uint32_t tss_edi;
+    uint32_t tss_es;
+    uint32_t tss_cs;
+    uint32_t tss_ss;
+    uint32_t tss_ds;
+    uint32_t tss_fs;
+    uint32_t tss_gs;
+    uint32_t tss_ldtr;
+    uint32_t tss_io;
+} __packed;
+
+
+#endif
+
+

File sys/arch/x86/machine/types.h

View file
+#ifndef __MACHINE_TYPES_H
+#define __MACHINE_TYPES_H
+
+
+typedef unsigned char  uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int   uint32_t;
+
+
+typedef signed char  int8_t;
+typedef signed short int16_t;
+typedef signed int   int32_t;
+
+typedef uint32_t uintptr_t;
+typedef int32_t  intptr_t;
+
+// adres (wzgledny)
+typedef void* addr_t;
+
+typedef uint32_t size_t;
+typedef int32_t  ssize_t;
+
+
+#endif

File sys/arch/x86/machine/video.h

View file
+// Mateusz Kocielski 
+#ifndef __MACHINE_VIDEO_H
+#define __MACHINE_VIDEO_H
+
+#include <sys/types.h>
+
+#define TEXTSCREEN_VIDEO   (addr_t)0xb8000
+#define TEXTSCREEN_VIDPORT_IDX  0x3d4
+#define TEXTSCREEN_VIDPORT_DATA 0x3d5
+#define TEXTSCREEN_WIDTH   80
+#define TEXTSCREEN_HEIGHT  25
+#define TEXTSCREEN_SIZE (TEXTSCREEN_WIDTH*TEXTSCREEN_HEIGHT)
+
+struct hw_textscreen {
+    uint16_t screen_map[TEXTSCREEN_SIZE];
+    int8_t cursor_y;
+    int8_t cursor_x;
+};
+
+/* tymczasowo na ekran */
+struct hw_textscreen textscreen;
+
+void textscreen_enable_forced_attr(int8_t f);
+void textscreen_disable_forced_attr(void);
+
+
+void textscreen_putat(struct hw_textscreen *screen, int8_t col, int8_t row,
+        char c, int8_t attribute);
+void textscreen_put(struct hw_textscreen *screen, char c, 
+        int8_t attr);
+void textscreen_scroll(struct hw_textscreen *screen);
+void textscreen_update_cursor(struct hw_textscreen *screen, int8_t col, 
+        int8_t row);
+void textscreen_reset(struct hw_textscreen *screen);
+void textscreen_clear(struct hw_textscreen *screen);
+void textscreen_draw(struct hw_textscreen *screen);
+
+enum {
+    COLOR_BLACK,
+    COLOR_BLUE,
+    COLOR_GREEN,
+    COLOR_CYAN,
+    COLOR_RED,
+    COLOR_MAGENTA,
+    COLOR_BROWN,
+    COLOR_BRIGHTGRAY,
+    COLOR_DARKGRAY,
+    COLOR_BRIGHTBLUE,
+    COLOR_BRIGHTGREEN,
+    COLOR_BRIGHTCYAN,
+    COLOR_BRIGHTRED,
+    COLOR_BRIGHTMAGENTA,
+    COLOR_BRIGHTBROWN,
+    COLOR_WHITE
+};
+
+#endif

File sys/conf/kernel.ld

View file
+ENTRY (kernel_entrypoint)
+OUTPUT(elf32-i386)
+SECTIONS{
+    . = 0x00100000;
+
+    .text :{
+	arch/bootstrap.o(.text)
+        *(.text)
+    }
+
+    .rodata ALIGN (0x1000) : {
+        *(.rodata)
+    }
+
+    .data ALIGN (0x1000) : {
+        *(.data)
+    }
+
+    .bss  ALIGN(0x1000) : {
+        *(COMMON)
+        *(.bss)
+    }
+
+}
+

File sys/dev/Makefile

View file
+
+
+build:
+
+clean:
+
+depend:

File sys/kern/Makefile

View file
+LIBS=-larch -lkutil 
+KERNEL=impala
+LIBDEPS=../arch/${IMPALA_ARCH}/libarch.a ../libkutil/libkutil.a
+
+SRCS=\
+	clock.c\
+	kprintf.c\
+	kthread.c\
+	main.c\
+	sched.c\
+	syscall.c\
+	thread.c\
+	sc/exit.c\
+	sc/write.c\
+
+
+
+
+include ${IMPALA_MK}/kernel.mk 

File sys/kern/clock.c

View file
+#include <sys/types.h>
+#include <sys/clock.h>
+#include <sys/sched.h>
+#include <sys/thread.h>
+#include <machine/interrupt.h>
+#include <machine/atomic.h>
+
+enum {
+    TICK_UNLOCKED,
+    TICK_LOCKED,
+    TICK_DISABLED
+};
+
+static uint entered = TICK_DISABLED;
+volatile uint clock_ticks;
+
+void
+clock_init()
+{
+    atomic_change32(&entered, TICK_UNLOCKED);
+}
+
+/* ,,twarde tykni�cie''
+ * Procedura uruchamiana wewn�trz obs�ugi przerwania zegara, nie mo�e
+ * op�ni� kolejnego tykni�cia!.
+ */
+void
+clock_hardtick()
+{
+    clock_ticks++;
+}
+
+/* ,,mi�kkie tykni�cie''
+ * Procedura uruchamiana na zewn�trz obs�ugi przerwania zegara. Czas procesora
+ * zaj�ty przez ni� op�nia jej kolejne wywo�anie, a nie przerwanie.
+ */
+
+void
+clock_softtick()
+{
+//     if (atomic_change32(&entered, TICK_LOCKED) != TICK_UNLOCKED) return;
+//     atomic_change32(&entered, TICK_UNLOCKED);
+    sched_action();
+
+}
+

File sys/kern/impala

Binary file added.

File sys/kern/impala.map

View file
+Archive member included because of file (symbol)
+
+/home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(init.o)
+                              /home/wieczyk/Workspace/impala/sys/arch/x86//arch/bootstrap.o (init_x86)
+/home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(isr.o)
+                              /home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(init.o) (_intrpt_syscall)
+/home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(thread.o)
+                              thread.o (thread_context_init)
+/home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(i8254.o)
+                              /home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(init.o) (i8254_init)
+/home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(i8259a.o)
+                              /home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(init.o) (i8259a_init)
+/home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(video.o)
+                              sc/write.o (textscreen_enable_forced_attr)
+/home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(asmstub.o)
+                              /home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(init.o) (gdt_load)
+/home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(interrupt.o)
+                              /home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(i8254.o) (irq_install_handler)
+/home/wieczyk/Workspace/impala/sys/libkutil/libkutil.a(string.o)
+                              /home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(video.o) (mem_move)
+
+Allocating common symbols
+Common symbol       size              file
+
+threads_list        0x14              thread.o
+textscreen          0xfa2             clock.o
+clock_ticks         0x4               clock.o
+stack               0x4000            /home/wieczyk/Workspace/impala/sys/arch/x86//arch/bootstrap.o
+thread_idle         0x4               thread.o
+irq_handlers        0x5c              /home/wieczyk/Workspace/impala/sys/arch/x86//libarch.a(interrupt.o)
+sp                  0x4               main.o
+sched_quantum       0x4               sched.o
+curthread           0x4               thread.o
+
+Memory Configuration
+
+Name             Origin             Length             Attributes
+*default*        0x0000000000000000 0xffffffffffffffff