├── font ├── font.o └── font.psf ├── TinyGL ├── src │ ├── glu.c │ ├── zmath.c │ ├── msghandling.h │ ├── error.c │ ├── memory.c │ ├── specbuf.h │ ├── clear.c │ ├── Makefile │ ├── zmath.h │ ├── zfeatures.h │ ├── msghandling.c │ ├── specbuf.c │ ├── get.c │ ├── opinfo.h │ ├── zline.c │ ├── oscontext.c │ ├── select.c │ ├── zline.h │ ├── image_util.c │ ├── nglx.c │ ├── misc.c │ ├── zdither.c │ ├── zbuffer.h │ └── init.c ├── README.soso ├── Makefile ├── INSTALL ├── include │ ├── GL │ │ ├── nglx.h │ │ ├── glu.h │ │ └── oscontext.h │ ├── zfeatures.h │ └── zbuffer.h ├── LICENCE ├── Changelog └── config.mk ├── run.sh ├── runkvm.sh ├── screenshots ├── soso1.png ├── soso-doom.png ├── soso-v0.3.png ├── soso-v0.4.png ├── soso-v0.1_1.png └── soso-v0.2_1.png ├── bootdisk-root ├── eltorito.img └── boot │ └── grub │ └── grub.cfg ├── create-cd-image.sh ├── create-eltorito.sh ├── kernel ├── utils.asm ├── null.h ├── mouse.h ├── random.h ├── keyboard.h ├── syscalls.h ├── stdint.h ├── fatfilesystem.h ├── rootfs.h ├── unixsocket.h ├── time.h ├── sleep.h ├── log.h ├── systemfs.h ├── serial.h ├── ramdisk.h ├── pipe.h ├── ipc.h ├── devfs.h ├── framebuffer.h ├── spinlock.h ├── kernelterminal_vga.h ├── gdt.asm ├── sleep.c ├── kernelterminal_fb.h ├── hashtable.h ├── sharedmemory.h ├── null.c ├── message.h ├── alloc.h ├── fatfs_integer.h ├── task.asm ├── timer.h ├── device.h ├── gfx.h ├── kernelterminal_fb.c ├── console.h ├── signal.h ├── syscall_select.h ├── spinlock.c ├── fifobuffer.h ├── multiboot.h ├── kernelterminal.h ├── isr.h ├── syscall_getthreads.h ├── .vscode │ ├── launch.json │ └── tasks.json ├── message.c ├── random.c ├── kernelterminal_vga.c ├── isr.c ├── keymap.h ├── list.h ├── ttydev.h ├── vmm.h ├── keymap.c ├── syscalltable.h ├── fatfs_diskio.h ├── timer.c ├── log.c ├── rootfs.c ├── interrupt.asm ├── descriptortables.h ├── hashtable.c ├── syscall_getthreads.c ├── boot.asm ├── vbe.h ├── fifobuffer.c ├── ramdisk.c ├── framebuffer.c ├── syscall_select.c ├── errno.h └── common.h ├── userspace ├── inputtest.c ├── Makefile ├── sleep.c ├── nx │ ├── Makefile │ ├── term │ │ └── Makefile │ └── progs.c ├── .vscode │ ├── c_cpp_properties.json │ └── launch.json ├── fault.c ├── wr.c ├── test_fork.c ├── fbdemo.c ├── cat.c ├── select.c ├── init.c ├── sterm.c └── ps.c ├── Makefile ├── create-initrd.sh ├── link.ld ├── .gitignore ├── create-image.sh ├── .vscode └── launch.json ├── LICENSE └── README.md /font/font.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/font/font.o -------------------------------------------------------------------------------- /font/font.psf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/font/font.psf -------------------------------------------------------------------------------- /TinyGL/src/glu.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/TinyGL/src/glu.c -------------------------------------------------------------------------------- /TinyGL/src/zmath.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/TinyGL/src/zmath.c -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-i386 -cdrom soso.iso -m 256 -serial stdio -s 4 | -------------------------------------------------------------------------------- /runkvm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | qemu-system-i386 -cdrom soso.iso -m 256 -enable-kvm 4 | -------------------------------------------------------------------------------- /screenshots/soso1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/screenshots/soso1.png -------------------------------------------------------------------------------- /bootdisk-root/eltorito.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/bootdisk-root/eltorito.img -------------------------------------------------------------------------------- /screenshots/soso-doom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/screenshots/soso-doom.png -------------------------------------------------------------------------------- /screenshots/soso-v0.3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/screenshots/soso-v0.3.png -------------------------------------------------------------------------------- /screenshots/soso-v0.4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/screenshots/soso-v0.4.png -------------------------------------------------------------------------------- /screenshots/soso-v0.1_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/screenshots/soso-v0.1_1.png -------------------------------------------------------------------------------- /screenshots/soso-v0.2_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ozkl/soso/HEAD/screenshots/soso-v0.2_1.png -------------------------------------------------------------------------------- /TinyGL/README.soso: -------------------------------------------------------------------------------- 1 | - Makefiles were modified in order to build for soso! 2 | - glu.c is now part of libTinyGL 3 | - Also BEOS and examples directories were deleted. -------------------------------------------------------------------------------- /create-cd-image.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | cp kernel.bin bootdisk-root/boot/ 3 | cp initrd.fat bootdisk-root/boot/ 4 | 5 | mkisofs -R -b eltorito.img -no-emul-boot -boot-info-table -o soso.iso bootdisk-root 6 | 7 | 8 | -------------------------------------------------------------------------------- /TinyGL/Makefile: -------------------------------------------------------------------------------- 1 | include config.mk 2 | 3 | all: 4 | ( for f in $(DIRS); do ( cd $$f ; make all ) || exit 1 ; done ) 5 | 6 | clean: 7 | rm -f *~ lib/libTinyGL.a include/GL/*~ TAGS 8 | ( for f in $(DIRS); do ( cd $$f ; make clean ; ) done ) 9 | -------------------------------------------------------------------------------- /TinyGL/src/msghandling.h: -------------------------------------------------------------------------------- 1 | #ifndef _msghandling_h_ 2 | #define _msghandling_h_ 3 | 4 | extern void tgl_warning(const char *text, ...); 5 | extern void tgl_trace(const char *text, ...); 6 | extern void tgl_fixme(const char *text, ...); 7 | 8 | #endif /* _msghandling_h_ */ 9 | -------------------------------------------------------------------------------- /create-eltorito.sh: -------------------------------------------------------------------------------- 1 | grub-mkimage -O i386-pc -o core.img --prefix=/boot/grub biosdisk iso9660 multiboot normal search search_label gfxterm vbe video 2 | 3 | cp /usr/lib/grub/i386-pc/cdboot.img bootdisk-root/eltorito.img 4 | cat core.img >> bootdisk-root/eltorito.img 5 | -------------------------------------------------------------------------------- /TinyGL/src/error.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "zgl.h" 3 | 4 | void gl_fatal_error(char *format, ...) 5 | { 6 | va_list ap; 7 | 8 | va_start(ap,format); 9 | 10 | fprintf(stderr,"TinyGL: fatal error: "); 11 | vfprintf(stderr,format,ap); 12 | fprintf(stderr,"\n"); 13 | exit(1); 14 | 15 | va_end(ap); 16 | } 17 | -------------------------------------------------------------------------------- /kernel/utils.asm: -------------------------------------------------------------------------------- 1 | [GLOBAL read_eip] 2 | read_eip: 3 | pop eax 4 | jmp eax 5 | 6 | [GLOBAL disable_paging] 7 | disable_paging: 8 | mov edx, cr0 9 | and edx, 0x7fffffff 10 | mov cr0, edx 11 | ret 12 | 13 | [GLOBAL enable_paging] 14 | enable_paging: 15 | mov edx, cr0 16 | or edx, 0x80000000 17 | mov cr0, edx 18 | ret 19 | -------------------------------------------------------------------------------- /kernel/null.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef NULL_H 12 | #define NULL_H 13 | 14 | void null_initialize(); 15 | 16 | #endif // NULL_H 17 | -------------------------------------------------------------------------------- /kernel/mouse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef MOUSE_H 12 | #define MOUSE_H 13 | 14 | void initialize_mouse(); 15 | 16 | #endif // MOUSE_H 17 | -------------------------------------------------------------------------------- /TinyGL/src/memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Memory allocator for TinyGL 3 | */ 4 | #include "zgl.h" 5 | 6 | /* modify these functions so that they suit your needs */ 7 | 8 | void gl_free(void *p) 9 | { 10 | free(p); 11 | } 12 | 13 | void *gl_malloc(int size) 14 | { 15 | return malloc(size); 16 | } 17 | 18 | void *gl_zalloc(int size) 19 | { 20 | return calloc(1, size); 21 | } 22 | -------------------------------------------------------------------------------- /kernel/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef RANDOM_H 12 | #define RANDOM_H 13 | 14 | void random_initialize(); 15 | 16 | #endif // RANDOM_H 17 | -------------------------------------------------------------------------------- /userspace/inputtest.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n = 0; 6 | while (1) 7 | { 8 | printf("enter number (-1 to quit):"); 9 | fflush(stdout); 10 | scanf("%d", &n); 11 | printf("you entered:%d\n", n); 12 | 13 | if (n == -1) 14 | { 15 | break; 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /kernel/keyboard.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef KEYBOARD_H 12 | #define KEYBOARD_H 13 | 14 | void keyboard_initialize(); 15 | 16 | #endif // KEYBOARD_H 17 | -------------------------------------------------------------------------------- /kernel/syscalls.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SYSCALLS_H 12 | #define SYSCALLS_H 13 | 14 | void syscalls_initialize(); 15 | 16 | #endif // SYSCALLS_H 17 | -------------------------------------------------------------------------------- /kernel/stdint.h: -------------------------------------------------------------------------------- 1 | #ifndef STDINT_H 2 | #define STDINT_H 3 | 4 | typedef unsigned long long uint64_t; 5 | typedef signed long long int64_t; 6 | typedef unsigned int uint32_t; 7 | typedef int int32_t; 8 | typedef unsigned short uint16_t; 9 | typedef short int16_t; 10 | typedef unsigned char uint8_t; 11 | typedef char int8_t; 12 | typedef unsigned int size_t; 13 | 14 | #endif -------------------------------------------------------------------------------- /userspace/Makefile: -------------------------------------------------------------------------------- 1 | SOURCES_C=$(patsubst %.c,%.o,$(wildcard ./*.c)) 2 | 3 | 4 | CC=musl-clang 5 | LD=musl-clang 6 | CFLAGS=-c -O3 -I../TinyGL/include 7 | LDFLAGS=-static -L../TinyGL/src -lTinyGL 8 | 9 | OBJ = $(SOURCES_C) 10 | 11 | all: $(OBJ) 12 | 13 | clean: 14 | -rm *.o 15 | -rm -rf bin 16 | 17 | %.o:%.c 18 | mkdir -p bin 19 | -$(CC) $(CFLAGS) $< -o $@ 20 | -$(LD) $@ $(LDFLAGS) -o bin/$* 21 | -------------------------------------------------------------------------------- /kernel/fatfilesystem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef FATFILESYSTEM_H 12 | #define FATFILESYSTEM_H 13 | 14 | void fatfs_initialize(); 15 | 16 | #endif // FATFILESYSTEM_H 17 | -------------------------------------------------------------------------------- /kernel/rootfs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef ROOTFS_H 12 | #define ROOTFS_H 13 | 14 | #include "fs.h" 15 | 16 | FileSystemNode* rootfs_initialize(); 17 | 18 | #endif // ROOTFS_H 19 | -------------------------------------------------------------------------------- /kernel/unixsocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef UNIX_SOCKET_H 12 | #define UNIX_SOCKET_H 13 | 14 | #include "socket.h" 15 | 16 | void unixsocket_setup(Socket* socket); 17 | 18 | #endif //UNIX_SOCKET_H -------------------------------------------------------------------------------- /kernel/time.h: -------------------------------------------------------------------------------- 1 | #ifndef TIME_H 2 | #define TIME_H 3 | 4 | #include "common.h" 5 | 6 | typedef uint64_t time_t; 7 | 8 | typedef int64_t suseconds_t; 9 | 10 | struct timespec 11 | { 12 | time_t tv_sec; /* seconds */ 13 | uint32_t tv_nsec; /* nanoseconds */ 14 | }; 15 | 16 | struct timeval 17 | { 18 | uint32_t tv_sec; /* seconds */ 19 | uint32_t tv_usec; /* microseconds */ 20 | }; 21 | 22 | #endif //TIME_H -------------------------------------------------------------------------------- /userspace/sleep.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char** argv) 8 | { 9 | int seconds = 1; 10 | 11 | if (argc > 1) 12 | { 13 | sscanf(argv[1], "%d", &seconds); 14 | } 15 | 16 | printf("sleeping %d seconds\n", seconds); 17 | sleep(seconds); 18 | printf("Finished.\n"); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /kernel/sleep.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SLEEP_H 12 | #define SLEEP_H 13 | 14 | #include "common.h" 15 | #include "process.h" 16 | 17 | void sleep_ms(Thread* thread, uint32_t ms); 18 | 19 | #endif // SLEEP_H 20 | -------------------------------------------------------------------------------- /bootdisk-root/boot/grub/grub.cfg: -------------------------------------------------------------------------------- 1 | insmod vbe 2 | insmod vga 3 | 4 | menuentry "SOSO CD" { 5 | set root=(cd) 6 | terminal_output console 7 | multiboot /boot/kernel.bin 8 | module /boot/initrd.fat 9 | set gfxpayload=1024x768x32 10 | boot 11 | } 12 | 13 | menuentry "SOSO DISK" { 14 | set root=(hd0,1) 15 | multiboot /boot/kernel.bin 16 | module /boot/initrd.fat 17 | set gfxpayload=1024x768x32 18 | boot 19 | } 20 | 21 | -------------------------------------------------------------------------------- /kernel/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef LOG_H 12 | #define LOG_H 13 | 14 | void log_initialize_serial(); 15 | void log_initialize(const char* file_name); 16 | void log_printf(const char *format, ...); 17 | 18 | #endif // LOG_H 19 | -------------------------------------------------------------------------------- /kernel/systemfs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SYSTEMFS_H 12 | #define SYSTEMFS_H 13 | 14 | #include "device.h" 15 | #include "fs.h" 16 | #include "common.h" 17 | 18 | void systemfs_initialize(); 19 | 20 | #endif // SYSTEMFS_H 21 | -------------------------------------------------------------------------------- /kernel/serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SERIAL_H 12 | #define SERIAL_H 13 | 14 | void serial_initialize(); 15 | void serial_initialize_file_device(); 16 | void serial_printf(const char *format, ...); 17 | 18 | #endif // SERIAL_H 19 | -------------------------------------------------------------------------------- /TinyGL/INSTALL: -------------------------------------------------------------------------------- 1 | Installation: 2 | 3 | - Edit config.mk and change what is needed. You can also look at 4 | src/zfeatures.h to change very specific details (only useful to tune 5 | TinyGL to your needs). You can link the examples with either OpenGL, 6 | Mesa or TinyGL. 7 | 8 | - Type 'make'. The library 'libTinyGL.a' is copied into './lib'. The 9 | examples are build in './examples'. Only the directories './lib' and 10 | './include' are needed to use TinyGL from another program. 11 | -------------------------------------------------------------------------------- /kernel/ramdisk.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef RAMDISK_H 12 | #define RAMDISK_H 13 | 14 | #include "common.h" 15 | 16 | BOOL ramdisk_create(const char* devName, uint32_t size, uint8_t* preallocated_buffer); 17 | 18 | #endif // RAMDISK_H 19 | -------------------------------------------------------------------------------- /userspace/nx/Makefile: -------------------------------------------------------------------------------- 1 | SOURCES_C=$(patsubst %.c,%.o,$(wildcard ./*.c)) 2 | 3 | 4 | CC=musl-clang 5 | LD=musl-clang 6 | CFLAGS=-c -O3 -I../../../microwindows/src/include -I../../TinyGL/include 7 | LDFLAGS=-L../../../microwindows/src/lib -L../../TinyGL/src -lTinyGL -lnano-X 8 | 9 | OBJ = $(SOURCES_C) 10 | 11 | all: $(OBJ) 12 | 13 | clean: 14 | -rm *.o 15 | -rm -f ../bin/$* 16 | 17 | %.o:%.c 18 | mkdir -p ../bin 19 | $(CC) $(CFLAGS) $< -o $@ 20 | $(LD) $@ $(LDFLAGS) -o ../bin/$* 21 | -------------------------------------------------------------------------------- /userspace/.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "${workspaceFolder}/../root/include/**" 8 | ], 9 | "defines": [], 10 | "compilerPath": "/usr/bin/clang", 11 | "cStandard": "c11", 12 | "cppStandard": "c++14", 13 | "intelliSenseMode": "linux-clang-x64" 14 | } 15 | ], 16 | "version": 4 17 | } -------------------------------------------------------------------------------- /kernel/pipe.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef PIPE_H 12 | #define PIPE_H 13 | 14 | #include "common.h" 15 | 16 | void pipe_initialize(); 17 | BOOL pipe_create(const char* name, uint32_t bufferSize); 18 | BOOL pipe_destroy(const char* name); 19 | BOOL pipe_exists(const char* name); 20 | 21 | #endif // PIPE_H 22 | -------------------------------------------------------------------------------- /userspace/fault.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char** argv) 7 | { 8 | char* xx = (char*)0xBAD; 9 | 10 | 11 | if (argc > 1) 12 | { 13 | printf("Let's try to write to %p\n", xx); 14 | *xx = 5; 15 | } 16 | else 17 | { 18 | printf("Let's try to read from %p\n", xx); 19 | printf("Value: %d\n", *xx); 20 | } 21 | 22 | 23 | printf("Finished. Should not reach here!\n"); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /kernel/ipc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef IPC_H 12 | #define IPC_H 13 | 14 | #include "stdint.h" 15 | 16 | #define IPC_CREAT 01000 17 | #define IPC_EXCL 02000 18 | #define IPC_NOWAIT 04000 19 | 20 | #define IPC_RMID 0 21 | #define IPC_SET 1 22 | #define IPC_INFO 3 23 | 24 | #define IPC_PRIVATE ((int32_t) 0) 25 | 26 | #endif //IPC_H -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SOURCES_C=$(patsubst %.c,%.o,$(wildcard kernel/*.c)) 2 | SOURCES_ASM=$(patsubst %.asm,%.o,$(wildcard kernel/*.asm)) 3 | 4 | 5 | CC=clang 6 | LD=ld 7 | CFLAGS=-nostdlib -nostdinc -fno-builtin -m32 -c 8 | LDFLAGS=-Tlink.ld -m elf_i386 9 | ASFLAGS=-felf 10 | 11 | OBJ = $(SOURCES_ASM) $(SOURCES_C) 12 | 13 | all: $(OBJ) link 14 | 15 | clean: 16 | -rm kernel/*.o kernel.bin 17 | 18 | link: 19 | $(LD) $(LDFLAGS) -o kernel.bin $(OBJ) font/font.o 20 | 21 | kernel/%.o:kernel/%.c 22 | $(CC) $(CFLAGS) $< -o $@ 23 | 24 | kernel/%.o:kernel/%.asm 25 | nasm $(ASFLAGS) $< -o $@ 26 | -------------------------------------------------------------------------------- /create-initrd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | 4 | IMG=initrd.fat 5 | MNT=mnt 6 | SIZE_MB=12 7 | 8 | mkdir -p "$MNT" 9 | dd if=/dev/zero of=$IMG bs=1M count=$SIZE_MB 10 | 11 | LOOP=$(sudo losetup --find --show $IMG) 12 | 13 | cleanup() { 14 | if mountpoint -q "$MNT"; then 15 | sudo umount "$MNT" 16 | fi 17 | if losetup -a | grep -q "$LOOP"; then 18 | sudo losetup -d "$LOOP" 19 | fi 20 | } 21 | trap cleanup EXIT 22 | 23 | sudo mkfs.vfat "$LOOP" 24 | sudo mount "$LOOP" "$MNT" 25 | sudo cp -r --no-preserve=ownership userspace/bin/* "$MNT/" 26 | sync 27 | -------------------------------------------------------------------------------- /kernel/devfs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef DEVFS_H 12 | #define DEVFS_H 13 | 14 | #include "device.h" 15 | #include "fs.h" 16 | #include "common.h" 17 | 18 | void devfs_initialize(); 19 | FileSystemNode* devfs_register_device(Device* device, BOOL add_to_fs); 20 | void devfs_unregister_device(FileSystemNode* device_node); 21 | 22 | #endif // DEVFS_H 23 | -------------------------------------------------------------------------------- /kernel/framebuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef FRAMEBUFFER_H 12 | #define FRAMEBUFFER_H 13 | 14 | #include "common.h" 15 | 16 | enum EnFrameBuferIoctl 17 | { 18 | FB_GET_WIDTH, 19 | FB_GET_HEIGHT, 20 | FB_GET_BITSPERPIXEL 21 | }; 22 | 23 | void framebuffer_initialize(uint8_t* p_address, uint8_t* v_address); 24 | 25 | #endif // FRAMEBUFFER_H 26 | -------------------------------------------------------------------------------- /kernel/spinlock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SPINLOCK_H 12 | #define SPINLOCK_H 13 | 14 | #include "common.h" 15 | 16 | typedef int32_t Spinlock; 17 | 18 | void spinlock_init(Spinlock* spinlock); 19 | void spinlock_lock(Spinlock* spinlock); 20 | BOOL spinlock_try_lock(Spinlock* spinlock); 21 | void spinlock_unlock(Spinlock* spinlock); 22 | 23 | #endif // SPINLOCK_H 24 | -------------------------------------------------------------------------------- /TinyGL/include/GL/nglx.h: -------------------------------------------------------------------------------- 1 | #ifndef NGLX_H 2 | #define NGLX_H 3 | 4 | #include 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | typedef void *NGLXContext; 12 | typedef GR_DRAW_ID NGLXDrawable; 13 | 14 | extern NGLXContext nglXCreateContext( NGLXContext shareList, int flags ); 15 | 16 | extern void nglXDestroyContext( NGLXContext ctx ); 17 | 18 | extern int nglXMakeCurrent( NGLXDrawable drawable, 19 | NGLXContext ctx); 20 | 21 | extern void nglXSwapBuffers( NGLXDrawable drawable ); 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /userspace/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Attach QEMU", 9 | "type": "gdb", 10 | "request": "attach", 11 | "executable": "bin/shell", 12 | "target": "localhost:1234", 13 | "remote": true, 14 | "cwd": "${workspaceRoot}", 15 | "gdbpath": "/usr/bin/gdb", 16 | } 17 | ] 18 | } -------------------------------------------------------------------------------- /userspace/wr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char** argv) 5 | { 6 | if (argc > 2) 7 | { 8 | const char* file = argv[1]; 9 | const char* text = argv[2]; 10 | 11 | FILE* f = fopen(file, "w"); 12 | if (f) 13 | { 14 | int bytes = strlen(text); 15 | fwrite(text, 1, bytes, f); 16 | 17 | fclose(f); 18 | } 19 | else 20 | { 21 | printf("Could not open for write: %s\n", file); 22 | } 23 | } 24 | else 25 | { 26 | printf("no input!\n"); 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /link.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf32-i386) 2 | ENTRY(_start) 3 | 4 | phys = 0x100000; 5 | offset = 0xC0000000; 6 | virt = offset + phys; 7 | 8 | SECTIONS 9 | { 10 | . = virt; 11 | 12 | .text : AT(ADDR(.text) - offset) { 13 | g_code = .; 14 | *(.text) 15 | *(.rodata) 16 | . = ALIGN(0x1000); 17 | } 18 | 19 | .data : AT(ADDR(.data) - offset) { 20 | g_data = .; 21 | *(.data) 22 | . = ALIGN(0x1000); 23 | } 24 | 25 | .bss : AT(ADDR(.bss) - offset) { 26 | g_bss = .; 27 | *(COMMON) 28 | *(.bss) 29 | . = ALIGN(0x1000); 30 | } 31 | 32 | _kernel_end = .; 33 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.user 2 | 3 | # Prerequisites 4 | *.d 5 | 6 | # Object files 7 | *.o 8 | *.ko 9 | *.obj 10 | *.elf 11 | 12 | # Linker output 13 | *.ilk 14 | *.map 15 | *.exp 16 | 17 | # Precompiled Headers 18 | *.gch 19 | *.pch 20 | 21 | # Libraries 22 | *.lib 23 | *.a 24 | *.la 25 | *.lo 26 | 27 | # Shared objects (inc. Windows DLLs) 28 | *.dll 29 | *.so 30 | *.so.* 31 | *.dylib 32 | 33 | # Executables 34 | *.exe 35 | *.out 36 | *.app 37 | *.i*86 38 | *.x86_64 39 | *.hex 40 | 41 | # Debug files 42 | *.dSYM/ 43 | *.su 44 | *.idb 45 | *.pdb 46 | 47 | # Kernel Module Compile Results 48 | *.mod* 49 | *.cmd 50 | .tmp_versions/ 51 | modules.order 52 | Module.symvers 53 | Mkfile.old 54 | dkms.conf 55 | -------------------------------------------------------------------------------- /kernel/kernelterminal_vga.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef KERNELTERMINAL_VGA_H 12 | #define KERNELTERMINAL_VGA_H 13 | 14 | #include "common.h" 15 | 16 | uint16_t vgaterminal_get_column_count(); 17 | uint16_t vgaterminal_get_row_count(); 18 | void vgaterminal_set_cursor_visible(BOOL visible); 19 | void vgaterminal_set_character(uint16_t row, uint16_t column, uint8_t character); 20 | void vgaterminal_move_cursor(uint16_t row, uint16_t column); 21 | 22 | #endif //KERNELTERMINAL_VGA_H -------------------------------------------------------------------------------- /userspace/nx/term/Makefile: -------------------------------------------------------------------------------- 1 | SOURCES := $(wildcard ./*.c) 2 | OBJS := $(SOURCES:.c=.o) 3 | 4 | CC := musl-clang 5 | LD := musl-clang 6 | CFLAGS := -c -O3 -I../../../../microwindows/src/include -I../../../TinyGL/include 7 | LDFLAGS := -L../../../../microwindows/src/lib -L../../../TinyGL/src -lTinyGL -lnano-X 8 | 9 | BIN_DIR := ../../bin 10 | EXE ?= term # set with `make EXE=myprog` 11 | TARGET := $(BIN_DIR)/$(EXE) 12 | 13 | .PHONY: all clean 14 | all: $(TARGET) 15 | 16 | $(TARGET): $(OBJS) | $(BIN_DIR) 17 | $(LD) $(OBJS) $(LDFLAGS) -o $@ 18 | 19 | $(BIN_DIR): 20 | mkdir -p $@ 21 | 22 | %.o: %.c 23 | $(CC) $(CFLAGS) -o $@ -c $< 24 | 25 | clean: 26 | -rm -f *.o $(TARGET) 27 | -------------------------------------------------------------------------------- /TinyGL/src/specbuf.h: -------------------------------------------------------------------------------- 1 | #ifndef _tgl_specbuf_h_ 2 | #define _tgl_specbuf_h_ 3 | 4 | /* Max # of specular light pow buffers */ 5 | #define MAX_SPECULAR_BUFFERS 8 6 | /* # of entries in specular buffer */ 7 | #define SPECULAR_BUFFER_SIZE 1024 8 | /* specular buffer granularity */ 9 | #define SPECULAR_BUFFER_RESOLUTION 1024 10 | 11 | typedef struct GLSpecBuf { 12 | int shininess_i; 13 | int last_used; 14 | float buf[SPECULAR_BUFFER_SIZE+1]; 15 | struct GLSpecBuf *next; 16 | } GLSpecBuf; 17 | 18 | GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, 19 | const float shininess); 20 | void specbuf_cleanup(GLContext *c); /* free all memory used */ 21 | 22 | #endif /* _tgl_specbuf_h_ */ -------------------------------------------------------------------------------- /TinyGL/src/clear.c: -------------------------------------------------------------------------------- 1 | #include "zgl.h" 2 | 3 | 4 | void glopClearColor(GLContext *c,GLParam *p) 5 | { 6 | c->clear_color.v[0]=p[1].f; 7 | c->clear_color.v[1]=p[2].f; 8 | c->clear_color.v[2]=p[3].f; 9 | c->clear_color.v[3]=p[4].f; 10 | } 11 | void glopClearDepth(GLContext *c,GLParam *p) 12 | { 13 | c->clear_depth=p[1].f; 14 | } 15 | 16 | 17 | void glopClear(GLContext *c,GLParam *p) 18 | { 19 | int mask=p[1].i; 20 | int z=0; 21 | int r=(int)(c->clear_color.v[0]*65535); 22 | int g=(int)(c->clear_color.v[1]*65535); 23 | int b=(int)(c->clear_color.v[2]*65535); 24 | 25 | /* TODO : correct value of Z */ 26 | 27 | ZB_clear(c->zb,mask & GL_DEPTH_BUFFER_BIT,z, 28 | mask & GL_COLOR_BUFFER_BIT,r,g,b); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /kernel/gdt.asm: -------------------------------------------------------------------------------- 1 | [GLOBAL flush_gdt] 2 | 3 | flush_gdt: 4 | mov eax, [esp+4] ;[esp+4] is the parametered passed 5 | lgdt [eax] 6 | 7 | mov ax, 0x10 ;0x10 is the offset to our data segment 8 | mov ds, ax 9 | mov es, ax 10 | mov fs, ax 11 | mov gs, ax 12 | mov ss, ax 13 | jmp 0x08:.flush ;0x08 is the offset to our code segment 14 | .flush: 15 | ret 16 | 17 | [GLOBAL flush_idt] 18 | 19 | flush_idt: 20 | mov eax, [esp+4] ;[esp+4] is the parametered passed 21 | lidt [eax] 22 | ret 23 | 24 | [GLOBAL flush_tss] 25 | 26 | flush_tss: 27 | mov ax, 0x2B ;index of the TSS structure is 0x28 (5*8) and OR'ing two bits in order to set RPL 3 and we get 0x2B 28 | 29 | ltr ax 30 | ret 31 | -------------------------------------------------------------------------------- /kernel/sleep.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "sleep.h" 12 | #include "timer.h" 13 | #include "process.h" 14 | 15 | void sleep_ms(Thread* thread, uint32_t ms) 16 | { 17 | uint32_t uptime = get_uptime_milliseconds(); 18 | 19 | //target uptime to wakeup 20 | uint32_t target = uptime + ms; 21 | 22 | thread_change_state(thread, TS_SLEEP, (void*)target); 23 | 24 | while (thread->state == TS_SLEEP) 25 | { 26 | enable_interrupts(); 27 | 28 | halt(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /create-image.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | dd if=/dev/zero of=soso.img bs=16M count=1 4 | fdisk soso.img << EOF 5 | n 6 | p 7 | 1 8 | 9 | 10 | a 11 | w 12 | EOF 13 | 14 | LOOPDISK=`losetup -f` 15 | sudo losetup $LOOPDISK soso.img 16 | LOOPPARTITION=`losetup -f` 17 | sudo losetup $LOOPPARTITION soso.img -o 1048576 18 | sudo mke2fs $LOOPPARTITION 19 | sudo mount $LOOPPARTITION /mnt 20 | cp kernel.bin bootdisk-root/boot/ 21 | cp initrd.fat bootdisk-root/boot/ 22 | sudo cp -r bootdisk-root/* /mnt/ 23 | sudo grub-install --root-directory=/mnt --no-floppy --modules="normal part_msdos ext2 multiboot" $LOOPDISK 24 | sudo umount /mnt 25 | sudo losetup -d $LOOPPARTITION 26 | sudo losetup -d $LOOPDISK 27 | sync 28 | 29 | sudo chown $SUDO_USER:$SUDO_USER soso.img 30 | -------------------------------------------------------------------------------- /kernel/kernelterminal_fb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef KERNELTERMINAL_FB_H 12 | #define KERNELTERMINAL_FB_H 13 | 14 | #include "common.h" 15 | 16 | uint16_t fbterminal_get_column_count(); 17 | uint16_t fbterminal_get_row_count(); 18 | void fbterminal_set_character(uint16_t row, uint16_t column, uint8_t character, uint32_t fg_color, uint32_t bg_color); 19 | void fbterminal_put_text(uint16_t row, uint16_t column, uint8_t *text, uint16_t size, uint32_t fg_color, uint32_t bg_color); 20 | 21 | #endif //KERNELTERMINAL_FB_H -------------------------------------------------------------------------------- /userspace/test_fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char** rgv) 5 | { 6 | int pid = getpid(); 7 | 8 | printf("I am parent:%d\n", pid); 9 | 10 | int child = fork(); 11 | 12 | if (child == 0) 13 | { 14 | int child_pid = getpid(); 15 | for (size_t i = 0; i < 5; i++) 16 | { 17 | printf("%d:I am child:%d\n", i, child_pid); 18 | sleep(1); 19 | } 20 | } 21 | else 22 | { 23 | int parent_pid = getpid(); 24 | for (size_t i = 0; i < 5; i++) 25 | { 26 | printf("%d:I am parent:%d and my child:%d\n", i, parent_pid, child); 27 | sleep(1); 28 | } 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /kernel/hashtable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef HASHTABLE_H 12 | #define HASHTABLE_H 13 | 14 | #include "common.h" 15 | 16 | typedef struct HashTable HashTable; 17 | 18 | HashTable* hashtable_create(uint32_t capacity); 19 | void hashtable_destroy(HashTable* hashtable); 20 | BOOL hashtable_search(HashTable* hashtable, uint32_t key, uint32_t* value); 21 | BOOL hashtable_insert(HashTable* hashtable, uint32_t key, uint32_t data); 22 | BOOL hashtable_remove(HashTable* hashtable, uint32_t key); 23 | 24 | #endif // HASHTABLE_H 25 | -------------------------------------------------------------------------------- /kernel/sharedmemory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SHAREDMEMORY_H 12 | #define SHAREDMEMORY_H 13 | 14 | #include "common.h" 15 | #include "fs.h" 16 | 17 | void sharedmemory_initialize(); 18 | FileSystemNode* sharedmemory_create(const char* name); 19 | BOOL sharedmemory_destroy_by_name(const char* name); 20 | FileSystemNode* sharedmemory_get_node(const char* name); 21 | BOOL sharedmemory_unmap_if_exists(Process* process, uint32_t address); 22 | void sharedmemory_unmap_for_process_all(Process* process); 23 | 24 | #endif // SHAREDMEMORY_H 25 | -------------------------------------------------------------------------------- /TinyGL/include/GL/glu.h: -------------------------------------------------------------------------------- 1 | 2 | void gluPerspective( GLdouble fovy, GLdouble aspect, 3 | GLdouble zNear, GLdouble zFar ); 4 | 5 | typedef struct { 6 | int draw_style; 7 | } GLUquadricObj; 8 | 9 | #define GLU_LINE 0 10 | 11 | GLUquadricObj *gluNewQuadric(void); 12 | void gluQuadricDrawStyle(GLUquadricObj *obj, int style); 13 | 14 | void gluSphere(GLUquadricObj *qobj, 15 | float radius,int slices,int stacks); 16 | void gluCylinder( GLUquadricObj *qobj, 17 | GLdouble baseRadius, GLdouble topRadius, GLdouble height, 18 | GLint slices, GLint stacks ); 19 | void gluDisk( GLUquadricObj *qobj, 20 | GLdouble innerRadius, GLdouble outerRadius, 21 | GLint slices, GLint loops ); 22 | 23 | void drawTorus(float rc, int numc, float rt, int numt); 24 | -------------------------------------------------------------------------------- /kernel/null.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "null.h" 12 | #include "devfs.h" 13 | #include "device.h" 14 | #include "common.h" 15 | 16 | static BOOL null_open(File *file, uint32_t flags); 17 | 18 | void null_initialize() 19 | { 20 | Device device; 21 | memset((uint8_t*)&device, 0, sizeof(Device)); 22 | strcpy(device.name, "null"); 23 | device.device_type = FT_CHARACTER_DEVICE; 24 | device.open = null_open; 25 | 26 | devfs_register_device(&device, TRUE); 27 | } 28 | 29 | static BOOL null_open(File *file, uint32_t flags) 30 | { 31 | return TRUE; 32 | } 33 | -------------------------------------------------------------------------------- /kernel/message.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef MESSAGE_H 12 | #define MESSAGE_H 13 | 14 | #include "common.h" 15 | 16 | typedef struct SosoMessage 17 | { 18 | uint32_t message_type; 19 | uint32_t parameter1; 20 | uint32_t parameter2; 21 | uint32_t parameter3; 22 | } SosoMessage; 23 | 24 | typedef struct Thread Thread; 25 | 26 | void message_send(Thread* thread, SosoMessage* message); 27 | 28 | uint32_t message_get_queue_count(Thread* thread); 29 | 30 | //returns remaining message count 31 | int32_t message_get_next(Thread* thread, SosoMessage* message); 32 | 33 | #endif // MESSAGE_H 34 | -------------------------------------------------------------------------------- /kernel/alloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef ALLOC_H 12 | #define ALLOC_H 13 | 14 | #include "common.h" 15 | #include "process.h" 16 | 17 | void initialize_kernel_heap(); 18 | void *ksbrk_page(int n); 19 | void *kmalloc(uint32_t size); 20 | void kfree(void *v_addr); 21 | 22 | void initialize_program_break(Process* process, uint32_t begin, uint32_t size); 23 | void *sbrk(Process* process, int n_bytes); 24 | 25 | uint32_t get_kernel_heap_used(); 26 | 27 | struct MallocHeader 28 | { 29 | unsigned long size:31; 30 | unsigned long used:1; 31 | } __attribute__ ((packed)); 32 | 33 | typedef struct MallocHeader MallocHeader; 34 | 35 | #endif // ALLOC_H 36 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Attach kernel", 9 | "type": "lldb", 10 | "request": "launch", 11 | "targetCreateCommands": ["target create ${workspaceFolder}/kernel.bin"], 12 | "processCreateCommands": ["gdb-remote localhost:1234"] 13 | }, 14 | { 15 | "name": "Attach shell", 16 | "type": "lldb", 17 | "request": "launch", 18 | "targetCreateCommands": ["target create ${workspaceFolder}/userspace/bin/shell"], 19 | "processCreateCommands": ["gdb-remote localhost:1234"] 20 | } 21 | ] 22 | } -------------------------------------------------------------------------------- /TinyGL/include/GL/oscontext.h: -------------------------------------------------------------------------------- 1 | #ifndef _tgl_osbuffer_h_ 2 | #define _tgl_osbuffer_h_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef struct { 9 | void **zbs; 10 | void **framebuffers; 11 | int numbuffers; 12 | int xsize, ysize; 13 | } ostgl_context; 14 | 15 | ostgl_context * 16 | ostgl_create_context(const int xsize, 17 | const int ysize, 18 | const int depth, 19 | void **framebuffers, 20 | const int numbuffers); 21 | void 22 | ostgl_delete_context(ostgl_context *context); 23 | 24 | void 25 | ostgl_make_current(ostgl_context *context, const int index); 26 | 27 | void 28 | ostgl_resize(ostgl_context * context, 29 | const int xsize, 30 | const int ysize, 31 | void **framebuffers); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif /* _tgl_osbuffer_h_ */ 38 | -------------------------------------------------------------------------------- /userspace/fbdemo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char** argv) 9 | { 10 | int fd = open("/dev/fb0", 0); 11 | if (fd >= 0) 12 | { 13 | int* buffer = mmap(NULL, 1024*768*4, 0, 0, fd, 0); 14 | 15 | if (buffer != (int*)-1) 16 | { 17 | printf("mapped to %p\n", buffer); 18 | 19 | sleep(2); 20 | 21 | while (1) 22 | { 23 | for (int i = 0; i < 1024*768; ++i) 24 | { 25 | buffer[i] = rand(); 26 | } 27 | } 28 | } 29 | else 30 | { 31 | printf("mmap failed\n"); 32 | } 33 | } 34 | else 35 | { 36 | printf("could not open /dev/fb0\n"); 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /kernel/fatfs_integer.h: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------*/ 2 | /* Integer type definitions for FatFs module */ 3 | /*-------------------------------------------*/ 4 | 5 | #ifndef FF_INTEGER 6 | #define FF_INTEGER 7 | 8 | #ifdef _WIN32 /* FatFs development platform */ 9 | 10 | #include 11 | typedef unsigned __int64 QWORD; 12 | 13 | #else /* Embedded platform */ 14 | 15 | /* These types MUST be 16-bit or 32-bit */ 16 | typedef int INT; 17 | typedef unsigned int UINT; 18 | 19 | /* This type MUST be 8-bit */ 20 | typedef unsigned char BYTE; 21 | 22 | /* These types MUST be 16-bit */ 23 | typedef short SHORT; 24 | typedef unsigned short WORD; 25 | typedef unsigned short WCHAR; 26 | 27 | /* These types MUST be 32-bit */ 28 | typedef long LONG; 29 | typedef unsigned long DWORD; 30 | 31 | /* This type MUST be 64-bit (Remove this for ANSI C (C89) compatibility) */ 32 | typedef unsigned long long QWORD; 33 | 34 | #endif 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /kernel/task.asm: -------------------------------------------------------------------------------- 1 | global switch_task 2 | 3 | switch_task: 4 | mov esi, [esp] 5 | pop eax ; *current thread 6 | 7 | ; get values from thread->regs structure 8 | 9 | push dword [esi+4] ; eax 10 | push dword [esi+8] ; ecx 11 | push dword [esi+12] ; edx 12 | push dword [esi+16] ; ebx 13 | push dword [esi+24] ; ebp 14 | push dword [esi+28] ; esi 15 | push dword [esi+32] ; edi 16 | push dword [esi+48] ; ds 17 | push dword [esi+50] ; es 18 | push dword [esi+52] ; fs 19 | push dword [esi+54] ; gs 20 | 21 | mov al, 0x20 22 | out 0x20, al 23 | 24 | mov eax, [esi+56] 25 | mov cr3, eax 26 | 27 | pop gs 28 | pop fs 29 | pop es 30 | pop ds 31 | pop edi 32 | pop esi 33 | pop ebp 34 | pop ebx 35 | pop edx 36 | pop ecx 37 | pop eax 38 | 39 | iret 40 | -------------------------------------------------------------------------------- /kernel/timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef TIMER_H 12 | #define TIMER_H 13 | 14 | #include "common.h" 15 | #include "time.h" 16 | 17 | extern uint64_t g_system_tick_count; 18 | 19 | void timer_initialize(); 20 | uint32_t get_system_tick_count(); 21 | uint64_t get_system_tick_count64(); 22 | uint32_t get_uptime_seconds(); 23 | uint64_t get_uptime_seconds64(); 24 | uint32_t get_uptime_milliseconds(); 25 | uint64_t get_uptime_milliseconds64(); 26 | void scheduler_enable(); 27 | void scheduler_disable(); 28 | 29 | int32_t clock_getres64(int32_t clockid, struct timespec *res); 30 | int32_t clock_gettime64(int32_t clockid, struct timespec *tp); 31 | int32_t clock_settime64(int32_t clockid, const struct timespec *tp); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /kernel/device.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef DEVICE_H 12 | #define DEVICE_H 13 | 14 | #include "common.h" 15 | #include "fs.h" 16 | 17 | typedef struct Device 18 | { 19 | char name[16]; 20 | FileType device_type; 21 | ReadWriteBlockFunction read_block; 22 | ReadWriteBlockFunction write_block; 23 | ReadWriteFunction read; 24 | ReadWriteFunction write; 25 | ReadWriteTestFunction read_test_ready; 26 | ReadWriteTestFunction write_test_ready; 27 | OpenFunction open; 28 | CreateFunction create; 29 | CloseFunction close; 30 | IoctlFunction ioctl; 31 | FtruncateFunction ftruncate; 32 | MmapFunction mmap; 33 | MunmapFunction munmap; 34 | void * private_data; 35 | } Device; 36 | 37 | #endif // DEVICE_H 38 | -------------------------------------------------------------------------------- /kernel/gfx.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef GFX_H 12 | #define GFX_H 13 | 14 | #include "common.h" 15 | 16 | void gfx_initialize(uint32_t* pixels, uint32_t width, uint32_t height, uint32_t bytesPerPixel, uint32_t pitch); 17 | 18 | void gfx_put_char_at( 19 | /* note that this is int, not char as it's a unicode character */ 20 | unsigned short int c, 21 | /* cursor position on screen, in characters not in pixels */ 22 | int cx, int cy, 23 | /* foreground and background colors, say 0xFFFFFF and 0x000000 */ 24 | uint32_t fg, uint32_t bg); 25 | 26 | 27 | 28 | uint8_t* gfx_get_video_memory(); 29 | uint16_t gfx_get_width(); 30 | uint16_t gfx_get_height(); 31 | uint16_t gfx_get_bytes_per_pixel(); 32 | void gfx_fill(uint32_t color); 33 | 34 | #endif // GFX_H 35 | -------------------------------------------------------------------------------- /kernel/kernelterminal_fb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "gfx.h" 12 | #include "kernelterminal_fb.h" 13 | 14 | 15 | uint16_t fbterminal_get_column_count() 16 | { 17 | return gfx_get_width() / 9; 18 | } 19 | 20 | uint16_t fbterminal_get_row_count() 21 | { 22 | return gfx_get_height() / 16; 23 | } 24 | 25 | void fbterminal_set_character(uint16_t row, uint16_t column, uint8_t character, uint32_t fg_color, uint32_t bg_color) 26 | { 27 | gfx_put_char_at(character, column, row, fg_color, bg_color); 28 | } 29 | 30 | void fbterminal_put_text(uint16_t row, uint16_t column, uint8_t *text, uint16_t size, uint32_t fg_color, uint32_t bg_color) 31 | { 32 | for (uint16_t i = 0; i < size; ++i) 33 | { 34 | gfx_put_char_at(text[i], column + i, row, fg_color, bg_color); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /kernel/console.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef CONSOLE_H 12 | #define CONSOLE_H 13 | 14 | #include "common.h" 15 | 16 | #define TERMINAL_COUNT 8 17 | 18 | typedef struct Terminal Terminal; 19 | 20 | typedef struct FileSystemNode FileSystemNode; 21 | 22 | extern Terminal* g_active_terminal; 23 | 24 | void console_initialize(BOOL graphic_mode); 25 | 26 | void console_send_key(uint8_t scancode); 27 | 28 | int32_t console_get_active_terminal_index(); 29 | 30 | void console_set_active_terminal_index(uint32_t index); 31 | 32 | void console_set_active_terminal(Terminal* terminal); 33 | 34 | Terminal* console_get_terminal(uint32_t index); 35 | 36 | Terminal* console_get_terminal_by_master(FileSystemNode* master_node); 37 | 38 | Terminal* console_get_terminal_by_slave(FileSystemNode* slave_node); 39 | 40 | #endif // CONSOLE_H 41 | -------------------------------------------------------------------------------- /kernel/signal.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGNAL_H 2 | #define SIGNAL_H 3 | 4 | #define SIGHUP 1 5 | #define SIGINT 2 6 | #define SIGQUIT 3 7 | #define SIGILL 4 8 | #define SIGTRAP 5 9 | #define SIGABRT 6 10 | #define SIGIOT SIGABRT 11 | #define SIGBUS 7 12 | #define SIGFPE 8 13 | #define SIGKILL 9 14 | #define SIGUSR1 10 15 | #define SIGSEGV 11 16 | #define SIGUSR2 12 17 | #define SIGPIPE 13 18 | #define SIGALRM 14 19 | #define SIGTERM 15 20 | #define SIGSTKFLT 16 21 | #define SIGCHLD 17 22 | #define SIGCONT 18 23 | #define SIGSTOP 19 24 | #define SIGTSTP 20 25 | #define SIGTTIN 21 26 | #define SIGTTOU 22 27 | #define SIGURG 23 28 | #define SIGXCPU 24 29 | #define SIGXFSZ 25 30 | #define SIGVTALRM 26 31 | #define SIGPROF 27 32 | #define SIGWINCH 28 33 | #define SIGIO 29 34 | #define SIGPOLL 29 35 | #define SIGPWR 30 36 | #define SIGSYS 31 37 | #define SIGUNUSED SIGSYS 38 | 39 | #define SIGNAL_COUNT 32 40 | 41 | // sigprocmask operations 42 | #define SIG_BLOCK 0 43 | #define SIG_UNBLOCK 1 44 | #define SIG_SETMASK 2 45 | 46 | #endif //SIGNAL_H -------------------------------------------------------------------------------- /TinyGL/LICENCE: -------------------------------------------------------------------------------- 1 | Copyright notice: 2 | 3 | (C) 1997-1998 Fabrice Bellard 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgment in the product and its documentation 16 | *is* required. 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source distribution. 20 | 21 | If you redistribute modified sources, I would appreciate that you 22 | include in the files history information documenting your changes. 23 | -------------------------------------------------------------------------------- /userspace/cat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char** argv) 7 | { 8 | if (argc > 1) 9 | { 10 | const char* file = argv[1]; 11 | 12 | int n = 1024; 13 | 14 | if (argc > 2) 15 | { 16 | n = atoi(argv[2]); 17 | } 18 | 19 | 20 | int f = open(file, O_RDONLY); 21 | if (f >= 0) 22 | { 23 | char buffer[1024]; 24 | 25 | int bytes = 0; 26 | do 27 | { 28 | bytes = read(f, buffer, n); 29 | 30 | if (bytes < 0) 31 | { 32 | write(1, "read negative!\n", bytes); 33 | break; 34 | } 35 | write(1, buffer, bytes); 36 | 37 | } while (bytes > 0); 38 | 39 | close(f); 40 | } 41 | else 42 | { 43 | printf("Could not open for read: %s\n", file); 44 | } 45 | } 46 | else 47 | { 48 | printf("no input!\n"); 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /userspace/select.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | 10 | #define BUFFER_SIZE 128 11 | 12 | int main() 13 | { 14 | int fdSerial = open("/dev/com1", O_RDWR); 15 | 16 | if (fdSerial < 0) 17 | { 18 | return 1; 19 | } 20 | 21 | fd_set rfds; 22 | struct timeval tv; 23 | 24 | char buffer[BUFFER_SIZE]; 25 | 26 | while (1) 27 | { 28 | FD_ZERO(&rfds); 29 | FD_SET(fdSerial, &rfds); 30 | /* Wait up to five seconds. */ 31 | tv.tv_sec = 5; 32 | tv.tv_usec = 0; 33 | 34 | int sel = select(fdSerial + 1, &rfds, NULL, NULL, &tv); 35 | printf("select returned %d and flag:%d\n", sel, FD_ISSET(fdSerial, &rfds)); 36 | 37 | if (sel > 0 && FD_ISSET(fdSerial, &rfds)) 38 | { 39 | int bytes = read(fdSerial, buffer, BUFFER_SIZE); 40 | 41 | printf("%d bytes read\n", bytes); 42 | } 43 | else 44 | { 45 | printf("no reading\n"); 46 | } 47 | } 48 | 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /TinyGL/src/Makefile: -------------------------------------------------------------------------------- 1 | include ../config.mk 2 | 3 | OBJS= api.o list.o vertex.o init.o matrix.o texture.o \ 4 | misc.o clear.o light.o clip.o select.o get.o error.o \ 5 | zbuffer.o zline.o zdither.o ztriangle.o \ 6 | zmath.o image_util.o oscontext.o msghandling.o \ 7 | arrays.o specbuf.o memory.o glu.o 8 | ifdef TINYGL_USE_GLX 9 | OBJS += glx.o 10 | endif 11 | ifdef TINYGL_USE_NANOX 12 | OBJS += nglx.o 13 | endif 14 | 15 | INCLUDES = -I../include 16 | LIB = libTinyGL.a 17 | 18 | all: $(LIB) 19 | 20 | $(LIB): $(OBJS) 21 | rm -f $(LIB) 22 | ar --target=elf32-i386 rcs $(LIB) $(OBJS) 23 | 24 | clean: 25 | rm -f *~ *.o *.a 26 | 27 | .c.o: 28 | $(CC) $(CFLAGS) $(INCLUDES) -c $*.c 29 | 30 | clip.o: zgl.h zfeatures.h 31 | vertex.o: zgl.h zfeatures.h 32 | light.o: zgl.h zfeatures.h 33 | matrix.o: zgl.h zfeatures.h 34 | list.o: zgl.h opinfo.h zfeatures.h 35 | arrays.c: zgl.h zfeatures.h 36 | specbuf.o: zgl.h zfeatures.h 37 | glx.o: zgl.h zfeatures.h 38 | nglx.o: zgl.h zfeatures.h 39 | zline.o: zgl.h zfeatures.h zline.h 40 | 41 | ztriangle.o: ztriangle.c ztriangle.h zgl.h zfeatures.h 42 | $(CC) $(CFLAGS) -Wno-uninitialized $(INCLUDES) -c $*.c 43 | -------------------------------------------------------------------------------- /TinyGL/src/zmath.h: -------------------------------------------------------------------------------- 1 | #ifndef __ZMATH__ 2 | #define __ZMATH__ 3 | 4 | /* Matrix & Vertex */ 5 | 6 | typedef struct { 7 | float m[4][4]; 8 | } M4; 9 | 10 | typedef struct { 11 | float m[3][3]; 12 | } M3; 13 | 14 | typedef struct { 15 | float m[3][4]; 16 | } M34; 17 | 18 | 19 | #define X v[0] 20 | #define Y v[1] 21 | #define Z v[2] 22 | #define W v[3] 23 | 24 | typedef struct { 25 | float v[3]; 26 | } V3; 27 | 28 | typedef struct { 29 | float v[4]; 30 | } V4; 31 | 32 | void gl_M4_Id(M4 *a); 33 | int gl_M4_IsId(M4 *a); 34 | void gl_M4_Move(M4 *a,M4 *b); 35 | void gl_MoveV3(V3 *a,V3 *b); 36 | void gl_MulM4V3(V3 *a,M4 *b,V3 *c); 37 | void gl_MulM3V3(V3 *a,M4 *b,V3 *c); 38 | 39 | void gl_M4_MulV4(V4 * a,M4 *b,V4 * c); 40 | void gl_M4_InvOrtho(M4 *a,M4 b); 41 | void gl_M4_Inv(M4 *a,M4 *b); 42 | void gl_M4_Mul(M4 *c,M4 *a,M4 *b); 43 | void gl_M4_MulLeft(M4 *c,M4 *a); 44 | void gl_M4_Transpose(M4 *a,M4 *b); 45 | void gl_M4_Rotate(M4 *c,float t,int u); 46 | int gl_V3_Norm(V3 *a); 47 | 48 | V3 gl_V3_New(float x,float y,float z); 49 | V4 gl_V4_New(float x,float y,float z,float w); 50 | 51 | int gl_Matrix_Inv(float *r,float *m,int n); 52 | 53 | #endif __ZMATH__ 54 | -------------------------------------------------------------------------------- /kernel/syscall_select.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SYSCALL_SELECT_H 12 | #define SYSCALL_SELECT_H 13 | 14 | #include "common.h" 15 | #include "time.h" 16 | 17 | #define FD_SETSIZE 1024 18 | 19 | typedef unsigned long fd_mask; 20 | 21 | typedef struct { 22 | unsigned long fds_bits[FD_SETSIZE / 8 / sizeof(long)]; 23 | } fd_set; 24 | 25 | #define FD_ZERO(s) do { int __i; unsigned long *__b=(s)->fds_bits; for(__i=sizeof (fd_set)/sizeof (long); __i; __i--) *__b++=0; } while(0) 26 | #define FD_SET(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] |= (1UL<<((d)%(8*sizeof(long))))) 27 | #define FD_CLR(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] &= ~(1UL<<((d)%(8*sizeof(long))))) 28 | #define FD_ISSET(d, s) !!((s)->fds_bits[(d)/(8*sizeof(long))] & (1UL<<((d)%(8*sizeof(long))))) 29 | 30 | void select_update(Thread* thread); 31 | 32 | int syscall_select(int n, fd_set* rfds, fd_set* wfds, fd_set* efds, struct timeval* tv); 33 | 34 | #endif //SYSCALL_SELECT_H -------------------------------------------------------------------------------- /kernel/spinlock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "spinlock.h" 12 | 13 | static inline int32_t exchange_atomic(volatile int32_t* old_value_address, int32_t new_value) 14 | { 15 | //no need to use lock instruction on xchg 16 | 17 | asm volatile ("xchgl %0, %1" 18 | : "=r"(new_value) 19 | : "m"(*old_value_address), "0"(new_value) 20 | : "memory"); 21 | return new_value; 22 | } 23 | 24 | void spinlock_init(Spinlock* spinlock) 25 | { 26 | *spinlock = 0; 27 | } 28 | 29 | void spinlock_lock(Spinlock* spinlock) 30 | { 31 | while (exchange_atomic((int32_t*)spinlock, 1)) 32 | { 33 | halt(); 34 | } 35 | } 36 | 37 | BOOL spinlock_try_lock(Spinlock* spinlock) 38 | { 39 | if (exchange_atomic((int32_t*)spinlock, 1)) 40 | { 41 | return FALSE; 42 | } 43 | 44 | return TRUE; 45 | } 46 | 47 | void spinlock_unlock(Spinlock* spinlock) 48 | { 49 | *spinlock = 0; 50 | } 51 | -------------------------------------------------------------------------------- /kernel/fifobuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef FIFOBUFFER_H 12 | #define FIFOBUFFER_H 13 | 14 | #include "common.h" 15 | 16 | typedef struct FifoBuffer 17 | { 18 | uint8_t* data; 19 | uint32_t write_index; 20 | uint32_t read_index; 21 | uint32_t capacity; 22 | uint32_t used_bytes; 23 | } FifoBuffer; 24 | 25 | FifoBuffer* fifobuffer_create(uint32_t capacity); 26 | void fifobuffer_destroy(FifoBuffer* fifo_buffer); 27 | void fifobuffer_clear(FifoBuffer* fifo_buffer); 28 | BOOL fifobuffer_is_empty(FifoBuffer* fifo_buffer); 29 | uint32_t fifobuffer_get_size(FifoBuffer* fifo_buffer); 30 | uint32_t fifobuffer_get_capacity(FifoBuffer* fifo_buffer); 31 | uint32_t fifobuffer_get_free(FifoBuffer* fifo_buffer); 32 | int32_t fifobuffer_enqueue(FifoBuffer* fifo_buffer, uint8_t* data, uint32_t size); 33 | int32_t fifobuffer_dequeue(FifoBuffer* fifo_buffer, uint8_t* data, uint32_t size); 34 | int32_t fifobuffer_enqueue_from_other(FifoBuffer* fifo_buffer, FifoBuffer* other); 35 | 36 | #endif // FIFOBUFFER_H 37 | -------------------------------------------------------------------------------- /kernel/multiboot.h: -------------------------------------------------------------------------------- 1 | #ifndef MULTIBOOT_H 2 | #define MULTIBOOT_H 3 | 4 | #include "common.h" 5 | 6 | #define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0 7 | #define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1 8 | #define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2 9 | 10 | struct Multiboot 11 | { 12 | uint32_t flags; 13 | uint32_t mem_lower; 14 | uint32_t mem_upper; 15 | uint32_t boot_device; 16 | uint32_t cmdline; 17 | uint32_t mods_count; 18 | uint32_t mods_addr; 19 | uint32_t num; 20 | uint32_t size; 21 | uint32_t addr; 22 | uint32_t shndx; 23 | uint32_t mmap_length; 24 | uint32_t mmap_addr; 25 | uint32_t drives_length; 26 | uint32_t drives_addr; 27 | uint32_t config_table; 28 | uint32_t boot_loader_name; 29 | uint32_t apm_table; 30 | uint32_t vbe_control_info; 31 | uint32_t vbe_mode_info; 32 | uint16_t vbe_mode; 33 | uint16_t vbe_interface_seg; 34 | uint16_t vbe_interface_off; 35 | uint16_t vbe_interface_len; 36 | 37 | uint64_t framebuffer_addr; 38 | uint32_t framebuffer_pitch; 39 | uint32_t framebuffer_width; 40 | uint32_t framebuffer_height; 41 | uint8_t framebuffer_bpp; 42 | uint8_t framebuffer_type; 43 | } __attribute__((packed)); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /kernel/kernelterminal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef KERNELTERMINAL_H 12 | #define KERNELTERMINAL_H 13 | 14 | #include "common.h" 15 | #include "fs.h" 16 | #include "fifobuffer.h" 17 | #include "termios.h" 18 | #include "ttydev.h" 19 | #include "ozterm.h" 20 | 21 | typedef struct Terminal Terminal; 22 | 23 | 24 | typedef struct Terminal 25 | { 26 | TtyDev* tty; 27 | File* opened_master; 28 | BOOL disabled; 29 | Ozterm* term; 30 | } Terminal; 31 | 32 | 33 | 34 | Terminal* terminal_create(BOOL graphic_mode); 35 | void terminal_destroy(Terminal* terminal); 36 | 37 | 38 | void terminal_clear(Terminal* terminal); 39 | void terminal_put_character(Terminal* terminal, uint8_t c); 40 | void terminal_put_text(Terminal* terminal, const uint8_t* text, uint32_t size); 41 | void terminal_move_cursor(Terminal* terminal, uint16_t line, uint16_t column); 42 | void terminal_scroll_up(Terminal* terminal); 43 | 44 | void terminal_send_key(Terminal* terminal, uint8_t modifier, uint8_t character); 45 | 46 | #endif // KERNELTERMINAL_H 47 | -------------------------------------------------------------------------------- /kernel/isr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef ISR_H 12 | #define ISR_H 13 | 14 | #include "common.h" 15 | 16 | #define IRQ0 32 17 | #define IRQ1 33 18 | #define IRQ2 34 19 | #define IRQ3 35 20 | #define IRQ4 36 21 | #define IRQ5 37 22 | #define IRQ6 38 23 | #define IRQ7 39 24 | #define IRQ8 40 25 | #define IRQ9 41 26 | #define IRQ10 42 27 | #define IRQ11 43 28 | #define IRQ12 44 29 | #define IRQ13 45 30 | #define IRQ14 46 31 | #define IRQ15 47 32 | 33 | typedef struct Registers 34 | { 35 | uint32_t gs; 36 | uint32_t fs; 37 | uint32_t es; 38 | uint32_t ds; 39 | uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; //pushed by pusha 40 | uint32_t interruptNumber, errorCode; //if applicable 41 | uint32_t eip, cs, eflags, userEsp, ss; //pushed by the CPU 42 | } Registers; 43 | 44 | typedef void (*IsrFunction)(Registers*); 45 | 46 | extern IsrFunction g_interrupt_handlers[]; 47 | 48 | extern uint32_t g_isr_count; 49 | extern uint32_t g_irq_count; 50 | 51 | void interrupt_register(uint8_t n, IsrFunction handler); 52 | 53 | 54 | #endif //ISR_H 55 | -------------------------------------------------------------------------------- /kernel/syscall_getthreads.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SYSCALL_GETTHREADS_H 12 | #define SYSCALL_GETTHREADS_H 13 | 14 | #include "stdint.h" 15 | #include "process.h" 16 | 17 | typedef struct ThreadInfo 18 | { 19 | uint32_t thread_id; 20 | uint32_t process_id; 21 | uint32_t state; 22 | uint32_t user_mode; 23 | 24 | uint32_t birth_time; 25 | uint32_t context_switch_count; 26 | uint32_t context_start_time; 27 | uint32_t context_end_time; 28 | uint32_t consumed_cpu_time_ms; 29 | uint32_t usage_cpu; 30 | uint32_t called_syscall_count; 31 | } ThreadInfo; 32 | 33 | typedef struct ProcInfo 34 | { 35 | uint32_t process_id; 36 | uint32_t process_gid; 37 | int32_t parent_process_id; 38 | uint32_t fd[SOSO_MAX_OPENED_FILES]; 39 | 40 | char name[SOSO_PROCESS_NAME_MAX]; 41 | char tty[128]; 42 | char working_directory[128]; 43 | } ProcInfo; 44 | 45 | int32_t syscall_getthreads(ThreadInfo* threads, uint32_t max_count, uint32_t flags); 46 | int32_t syscall_getprocs(ProcInfo* procs, uint32_t max_count, uint32_t flags); 47 | 48 | #endif //SYSCALL_GETTHREADS_H -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2017, ozkl 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /TinyGL/src/zfeatures.h: -------------------------------------------------------------------------------- 1 | #ifndef _tgl_features_h_ 2 | #define _tgl_features_h_ 3 | 4 | /* It is possible to enable/disable (compile time) features in this 5 | header file. */ 6 | 7 | #define TGL_FEATURE_ARRAYS 1 8 | #define TGL_FEATURE_DISPLAYLISTS 1 9 | #define TGL_FEATURE_POLYGON_OFFSET 1 10 | 11 | /* 12 | * Matrix of internal and external pixel formats supported. 'Y' means 13 | * supported. 14 | * 15 | * External 8 16 24 32 16 | * Internal 17 | * 15 . . . . 18 | * 16 Y Y Y Y 19 | * 24 . Y Y . 20 | * 32 . Y . Y 21 | * 22 | * 23 | * 15 bpp does not work yet (although it is easy to add it - ask me if 24 | * you need it). 25 | * 26 | * Internal pixel format: see TGL_FEATURE_RENDER_BITS 27 | * External pixel format: see TGL_FEATURE_xxx_BITS 28 | */ 29 | 30 | /* enable various convertion code from internal pixel format (usually 31 | 16 bits per pixel) to any external format */ 32 | #define TGL_FEATURE_16_BITS 1 33 | //#define TGL_FEATURE_8_BITS 1 34 | #define TGL_FEATURE_24_BITS 1 35 | #define TGL_FEATURE_32_BITS 1 36 | 37 | 38 | //#define TGL_FEATURE_RENDER_BITS 15 39 | //#define TGL_FEATURE_RENDER_BITS 16 40 | //#define TGL_FEATURE_RENDER_BITS 24 41 | #define TGL_FEATURE_RENDER_BITS 32 42 | 43 | #endif /* _tgl_features_h_ */ 44 | -------------------------------------------------------------------------------- /TinyGL/include/zfeatures.h: -------------------------------------------------------------------------------- 1 | #ifndef _tgl_features_h_ 2 | #define _tgl_features_h_ 3 | 4 | /* It is possible to enable/disable (compile time) features in this 5 | header file. */ 6 | 7 | #define TGL_FEATURE_ARRAYS 1 8 | #define TGL_FEATURE_DISPLAYLISTS 1 9 | #define TGL_FEATURE_POLYGON_OFFSET 1 10 | 11 | /* 12 | * Matrix of internal and external pixel formats supported. 'Y' means 13 | * supported. 14 | * 15 | * External 8 16 24 32 16 | * Internal 17 | * 15 . . . . 18 | * 16 Y Y Y Y 19 | * 24 . Y Y . 20 | * 32 . Y . Y 21 | * 22 | * 23 | * 15 bpp does not work yet (although it is easy to add it - ask me if 24 | * you need it). 25 | * 26 | * Internal pixel format: see TGL_FEATURE_RENDER_BITS 27 | * External pixel format: see TGL_FEATURE_xxx_BITS 28 | */ 29 | 30 | /* enable various convertion code from internal pixel format (usually 31 | 16 bits per pixel) to any external format */ 32 | #define TGL_FEATURE_16_BITS 1 33 | //#define TGL_FEATURE_8_BITS 1 34 | #define TGL_FEATURE_24_BITS 1 35 | #define TGL_FEATURE_32_BITS 1 36 | 37 | 38 | //#define TGL_FEATURE_RENDER_BITS 15 39 | //#define TGL_FEATURE_RENDER_BITS 16 40 | //#define TGL_FEATURE_RENDER_BITS 24 41 | #define TGL_FEATURE_RENDER_BITS 32 42 | 43 | #endif /* _tgl_features_h_ */ 44 | -------------------------------------------------------------------------------- /TinyGL/src/msghandling.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define NDEBUG 5 | 6 | #ifdef NDEBUG 7 | #define NO_DEBUG_OUTPUT 8 | #endif 9 | 10 | /* Use this function to output messages when something unexpected 11 | happens (which might be an indication of an error). *Don't* use it 12 | when there's internal errors in the code - these should be handled 13 | by asserts. */ 14 | void 15 | tgl_warning(const char *format, ...) 16 | { 17 | #ifndef NO_DEBUG_OUTPUT 18 | va_list args; 19 | va_start(args, format); 20 | fprintf(stderr, "*WARNING* "); 21 | vfprintf(stderr, format, args); 22 | va_end(args); 23 | #endif /* !NO_DEBUG_OUTPUT */ 24 | } 25 | 26 | /* This function should be used for debug output only. */ 27 | void 28 | tgl_trace(const char *format, ...) 29 | { 30 | #ifndef NO_DEBUG_OUTPUT 31 | va_list args; 32 | va_start(args, format); 33 | fprintf(stderr, "*DEBUG* "); 34 | vfprintf(stderr, format, args); 35 | va_end(args); 36 | #endif /* !NO_DEBUG_OUTPUT */ 37 | } 38 | 39 | /* Use this function to output info about things in the code which 40 | should be fixed (missing handling of special cases, important 41 | features not implemented, known bugs/buglets, ...). */ 42 | void 43 | tgl_fixme(const char *format, ...) 44 | { 45 | #ifndef NO_DEBUG_OUTPUT 46 | va_list args; 47 | va_start(args, format); 48 | fprintf(stderr, "*FIXME* "); 49 | vfprintf(stderr, format, args); 50 | va_end(args); 51 | #endif /* !NO_DEBUG_OUTPUT */ 52 | } 53 | -------------------------------------------------------------------------------- /kernel/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Attach QEMU", 9 | "type": "gdb", 10 | "request": "attach", 11 | "executable": "../kernel.bin", 12 | "target": "localhost:1234", 13 | "remote": true, 14 | "cwd": "${workspaceRoot}", 15 | "gdbpath": "/usr/bin/gdb", 16 | }, 17 | { 18 | "name": "Run QEMU", 19 | "type": "cppdbg", 20 | "request": "launch", 21 | "cwd": "${workspaceRoot}/..", 22 | "program": "/usr/bin/qemu-system-i386", 23 | "args" : [ 24 | "-cdrom", 25 | "soso.iso", 26 | "-m", 27 | "256" 28 | ], 29 | }, 30 | { 31 | "name": "Run QEMU Serial", 32 | "type": "cppdbg", 33 | "request": "launch", 34 | "cwd": "${workspaceRoot}/..", 35 | "program": "/usr/bin/qemu-system-i386", 36 | "args" : [ 37 | "-cdrom", 38 | "soso.iso", 39 | "-m", 40 | "256", 41 | "-serial", 42 | "file:serial.txt" 43 | ], 44 | } 45 | ] 46 | } -------------------------------------------------------------------------------- /kernel/message.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "message.h" 12 | #include "process.h" 13 | #include "fifobuffer.h" 14 | 15 | 16 | void message_send(Thread* thread, SosoMessage* message) 17 | { 18 | spinlock_lock(&(thread->message_queue_lock)); 19 | 20 | fifobuffer_enqueue(thread->message_queue, (uint8_t*)message, sizeof(SosoMessage)); 21 | 22 | spinlock_unlock(&(thread->message_queue_lock)); 23 | } 24 | 25 | uint32_t message_get_queue_count(Thread* thread) 26 | { 27 | int result = 0; 28 | 29 | spinlock_lock(&(thread->message_queue_lock)); 30 | 31 | result = fifobuffer_get_size(thread->message_queue) / sizeof(SosoMessage); 32 | 33 | spinlock_unlock(&(thread->message_queue_lock)); 34 | 35 | return result; 36 | } 37 | 38 | //returns remaining message count 39 | int32_t message_get_next(Thread* thread, SosoMessage* message) 40 | { 41 | uint32_t result = -1; 42 | 43 | spinlock_lock(&(thread->message_queue_lock)); 44 | 45 | result = fifobuffer_get_size(thread->message_queue) / sizeof(SosoMessage); 46 | 47 | if (result > 0) 48 | { 49 | fifobuffer_dequeue(thread->message_queue, (uint8_t*)message, sizeof(SosoMessage)); 50 | 51 | --result; 52 | } 53 | else 54 | { 55 | result = -1; 56 | } 57 | 58 | spinlock_unlock(&(thread->message_queue_lock)); 59 | 60 | return result; 61 | } 62 | -------------------------------------------------------------------------------- /TinyGL/src/specbuf.c: -------------------------------------------------------------------------------- 1 | #include "zgl.h" 2 | #include "msghandling.h" 3 | #include 4 | #include 5 | 6 | static void calc_buf(GLSpecBuf *buf, const float shininess) 7 | { 8 | int i; 9 | float val, inc; 10 | val = 0.0f; 11 | inc = 1.0f/SPECULAR_BUFFER_SIZE; 12 | for (i = 0; i <= SPECULAR_BUFFER_SIZE; i++) { 13 | buf->buf[i] = pow(val, shininess); 14 | val += inc; 15 | } 16 | } 17 | 18 | GLSpecBuf * 19 | specbuf_get_buffer(GLContext *c, const int shininess_i, 20 | const float shininess) 21 | { 22 | GLSpecBuf *found, *oldest; 23 | found = oldest = c->specbuf_first; 24 | while (found && found->shininess_i != shininess_i) { 25 | if (found->last_used < oldest->last_used) { 26 | oldest = found; 27 | } 28 | found = found->next; 29 | } 30 | if (found) { /* hey, found one! */ 31 | found->last_used = c->specbuf_used_counter++; 32 | return found; 33 | } 34 | if (oldest == NULL || c->specbuf_num_buffers < MAX_SPECULAR_BUFFERS) { 35 | /* create new buffer */ 36 | GLSpecBuf *buf = gl_malloc(sizeof(GLSpecBuf)); 37 | if (!buf) gl_fatal_error("could not allocate specular buffer"); 38 | c->specbuf_num_buffers++; 39 | buf->next = c->specbuf_first; 40 | c->specbuf_first = buf; 41 | buf->last_used = c->specbuf_used_counter++; 42 | buf->shininess_i = shininess_i; 43 | calc_buf(buf, shininess); 44 | return buf; 45 | } 46 | /* overwrite the lru buffer */ 47 | /*tgl_trace("overwriting spec buffer :(\n");*/ 48 | oldest->shininess_i = shininess_i; 49 | oldest->last_used = c->specbuf_used_counter++; 50 | calc_buf(oldest, shininess); 51 | return oldest; 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # soso 2 | Soso is a simple unix-like operating system written in Nasm assembly and mostly in C. It supports multiboot, so it is started by Grub. 3 | It can be built using Nasm and Clang. 4 | Tested build environments are Linux, FreeBSD. 5 | 6 | As of version 0.4, executables are 32 bit static Linux binaries. 7 | 8 | Soso is a 32-bit x86 operating system and its features are 9 | - Runs simple statically built Linux binaries (from version 0.4) 10 | - Multitasking with processes and threads 11 | - Paging 12 | - Higher half kernel (from version 0.4) 13 | - Kernelspace (runs in ring0) and userspace (runs in ring3) are separated 14 | - Virtual File System 15 | - FAT32 filesystem using FatFs 16 | - System calls 17 | - Userspace programs as ELF files (32 bit static Linux ELF executables) 18 | - mmap support 19 | - Framebuffer graphics (userspace can access with mmap) 20 | - Shared memory 21 | - Serial port 22 | - PS/2 mouse 23 | - Unix sockets 24 | - TTY driver 25 | 26 | 27 | ![Soso](screenshots/soso-v0.4.png) 28 | 29 | # running 30 | 31 | You can download a [CD image (ISO file)](https://github.com/ozkl/soso/releases/download/v0.4/soso.zip) from releases and try it in a PC emulator like QEMU. When it is started, you can run: "doom", "lua" in a terminal window. 32 | 33 | To try Soso in QEMU, just run: 34 | 35 | qemu-system-i386 -cdrom soso.iso 36 | 37 | To run doom from a terminal window: 38 | 39 | cd /initrd 40 | doom 41 | 42 | # building 43 | To build kernel just run: 44 | 45 | make 46 | 47 | this will build only kernel (kernel.bin). 48 | 49 | ## Building userspace 50 | You don't need a special compiler! Just build 32 bit static executables for Linux 51 | 52 | -------------------------------------------------------------------------------- /kernel/random.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "random.h" 12 | #include "devfs.h" 13 | #include "device.h" 14 | #include "common.h" 15 | #include "process.h" 16 | #include "sleep.h" 17 | 18 | static BOOL random_open(File *file, uint32_t flags); 19 | static int32_t random_read(File *file, uint32_t size, uint8_t *buffer); 20 | 21 | void random_initialize() 22 | { 23 | Device device; 24 | memset((uint8_t*)&device, 0, sizeof(Device)); 25 | strcpy(device.name, "random"); 26 | device.device_type = FT_CHARACTER_DEVICE; 27 | device.open = random_open; 28 | device.read = random_read; 29 | 30 | devfs_register_device(&device, TRUE); 31 | } 32 | 33 | static BOOL random_open(File *file, uint32_t flags) 34 | { 35 | return TRUE; 36 | } 37 | 38 | static int32_t random_read(File *file, uint32_t size, uint8_t *buffer) 39 | { 40 | if (size == 0) 41 | { 42 | return 0; 43 | } 44 | 45 | uint32_t number = rand(); 46 | 47 | if (size == 1) 48 | { 49 | *buffer = (uint8_t)number; 50 | return 1; 51 | } 52 | else if (size == 2 || size == 3) 53 | { 54 | *((uint16_t*)buffer) = (uint16_t)number; 55 | return 2; 56 | } 57 | else if (size >= 4) 58 | { 59 | //Screen_PrintF("random_read: buffer is %x, writing %x to buffer\n", buffer, number); 60 | 61 | *((uint32_t*)buffer) = number; 62 | return 4; 63 | } 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /userspace/init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | #define VT_ACTIVATE 0x5606 12 | #define VT_SOSO_DISABLE 0x9902f 13 | 14 | int execute_on_tty(const char *path, char *const argv[], char *const envp[], const char *tty_path) 15 | { 16 | return syscall(3008, path, argv, envp, tty_path); 17 | } 18 | 19 | int main() 20 | { 21 | char* envp[] = {"HOME=/", "PATH=/initrd", NULL}; 22 | 23 | char* argv[2]; 24 | argv[0] = "/initrd/shell"; 25 | argv[1] = NULL; 26 | 27 | execute_on_tty(argv[0], argv, envp, "/dev/pts/1"); 28 | execute_on_tty(argv[0], argv, envp, "/dev/pts/2"); 29 | execute_on_tty(argv[0], argv, envp, "/dev/pts/3"); 30 | execute_on_tty(argv[0], argv, envp, "/dev/pts/4"); 31 | 32 | int console_fd = open("/dev/console", 0, 0); 33 | if (console_fd >= 0) 34 | { 35 | ioctl(console_fd, VT_SOSO_DISABLE, 7); 36 | ioctl(console_fd, VT_ACTIVATE, 7); 37 | } 38 | 39 | argv[0] = "/initrd/nano-X"; 40 | execute_on_tty(argv[0], argv, envp, "/dev/pts/7"); 41 | 42 | usleep(1000 * 1000); 43 | 44 | argv[0] = "/initrd/nanowm"; 45 | execute_on_tty(argv[0], argv, envp, "/dev/null"); 46 | 47 | usleep(1000 * 500); 48 | 49 | argv[0] = "/initrd/progs"; 50 | execute_on_tty(argv[0], argv, envp, "/dev/null"); 51 | 52 | usleep(1000 * 100); 53 | 54 | argv[0] = "/initrd/term"; 55 | execute_on_tty(argv[0], argv, envp, "/dev/null"); 56 | 57 | while (1) 58 | { 59 | usleep(1000 * 1000); 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /TinyGL/Changelog: -------------------------------------------------------------------------------- 1 | version 0.4: 2 | - added 24/32 bit rendering support (Olivier Landemarre - F. Bellard) 3 | - fixed GL_TRIANGLE_STRIP (Olivier Landemarre) 4 | - added gl_malloc, gl_free, gl_zalloc wrappers (Olivier Landemarre) 5 | 6 | version 0.3: 7 | - added NanoX API (nglx) (F. Bellard) 8 | - added gears example and unified GUI in examples (F. Bellard) 9 | - added TGL_FEATURE_RENDER_BITS so that it will be possible to render 10 | natively in 15/16/24 or 32 bits. (F. Bellard) 11 | - interpolated lines (Olivier Landemarre) 12 | - fast no shading case (Olivier Landemarre) 13 | - fast no projection case (Olivier Landemarre) 14 | 15 | version 0.2: Fabrice Bellard 16 | - added 24/32 bpp support. Added some features.h ifdefs. 17 | - fixed some error reporting cases in the examples 18 | - endianness is deduced from the glibc (BYTE_ORDER macro) 19 | 20 | version 0.19: Peder Blekken 21 | - new files BeOS/* src/msghandling.*, src/arrays.*, src/oscontext.c 22 | include/GL/oscontext.h src/features.h 23 | - added support for BeOS, see README.BEOS 24 | - added support for drawing convex polygons with unlimited # of vertices 25 | - added support for GL_LIGHT_MODEL_TWO_SIDE 26 | - added generic rotation code for glopRotate 27 | - added support for opengl 1.1 arrays 28 | - added support for glPolygonOffset, not implemented. 29 | - added glGetFloatv, limited support. 30 | - added some pnames for glGetIntegerv 31 | - added some empty functions in include/GL/gl.h to compile VRMLView 32 | - added GL_VERSION_1_1 define in include/GL/gl.h 33 | - fixed "bug" when context->gl_resize_viewport is not set. 34 | - fixed bug in glBindTexture (didn't accept texture object 0) 35 | 36 | version 0.1: 37 | - Initial revision, Fabrice Bellard 38 | 39 | -------------------------------------------------------------------------------- /kernel/kernelterminal_vga.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "kernelterminal_vga.h" 12 | 13 | #define SCREEN_ROW_COUNT 25 14 | #define SCREEN_COLUMN_COUNT 80 15 | 16 | static uint8_t * g_video_start = (uint8_t*)0xB8000; 17 | 18 | static uint8_t g_color = 0x0A; 19 | 20 | uint16_t vgaterminal_get_column_count() 21 | { 22 | return SCREEN_COLUMN_COUNT; 23 | } 24 | 25 | uint16_t vgaterminal_get_row_count() 26 | { 27 | return SCREEN_ROW_COUNT; 28 | } 29 | 30 | void vgaterminal_set_cursor_visible(BOOL visible) 31 | { 32 | uint8_t cursor = inb(0x3d5); 33 | 34 | if (visible) 35 | { 36 | cursor &= ~0x20;//5th bit cleared when cursor visible 37 | } 38 | else 39 | { 40 | cursor |= 0x20;//5th bit set when cursor invisible 41 | } 42 | outb(0x3D5, cursor); 43 | } 44 | 45 | void vgaterminal_set_character(uint16_t row, uint16_t column, uint8_t character) 46 | { 47 | uint8_t * video = g_video_start + (row * SCREEN_COLUMN_COUNT + column) * 2; 48 | 49 | *video++ = character; 50 | *video++ = g_color; 51 | } 52 | 53 | void vgaterminal_move_cursor(uint16_t row, uint16_t column) 54 | { 55 | uint16_t cursorLocation = row * SCREEN_COLUMN_COUNT + column; 56 | outb(0x3D4, 14); // Tell the VGA board we are setting the high cursor byte. 57 | outb(0x3D5, cursorLocation >> 8); // Send the high cursor byte. 58 | outb(0x3D4, 15); // Tell the VGA board we are setting the low cursor byte. 59 | outb(0x3D5, cursorLocation); // Send the low cursor byte. 60 | } -------------------------------------------------------------------------------- /kernel/isr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "common.h" 12 | #include "timer.h" 13 | #include "isr.h" 14 | 15 | IsrFunction g_interrupt_handlers[256]; 16 | 17 | uint32_t g_isr_count = 0; 18 | uint32_t g_irq_count = 0; 19 | 20 | void interrupt_register(uint8_t n, IsrFunction handler) 21 | { 22 | g_interrupt_handlers[n] = handler; 23 | } 24 | 25 | void handle_isr(Registers regs) 26 | { 27 | //Screen_PrintF("handle_isr interrupt no:%d\n", regs.int_no); 28 | 29 | g_isr_count++; 30 | 31 | uint8_t int_no = regs.interruptNumber & 0xFF; 32 | 33 | if (g_interrupt_handlers[int_no] != 0) 34 | { 35 | IsrFunction handler = g_interrupt_handlers[int_no]; 36 | handler(®s); 37 | } 38 | else 39 | { 40 | printkf("unhandled interrupt: %d\n", int_no); 41 | printkf("Tick: %d\n", get_system_tick_count()); 42 | PANIC("unhandled interrupt"); 43 | } 44 | } 45 | 46 | void handle_irq(Registers regs) 47 | { 48 | g_irq_count++; 49 | 50 | // end of interrupt message 51 | if (regs.interruptNumber >= 40) 52 | { 53 | //slave PIC 54 | outb(0xA0, 0x20); 55 | } 56 | 57 | outb(0x20, 0x20); 58 | 59 | //Screen_PrintF("irq: %d\n", regs.int_no); 60 | 61 | if (g_interrupt_handlers[regs.interruptNumber] != 0) 62 | { 63 | IsrFunction handler = g_interrupt_handlers[regs.interruptNumber]; 64 | handler(®s); 65 | } 66 | else 67 | { 68 | //printkf("unhandled IRQ: %d\n", regs.interruptNumber); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /kernel/keymap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef KEYMAP_H 12 | #define KEYMAP_H 13 | 14 | #include "common.h" 15 | 16 | typedef enum KeyModifier 17 | { 18 | KM_LeftShift = 1, 19 | KM_RightShift = 2, 20 | KM_Ctrl = 4, 21 | KM_Alt = 8 22 | } KeyModifier; 23 | 24 | enum 25 | { 26 | KEY_LEFTSHIFT = 0x2A, 27 | KEY_RIGHTSHIFT = 0x36, 28 | KEY_CTRL = 0x1D, 29 | KEY_ALT = 0x38, 30 | KEY_CAPSLOCK = 0x3A, 31 | KEY_F1 = 0x3B, 32 | KEY_F2 = 0x3C, 33 | KEY_F3 = 0x3D 34 | }; 35 | 36 | // PC keyboard interface constants 37 | 38 | #define KBSTATP 0x64 // kbd controller status port(I) 39 | #define KBS_DIB 0x01 // kbd data in buffer 40 | #define KBDATAP 0x60 // kbd data port(I) 41 | 42 | #define NO 0 43 | 44 | #define SHIFT (1<<0) 45 | #define CTL (1<<1) 46 | #define ALT (1<<2) 47 | 48 | #define CAPSLOCK (1<<3) 49 | #define NUMLOCK (1<<4) 50 | #define SCROLLLOCK (1<<5) 51 | 52 | #define E0ESC (1<<6) 53 | 54 | // Special keycodes 55 | #define KEY_HOME 0xE0 56 | #define KEY_END 0xE1 57 | #define KEY_UP 0xE2 58 | #define KEY_DOWN 0xE3 59 | #define KEY_LEFT 0xE4 60 | #define KEY_RIGHT 0xE5 61 | #define KEY_PAGEUP 0xE6 62 | #define KEY_PAGEDOWN 0xE7 63 | #define KEY_INSERT 0xE8 64 | #define KEY_DELETE 0xE9 65 | 66 | // C('A') == Control-A 67 | #define C(x) (x - '@') 68 | 69 | extern uint8_t g_key_map[256]; 70 | extern uint8_t g_key_shift_map[256]; 71 | 72 | #endif //KEYMAP_H -------------------------------------------------------------------------------- /kernel/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "label": "Build Kernel", 8 | "type": "shell", 9 | "command": "make", 10 | "options": { 11 | "cwd": "${workspaceFolder}/.." 12 | }, 13 | "problemMatcher": [], 14 | "group": { 15 | "kind": "build", 16 | "isDefault": true 17 | } 18 | }, 19 | { 20 | "label": "Clean Kernel", 21 | "type": "shell", 22 | "command": "make clean", 23 | "options": { 24 | "cwd": "${workspaceFolder}/.." 25 | }, 26 | "problemMatcher": [], 27 | "group": { 28 | "kind": "build", 29 | "isDefault": true 30 | } 31 | }, 32 | { 33 | "label": "Setup initrd", 34 | "type": "shell", 35 | "command": "mcopy -o -i initrd.fat userspace/bin/* ::", 36 | "options": { 37 | "cwd": "${workspaceFolder}/.." 38 | }, 39 | "problemMatcher": [], 40 | "group": { 41 | "kind": "build", 42 | "isDefault": true 43 | } 44 | }, 45 | { 46 | "label": "Create CD image", 47 | "type": "shell", 48 | "command": "./create-cd-image.sh", 49 | "options": { 50 | "cwd": "${workspaceFolder}/.." 51 | }, 52 | "problemMatcher": [], 53 | "group": { 54 | "kind": "build", 55 | "isDefault": true 56 | } 57 | } 58 | ] 59 | } -------------------------------------------------------------------------------- /TinyGL/src/get.c: -------------------------------------------------------------------------------- 1 | #include "zgl.h" 2 | 3 | void glGetIntegerv(int pname,int *params) 4 | { 5 | GLContext *c=gl_get_context(); 6 | 7 | switch(pname) { 8 | case GL_VIEWPORT: 9 | params[0]=c->viewport.xmin; 10 | params[1]=c->viewport.ymin; 11 | params[2]=c->viewport.xsize; 12 | params[3]=c->viewport.ysize; 13 | break; 14 | case GL_MAX_MODELVIEW_STACK_DEPTH: 15 | *params = MAX_MODELVIEW_STACK_DEPTH; 16 | break; 17 | case GL_MAX_PROJECTION_STACK_DEPTH: 18 | *params = MAX_PROJECTION_STACK_DEPTH; 19 | break; 20 | case GL_MAX_LIGHTS: 21 | *params = MAX_LIGHTS; 22 | break; 23 | case GL_MAX_TEXTURE_SIZE: 24 | *params = 256; /* not completely true, but... */ 25 | break; 26 | case GL_MAX_TEXTURE_STACK_DEPTH: 27 | *params = MAX_TEXTURE_STACK_DEPTH; 28 | break; 29 | default: 30 | gl_fatal_error("glGet: option not implemented"); 31 | break; 32 | } 33 | } 34 | 35 | void glGetFloatv(int pname, float *v) 36 | { 37 | int i; 38 | int mnr = 0; /* just a trick to return the correct matrix */ 39 | GLContext *c = gl_get_context(); 40 | switch (pname) { 41 | case GL_TEXTURE_MATRIX: 42 | mnr++; 43 | case GL_PROJECTION_MATRIX: 44 | mnr++; 45 | case GL_MODELVIEW_MATRIX: 46 | { 47 | float *p = &c->matrix_stack_ptr[mnr]->m[0][0];; 48 | for (i = 0; i < 4; i++) { 49 | *v++ = p[0]; 50 | *v++ = p[4]; 51 | *v++ = p[8]; 52 | *v++ = p[12]; 53 | p++; 54 | } 55 | } 56 | break; 57 | case GL_LINE_WIDTH: 58 | *v = 1.0f; 59 | break; 60 | case GL_LINE_WIDTH_RANGE: 61 | v[0] = v[1] = 1.0f; 62 | break; 63 | case GL_POINT_SIZE: 64 | *v = 1.0f; 65 | break; 66 | case GL_POINT_SIZE_RANGE: 67 | v[0] = v[1] = 1.0f; 68 | default: 69 | fprintf(stderr,"warning: unknown pname in glGetFloatv()\n"); 70 | break; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /TinyGL/config.mk: -------------------------------------------------------------------------------- 1 | ##################################################################### 2 | # C compiler 3 | 4 | # linux 5 | CC=musl-clang 6 | CFLAGS=-Wall -O2 7 | LFLAGS=-static 8 | 9 | # for BeOS PPC 10 | #CC= mwcc 11 | #CFLAGS= -I. -i- 12 | #LFLAGS= 13 | 14 | ##################################################################### 15 | # TinyGL configuration 16 | 17 | ##################################################################### 18 | # Select window API for TinyGL: 19 | 20 | # standard X11 GLX like API 21 | #TINYGL_USE_GLX=y 22 | 23 | # BEOS API 24 | #TINYGL_USE_BEOS=y 25 | 26 | # Micro Windows NanoX API 27 | #TINYGL_USE_NANOX=y 28 | 29 | ##################################################################### 30 | # X11 configuration (for the examples only) 31 | 32 | ifdef TINYGL_USE_GLX 33 | # Linux 34 | UI_LIBS= -L/usr/X11R6/lib -lX11 -lXext 35 | UI_INCLUDES= 36 | # Solaris 37 | #UI_LIBS= -L/usr/X11/lib -lX11 -lXext -lsocket -lnsl 38 | #UI_INCLUDES= 39 | 40 | UI_OBJS=x11.o 41 | endif 42 | 43 | ##################################################################### 44 | # Micro windowX11 configuration (for the examples only) 45 | 46 | ifdef TINYGL_USE_NANOX 47 | UI_LIBS= -lnano-X -lmwengine -lmwdrivers -lmwfonts 48 | UI_INCLUDES= 49 | 50 | # X11 target for nanoX 51 | UI_LIBS+= -L/usr/X11R6/lib -lX11 -lXext 52 | 53 | UI_OBJS=nanox.o 54 | endif 55 | 56 | ##################################################################### 57 | # OpenGL configuration (for the examples only) 58 | 59 | # use TinyGL 60 | GL_LIBS= -L../lib -lTinyGL 61 | GL_INCLUDES= -I../include 62 | GL_DEPS= ../lib/libTinyGL.a 63 | 64 | # use Mesa 65 | #GL_LIBS= -lMesaGL 66 | #GL_INCLUDES= 67 | #GL_DEPS= 68 | 69 | # use OpenGL 70 | #GL_LIBS= -lGL 71 | #GL_INCLUDES= 72 | #GL_DEPS= 73 | 74 | #################################################################### 75 | # Compile and link control 76 | 77 | # UNIX systems 78 | DIRS= src 79 | 80 | # BeOS 81 | # DIRS= src BeOS 82 | 83 | -------------------------------------------------------------------------------- /TinyGL/src/opinfo.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | ADD_OP(Color,7,"%f %f %f %f %d %d %d") 4 | ADD_OP(TexCoord,4,"%f %f %f %f") 5 | ADD_OP(EdgeFlag,1,"%d") 6 | ADD_OP(Normal,3,"%f %f %f") 7 | 8 | ADD_OP(Begin,1,"%C") 9 | ADD_OP(Vertex,4,"%f %f %f %f") 10 | ADD_OP(End,0,"") 11 | 12 | ADD_OP(EnableDisable,2,"%C %d") 13 | 14 | ADD_OP(MatrixMode,1,"%C") 15 | ADD_OP(LoadMatrix,16,"") 16 | ADD_OP(LoadIdentity,0,"") 17 | ADD_OP(MultMatrix,16,"") 18 | ADD_OP(PushMatrix,0,"") 19 | ADD_OP(PopMatrix,0,"") 20 | ADD_OP(Rotate,4,"%f %f %f %f") 21 | ADD_OP(Translate,3,"%f %f %f") 22 | ADD_OP(Scale,3,"%f %f %f") 23 | 24 | ADD_OP(Viewport,4,"%d %d %d %d") 25 | ADD_OP(Frustum,6,"%f %f %f %f %f %f") 26 | 27 | ADD_OP(Material,6,"%C %C %f %f %f %f") 28 | ADD_OP(ColorMaterial,2,"%C %C") 29 | ADD_OP(Light,6,"%C %C %f %f %f %f") 30 | ADD_OP(LightModel,5,"%C %f %f %f %f") 31 | 32 | ADD_OP(Clear,1,"%d") 33 | ADD_OP(ClearColor,4,"%f %f %f %f") 34 | ADD_OP(ClearDepth,1,"%f") 35 | 36 | ADD_OP(InitNames,0,"") 37 | ADD_OP(PushName,1,"%d") 38 | ADD_OP(PopName,0,"") 39 | ADD_OP(LoadName,1,"%d") 40 | 41 | ADD_OP(TexImage2D,9,"%d %d %d %d %d %d %d %d %d") 42 | ADD_OP(BindTexture,2,"%C %d") 43 | ADD_OP(TexEnv,7,"%C %C %C %f %f %f %f") 44 | ADD_OP(TexParameter,7,"%C %C %C %f %f %f %f") 45 | ADD_OP(PixelStore,2,"%C %C") 46 | 47 | ADD_OP(ShadeModel,1,"%C") 48 | ADD_OP(CullFace,1,"%C") 49 | ADD_OP(FrontFace,1,"%C") 50 | ADD_OP(PolygonMode,2,"%C %C") 51 | 52 | ADD_OP(CallList,1,"%d") 53 | ADD_OP(Hint,2,"%C %C") 54 | 55 | /* special opcodes */ 56 | ADD_OP(EndList,0,"") 57 | ADD_OP(NextBuffer,1,"%p") 58 | 59 | /* opengl 1.1 arrays */ 60 | ADD_OP(ArrayElement, 1, "%d") 61 | ADD_OP(EnableClientState, 1, "%C") 62 | ADD_OP(DisableClientState, 1, "%C") 63 | ADD_OP(VertexPointer, 4, "%d %C %d %p") 64 | ADD_OP(ColorPointer, 4, "%d %C %d %p") 65 | ADD_OP(NormalPointer, 3, "%C %d %p") 66 | ADD_OP(TexCoordPointer, 4, "%d %C %d %p") 67 | 68 | /* opengl 1.1 polygon offset */ 69 | ADD_OP(PolygonOffset, 2, "%f %f") 70 | 71 | #undef ADD_OP 72 | -------------------------------------------------------------------------------- /kernel/list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef LIST_H 12 | #define LIST_H 13 | 14 | #include "common.h" 15 | 16 | #define list_foreach(list_node, list) for (ListNode* list_node = list->head; NULL != list_node ; list_node = list_node->next) 17 | 18 | typedef struct ListNode 19 | { 20 | struct ListNode* previous; 21 | struct ListNode* next; 22 | void* data; 23 | } ListNode; 24 | 25 | typedef struct List 26 | { 27 | struct ListNode* head; 28 | struct ListNode* tail; 29 | } List; 30 | 31 | List* list_create(); 32 | void list_clear(List* list); 33 | void list_destroy(List* list); 34 | List* list_create_clone(List* list); 35 | BOOL list_is_empty(List* list); 36 | void list_append(List* list, void* data); 37 | void list_prepend(List* list, void* data); 38 | ListNode* list_get_first_node(List* list); 39 | ListNode* list_get_last_node(List* list); 40 | ListNode* list_find_first_occurrence(List* list, void* data); 41 | int list_find_first_occurrence_index(List* list, void* data); 42 | int list_get_count(List* list); 43 | void list_remove_node(List* list, ListNode* node); 44 | void list_remove_first_node(List* list); 45 | void list_remove_last_node(List* list); 46 | void list_remove_first_occurrence(List* list, void* data); 47 | 48 | typedef struct Stack 49 | { 50 | List* list; 51 | } Stack; 52 | 53 | Stack* stack_create(); 54 | void stack_clear(Stack* stack); 55 | void stack_destroy(Stack* stack); 56 | BOOL stack_is_empty(Stack* stack); 57 | void stack_push(Stack* stack, void* data); 58 | void* stack_pop(Stack* stack); 59 | 60 | typedef struct Queue 61 | { 62 | List* list; 63 | } Queue; 64 | 65 | Queue* queue_create(); 66 | void queue_clear(Queue* queue); 67 | void queue_destroy(Queue* queue); 68 | BOOL queue_is_empty(Queue* queue); 69 | void queue_enqueue(Queue* queue, void* data); 70 | void* queue_dequeue(Queue* queue); 71 | 72 | #endif // LIST_H 73 | -------------------------------------------------------------------------------- /kernel/ttydev.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef TTYDEV_H 12 | #define TTYDEV_H 13 | 14 | #include "common.h" 15 | #include "spinlock.h" 16 | #include "termios.h" 17 | #include "fs.h" 18 | 19 | #define TTYDEV_LINEBUFFER_SIZE 4096 20 | 21 | typedef struct FileSystemNode FileSystemNode; 22 | typedef struct FifoBuffer FifoBuffer; 23 | typedef struct List List; 24 | typedef struct Thread Thread; 25 | 26 | typedef struct winsize_t 27 | { 28 | uint16_t ws_row; /* rows, in characters */ 29 | uint16_t ws_col; /* columns, in characters */ 30 | uint16_t ws_xpixel; /* horizontal size, pixels */ 31 | uint16_t ws_ypixel; /* vertical size, pixels */ 32 | } winsize_t; 33 | 34 | typedef struct TtyDev TtyDev; 35 | 36 | typedef void (*TtyIOReady)(TtyDev* tty, uint32_t size); 37 | typedef struct TtyDev 38 | { 39 | FileSystemNode* master_node; 40 | FileSystemNode* slave_node; 41 | void* private_data; 42 | uint32_t pty_number; 43 | int32_t controlling_process; 44 | int32_t foreground_process; 45 | winsize_t winsize; 46 | FifoBuffer* buffer_master_write; 47 | Spinlock buffer_master_write_lock; 48 | FifoBuffer* buffer_master_read; 49 | Spinlock buffer_master_read_lock; 50 | FifoBuffer* buffer_echo; //used in only echoing by master_write, no need lock 51 | List* slave_readers; 52 | Spinlock slave_readers_lock; 53 | int32_t slave_open_count; 54 | Thread* master_reader; 55 | TtyIOReady master_read_ready; //used for kernel terminal, because it does not read like a user process 56 | uint8_t line_buffer[TTYDEV_LINEBUFFER_SIZE]; 57 | uint32_t line_buffer_index; 58 | BOOL is_closed; 59 | struct termios term; 60 | 61 | } TtyDev; 62 | 63 | FileSystemNode* ttydev_create(uint16_t column_count, uint16_t row_count); 64 | 65 | int32_t ttydev_master_read_nonblock(File *file, uint32_t size, uint8_t *buffer); 66 | 67 | #endif //TTYDEV_H -------------------------------------------------------------------------------- /kernel/vmm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef VMM_H 12 | #define VMM_H 13 | 14 | #include "common.h" 15 | 16 | #define MAP_FIXED 0x10 17 | 18 | typedef struct Process Process; 19 | typedef struct List List; 20 | 21 | extern uint32_t *g_kernel_page_directory; 22 | extern uint32_t g_kernel_page_directory_physical; 23 | 24 | 25 | #define SET_PAGEFRAME_USED(bitmap, page_index) bitmap[((uint32_t) page_index)/8] |= (1 << (((uint32_t) page_index)%8)) 26 | #define SET_PAGEFRAME_UNUSED(bitmap, p_addr) bitmap[((uint32_t) p_addr/PAGESIZE_4K)/8] &= ~(1 << (((uint32_t) p_addr/PAGESIZE_4K)%8)) 27 | #define IS_PAGEFRAME_USED(bitmap, page_index) (bitmap[((uint32_t) page_index)/8] & (1 << (((uint32_t) page_index)%8))) 28 | 29 | #define CHANGE_PD(pd) asm("mov %0, %%eax ;mov %%eax, %%cr3":: "m"(pd)) 30 | #define INVALIDATE(v_addr) asm("invlpg %0"::"m"(v_addr)) 31 | 32 | uint32_t vmm_acquire_page_frame_4k(); 33 | void vmm_release_page_frame_4k(uint32_t p_addr); 34 | 35 | void vmm_initialize(uint32_t high_mem); 36 | void unmap_first_4m(); 37 | 38 | uint32_t vmm_acquire_page_directory(); 39 | uint32_t vmm_clone_page_directory_with_memory2(); 40 | void vmm_destroy_page_directory_with_memory(uint32_t physical_pd); 41 | 42 | BOOL vmm_add_page_to_pd(char *v_addr, uint32_t p_addr, int flags); 43 | BOOL vmm_remove_page_from_pd(char *v_addr); 44 | 45 | void enable_paging(); 46 | void disable_paging(); 47 | 48 | uint32_t vmm_get_total_page_count(); 49 | uint32_t vmm_get_used_page_count(); 50 | uint32_t vmm_get_free_page_count(); 51 | 52 | void vmm_initialize_process_pages(Process* process); 53 | BOOL vmm_is_address_mappable(Process* process, uint32_t address, uint32_t page_count); 54 | void* vmm_map_memory(Process* process, uint32_t v_address_search_start, uint32_t* p_address_array, uint32_t page_count, BOOL own); 55 | BOOL vmm_map_memory_simple(Process * process, uint32_t v_address, uint32_t size); 56 | BOOL vmm_unmap_memory(Process* process, uint32_t v_address, uint32_t page_count); 57 | 58 | #endif // VMM_H 59 | -------------------------------------------------------------------------------- /TinyGL/src/zline.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "zbuffer.h" 3 | 4 | #define ZCMP(z,zpix) ((z) >= (zpix)) 5 | 6 | void ZB_plot(ZBuffer * zb, ZBufferPoint * p) 7 | { 8 | unsigned short *pz; 9 | PIXEL *pp; 10 | int zz; 11 | 12 | pz = zb->zbuf + (p->y * zb->xsize + p->x); 13 | pp = (PIXEL *) ((char *) zb->pbuf + zb->linesize * p->y + p->x * PSZB); 14 | zz = p->z >> ZB_POINT_Z_FRAC_BITS; 15 | if (ZCMP(zz, *pz)) { 16 | #if TGL_FEATURE_RENDER_BITS == 24 17 | pp[0]=p->r>>8; 18 | pp[1]=p->g>>8; 19 | pp[2]=p->b>>8; 20 | #else 21 | *pp = RGB_TO_PIXEL(p->r, p->g, p->b); 22 | #endif 23 | *pz = zz; 24 | } 25 | } 26 | 27 | #define INTERP_Z 28 | static void ZB_line_flat_z(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2, 29 | int color) 30 | { 31 | #include "zline.h" 32 | } 33 | 34 | /* line with color interpolation */ 35 | #define INTERP_Z 36 | #define INTERP_RGB 37 | static void ZB_line_interp_z(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2) 38 | { 39 | #include "zline.h" 40 | } 41 | 42 | /* no Z interpolation */ 43 | 44 | static void ZB_line_flat(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2, 45 | int color) 46 | { 47 | #include "zline.h" 48 | } 49 | 50 | #define INTERP_RGB 51 | static void ZB_line_interp(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2) 52 | { 53 | #include "zline.h" 54 | } 55 | 56 | void ZB_line_z(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2) 57 | { 58 | int color1, color2; 59 | 60 | color1 = RGB_TO_PIXEL(p1->r, p1->g, p1->b); 61 | color2 = RGB_TO_PIXEL(p2->r, p2->g, p2->b); 62 | 63 | /* choose if the line should have its color interpolated or not */ 64 | if (color1 == color2) { 65 | ZB_line_flat_z(zb, p1, p2, color1); 66 | } else { 67 | ZB_line_interp_z(zb, p1, p2); 68 | } 69 | } 70 | 71 | void ZB_line(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2) 72 | { 73 | int color1, color2; 74 | 75 | color1 = RGB_TO_PIXEL(p1->r, p1->g, p1->b); 76 | color2 = RGB_TO_PIXEL(p2->r, p2->g, p2->b); 77 | 78 | /* choose if the line should have its color interpolated or not */ 79 | if (color1 == color2) { 80 | ZB_line_flat(zb, p1, p2, color1); 81 | } else { 82 | ZB_line_interp(zb, p1, p2); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /TinyGL/src/oscontext.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "zbuffer.h" 3 | #include "zgl.h" 4 | #include 5 | #include 6 | #include 7 | 8 | static int buffercnt = 0; 9 | 10 | ostgl_context * 11 | ostgl_create_context(const int xsize, 12 | const int ysize, 13 | const int depth, 14 | void **framebuffers, 15 | const int numbuffers) 16 | { 17 | ostgl_context *context; 18 | int i; 19 | ZBuffer *zb; 20 | 21 | assert(depth == 16); /* support for other depths must include bpp 22 | convertion */ 23 | assert(numbuffers >= 1); 24 | 25 | context = gl_malloc(sizeof(ostgl_context)); 26 | assert(context); 27 | context->zbs = gl_malloc(sizeof(void*)*numbuffers); 28 | context->framebuffers = gl_malloc(sizeof(void*)*numbuffers); 29 | 30 | assert(context->zbs != NULL && context->framebuffers != NULL); 31 | 32 | for (i = 0; i < numbuffers; i++) { 33 | context->framebuffers[i] = framebuffers[i]; 34 | zb = ZB_open(xsize, ysize, ZB_MODE_5R6G5B, 0, NULL, NULL, framebuffers[i]); 35 | if (zb == NULL) { 36 | fprintf(stderr, "Error while initializing Z buffer\n"); 37 | exit(1); 38 | } 39 | context->zbs[i] = zb; 40 | } 41 | if (++buffercnt == 1) { 42 | glInit(context->zbs[0]); 43 | } 44 | context->xsize = xsize; 45 | context->ysize = ysize; 46 | context->numbuffers = numbuffers; 47 | return context; 48 | } 49 | 50 | void 51 | ostgl_delete_context(ostgl_context *context) 52 | { 53 | int i; 54 | for (i = 0; i < context->numbuffers; i++) { 55 | ZB_close(context->zbs[i]); 56 | } 57 | gl_free(context->zbs); 58 | gl_free(context->framebuffers); 59 | gl_free(context); 60 | 61 | if (--buffercnt == 0) { 62 | glClose(); 63 | } 64 | } 65 | 66 | void 67 | ostgl_make_current(ostgl_context *oscontext, const int idx) 68 | { 69 | GLContext *context = gl_get_context(); 70 | assert(idx < oscontext->numbuffers); 71 | context->zb = oscontext->zbs[idx]; 72 | } 73 | 74 | void 75 | ostgl_resize(ostgl_context *context, 76 | const int xsize, 77 | const int ysize, 78 | void **framebuffers) 79 | { 80 | int i; 81 | for (i = 0; i < context->numbuffers; i++) { 82 | ZB_resize(context->zbs[i], framebuffers[i], xsize, ysize); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /userspace/sterm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #define BUFFER_SIZE 128 19 | 20 | #define MAX(a,b) (((a)>(b))?(a):(b)) 21 | 22 | extern char **environ; 23 | 24 | int execute_on_tty(const char *path, char *const argv[], char *const envp[], const char *tty_path) 25 | { 26 | return syscall(3008, path, argv, envp, tty_path); 27 | } 28 | 29 | int posix_openpt_(int flags) 30 | { 31 | return syscall(3001, flags); 32 | } 33 | 34 | int ptsname_r_(int fd, char *buf, int buflen) 35 | { 36 | return syscall(3002, fd, buf, buflen); 37 | } 38 | 39 | int main() 40 | { 41 | int fdSerial = open("/dev/com1", O_RDWR); 42 | 43 | if (fdSerial < 0) 44 | { 45 | return 1; 46 | } 47 | 48 | int fdMaster = posix_openpt_(O_RDWR | O_NOCTTY | O_NONBLOCK); 49 | //printf("master:%d\n", master); 50 | 51 | if (fdMaster < 0) 52 | { 53 | return 1; 54 | } 55 | 56 | char slavePath[128]; 57 | ptsname_r_(fdMaster, slavePath, 128); 58 | 59 | fd_set rfds; 60 | struct timeval tv; 61 | 62 | char* argv[2]; 63 | argv[0] = "/initrd/shell"; 64 | argv[1] = NULL; 65 | 66 | execute_on_tty(argv[0], argv, environ, slavePath); 67 | 68 | int maxFd = MAX(fdSerial, fdMaster); 69 | 70 | char buffer[BUFFER_SIZE]; 71 | while (1) 72 | { 73 | tv.tv_sec = 0; 74 | tv.tv_usec = 100000; 75 | 76 | FD_ZERO(&rfds); 77 | FD_SET(fdSerial, &rfds); 78 | FD_SET(fdMaster, &rfds); 79 | 80 | int sel = select(maxFd + 1, &rfds, NULL, NULL, &tv); 81 | 82 | if (sel > 0) 83 | { 84 | if (FD_ISSET(fdSerial, &rfds)) 85 | { 86 | int bytes = read(fdSerial, buffer, BUFFER_SIZE); 87 | 88 | if (bytes > 0) 89 | { 90 | write(fdMaster, buffer, bytes); 91 | } 92 | } 93 | 94 | if (FD_ISSET(fdMaster, &rfds)) 95 | { 96 | int bytes = read(fdMaster, buffer, BUFFER_SIZE); 97 | 98 | if (bytes > 0) 99 | { 100 | write(fdSerial, buffer, bytes); 101 | } 102 | } 103 | } 104 | } 105 | 106 | 107 | return 0; 108 | } -------------------------------------------------------------------------------- /kernel/keymap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "keymap.h" 12 | 13 | uint8_t g_key_map[256] = 14 | { 15 | NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00 16 | '7', '8', '9', '0', '-', '=', '\b', '\t', 17 | 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 0x10 18 | 'o', 'p', '[', ']', '\n', NO, 'a', 's', 19 | 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 0x20 20 | '\'', '`', NO, '\\', 'z', 'x', 'c', 'v', 21 | 'b', 'n', 'm', ',', '.', '/', NO, '*', // 0x30 22 | NO, ' ', NO, NO, NO, NO, NO, NO, 23 | NO, NO, NO, NO, NO, NO, NO, '7', // 0x40 24 | '8', '9', '-', '4', '5', '6', '+', '1', 25 | '2', '3', '0', '.', NO, NO, NO, NO, // 0x50 26 | [0x49] = KEY_PAGEUP, 27 | [0x51] = KEY_PAGEDOWN, 28 | [0x47] = KEY_HOME, 29 | [0x4F] = KEY_END, 30 | [0x52] = KEY_INSERT, 31 | [0x53] = KEY_DELETE, 32 | [0x48] = KEY_UP, 33 | [0x50] = KEY_DOWN, 34 | [0x4B] = KEY_LEFT, 35 | [0x4D] = KEY_RIGHT, 36 | [0x9C] = '\n', // KP_Enter 37 | [0xB5] = '/', // KP_Div 38 | [0xC8] = KEY_UP, 39 | [0xD0] = KEY_DOWN, 40 | [0xC9] = KEY_PAGEUP, 41 | [0xD1] = KEY_PAGEDOWN, 42 | [0xCB] = KEY_LEFT, 43 | [0xCD] = KEY_RIGHT, 44 | [0x97] = KEY_HOME, 45 | [0xCF] = KEY_END, 46 | [0xD2] = KEY_INSERT, 47 | [0xD3] = KEY_DELETE 48 | }; 49 | 50 | uint8_t g_key_shift_map[256] = 51 | { 52 | NO, 033, '!', '@', '#', '$', '%', '^', // 0x00 53 | '&', '*', '(', ')', '_', '+', '\b', '\t', 54 | 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 0x10 55 | 'O', 'P', '{', '}', '\n', NO, 'A', 'S', 56 | 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 0x20 57 | '"', '~', NO, '|', 'Z', 'X', 'C', 'V', 58 | 'B', 'N', 'M', '<', '>', '?', NO, '*', // 0x30 59 | NO, ' ', NO, NO, NO, NO, NO, NO, 60 | NO, NO, NO, NO, NO, NO, NO, '7', // 0x40 61 | '8', '9', '-', '4', '5', '6', '+', '1', 62 | '2', '3', '0', '.', NO, NO, NO, NO, // 0x50 63 | [0x49] = KEY_PAGEUP, 64 | [0x51] = KEY_PAGEDOWN, 65 | [0x47] = KEY_HOME, 66 | [0x4F] = KEY_END, 67 | [0x52] = KEY_INSERT, 68 | [0x53] = KEY_DELETE, 69 | [0x48] = KEY_UP, 70 | [0x50] = KEY_DOWN, 71 | [0x4B] = KEY_LEFT, 72 | [0x4D] = KEY_RIGHT, 73 | [0x9C] = '\n', // KP_Enter 74 | [0xB5] = '/', // KP_Div 75 | [0xC8] = KEY_UP, 76 | [0xD0] = KEY_DOWN, 77 | [0xC9] = KEY_PAGEUP, 78 | [0xD1] = KEY_PAGEDOWN, 79 | [0xCB] = KEY_LEFT, 80 | [0xCD] = KEY_RIGHT, 81 | [0x97] = KEY_HOME, 82 | [0xCF] = KEY_END, 83 | [0xD2] = KEY_INSERT, 84 | [0xD3] = KEY_DELETE 85 | }; -------------------------------------------------------------------------------- /TinyGL/src/select.c: -------------------------------------------------------------------------------- 1 | #include "zgl.h" 2 | 3 | int glRenderMode(int mode) 4 | { 5 | GLContext *c=gl_get_context(); 6 | int result=0; 7 | 8 | switch(c->render_mode) { 9 | case GL_RENDER: 10 | break; 11 | case GL_SELECT: 12 | if (c->select_overflow) { 13 | result=-c->select_hits; 14 | } else { 15 | result=c->select_hits; 16 | } 17 | c->select_overflow=0; 18 | c->select_ptr=c->select_buffer; 19 | c->name_stack_size=0; 20 | break; 21 | default: 22 | assert(0); 23 | } 24 | switch(mode) { 25 | case GL_RENDER: 26 | c->render_mode=GL_RENDER; 27 | break; 28 | case GL_SELECT: 29 | c->render_mode=GL_SELECT; 30 | assert( c->select_buffer != NULL); 31 | c->select_ptr=c->select_buffer; 32 | c->select_hits=0; 33 | c->select_overflow=0; 34 | c->select_hit=NULL; 35 | break; 36 | default: 37 | assert(0); 38 | } 39 | return result; 40 | } 41 | 42 | void glSelectBuffer(int size,unsigned int *buf) 43 | { 44 | GLContext *c=gl_get_context(); 45 | 46 | assert(c->render_mode != GL_SELECT); 47 | 48 | c->select_buffer=buf; 49 | c->select_size=size; 50 | } 51 | 52 | 53 | void glopInitNames(GLContext *c,GLParam *p) 54 | { 55 | if (c->render_mode == GL_SELECT) { 56 | c->name_stack_size=0; 57 | c->select_hit=NULL; 58 | } 59 | } 60 | 61 | void glopPushName(GLContext *c,GLParam *p) 62 | { 63 | if (c->render_mode == GL_SELECT) { 64 | assert(c->name_stack_sizename_stack[c->name_stack_size++]=p[1].i; 66 | c->select_hit=NULL; 67 | } 68 | } 69 | 70 | void glopPopName(GLContext *c,GLParam *p) 71 | { 72 | if (c->render_mode == GL_SELECT) { 73 | assert(c->name_stack_size>0); 74 | c->name_stack_size--; 75 | c->select_hit=NULL; 76 | } 77 | } 78 | 79 | void glopLoadName(GLContext *c,GLParam *p) 80 | { 81 | if (c->render_mode == GL_SELECT) { 82 | assert(c->name_stack_size>0); 83 | c->name_stack[c->name_stack_size-1]=p[1].i; 84 | c->select_hit=NULL; 85 | } 86 | } 87 | 88 | void gl_add_select(GLContext *c,unsigned int zmin,unsigned int zmax) 89 | { 90 | unsigned int *ptr; 91 | int n,i; 92 | 93 | if (!c->select_overflow) { 94 | if (c->select_hit==NULL) { 95 | n=c->name_stack_size; 96 | if ((c->select_ptr-c->select_buffer+3+n) > 97 | c->select_size) { 98 | c->select_overflow=1; 99 | } else { 100 | ptr=c->select_ptr; 101 | c->select_hit=ptr; 102 | *ptr++=c->name_stack_size; 103 | *ptr++=zmin; 104 | *ptr++=zmax; 105 | for(i=0;iname_stack[i]; 106 | c->select_ptr=ptr; 107 | c->select_hits++; 108 | } 109 | } else { 110 | if (zminselect_hit[1]) c->select_hit[1]=zmin; 111 | if (zmax>c->select_hit[2]) c->select_hit[2]=zmax; 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /kernel/syscalltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef SYSCALLTABLE_H 12 | #define SYSCALLTABLE_H 13 | 14 | //C library must be compatible with these numbers. 15 | enum 16 | { 17 | SYS_exit = 1, 18 | SYS_fork = 2, 19 | SYS_read = 3, 20 | SYS_write = 4, 21 | SYS_open = 5, 22 | SYS_close = 6, 23 | SYS_unlink = 10, 24 | SYS_execve = 11, 25 | SYS_chdir = 12, 26 | SYS_lseek = 19, 27 | SYS_getpid = 20, 28 | SYS_mount = 21, 29 | SYS_unmount = 22, 30 | SYS_kill = 37, 31 | SYS_mkdir = 39, 32 | SYS_rmdir = 40, 33 | SYS_brk = 45, 34 | SYS_ioctl = 54, 35 | SYS_setpgid = 57, 36 | SYS_mmap = 90, 37 | SYS_munmap = 91, 38 | SYS_ftruncate = 93, 39 | SYS_stat = 106, 40 | SYS_fstat = 108, 41 | SYS_wait4 = 114, 42 | SYS_getpgid = 132, 43 | SYS_llseek = 140, 44 | SYS_select = 142, 45 | SYS_readv = 145, 46 | SYS_writev = 146, 47 | SYS_nanosleep = 162, 48 | SYS_rt_sigaction = 174, 49 | SYS_rt_sigprocmask = 175, 50 | SYS_getcwd = 183, 51 | SYS_mmap2 = 192, 52 | SYS_ftruncate64 = 194, 53 | SYS_getdents64 = 220, 54 | SYS_set_thread_area = 243, 55 | SYS_exit_group = 252, 56 | SYS_set_tid_address = 258, 57 | 58 | SYS_socket = 359, 59 | SYS_socketpair = 360, 60 | SYS_bind = 361, 61 | SYS_connect = 362, 62 | SYS_listen = 363, 63 | SYS_accept4 = 364, 64 | SYS_getsockopt = 365, 65 | SYS_setsockopt = 366, 66 | SYS_getsockname = 367, 67 | SYS_getpeername = 368, 68 | SYS_sendto = 369, 69 | SYS_sendmsg = 370, 70 | SYS_recvfrom = 371, 71 | SYS_recvmsg = 372, 72 | SYS_shutdown = 373, 73 | 74 | SYS_statx = 383, 75 | 76 | SYS_shmget = 395, 77 | SYS_shmctl = 396, 78 | SYS_shmat = 397, 79 | SYS_shmdt = 398, 80 | 81 | SYS_clock_gettime64 = 403, 82 | SYS_clock_settime64 = 404, 83 | SYS_clock_getres64 = 406, 84 | 85 | 86 | //soso specific 87 | //TODO: make them linux compatible and delete below 88 | //we can keep some of them like SYS_getprocs vs. 89 | //but openpt and ptsname really should be implemented by /dev/ptmx 90 | 91 | SYS_posix_openpt = 3001, 92 | SYS_ptsname_r, 93 | SYS_accept, 94 | SYS_getthreads, 95 | SYS_getprocs, 96 | SYS_unused3, 97 | SYS_execute, 98 | SYS_execute_on_tty, 99 | SYS_soso_read_dir, 100 | SYS_unused, 101 | SYS_unused2, 102 | SYS_manage_message, 103 | SYS_manage_pipe, 104 | SYS_wait, 105 | SYS_printk, 106 | 107 | SYSCALL_COUNT 108 | }; 109 | 110 | #endif // SYSCALLTABLE_H 111 | -------------------------------------------------------------------------------- /userspace/ps.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | #define SOSO_MAX_OPENED_FILES 20 21 | #define SOSO_PROCESS_NAME_MAX 32 22 | 23 | typedef struct ThreadInfo 24 | { 25 | uint32_t thread_id; 26 | uint32_t process_id; 27 | uint32_t state; 28 | uint32_t user_mode; 29 | 30 | uint32_t birth_time; 31 | uint32_t context_switch_count; 32 | uint32_t context_start_time; 33 | uint32_t context_end_time; 34 | uint32_t consumed_cpu_time_ms; 35 | uint32_t usage_cpu; 36 | uint32_t called_syscall_count; 37 | } ThreadInfo; 38 | 39 | typedef struct ProcInfo 40 | { 41 | uint32_t process_id; 42 | uint32_t process_gid; 43 | int32_t parent_process_id; 44 | uint32_t fd[SOSO_MAX_OPENED_FILES]; 45 | 46 | char name[SOSO_PROCESS_NAME_MAX]; 47 | char tty[128]; 48 | char working_directory[128]; 49 | } ProcInfo; 50 | 51 | int32_t getthreads(ThreadInfo* threads, uint32_t max_count, uint32_t flags) 52 | { 53 | return syscall(3004, threads, max_count, flags); 54 | } 55 | 56 | int32_t getprocs(ProcInfo* procs, uint32_t max_count, uint32_t flags) 57 | { 58 | return syscall(3005, procs, max_count, flags); 59 | } 60 | 61 | int get_process_cpu_usage(uint32_t pid, ThreadInfo* threads, uint32_t count) 62 | { 63 | for (size_t i = 0; i < count; i++) 64 | { 65 | ThreadInfo* info = threads + i; 66 | 67 | if (info->process_id == pid) 68 | { 69 | return info->usage_cpu; 70 | } 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | int main(int argc, char** argv) 77 | { 78 | int seconds = 1; 79 | 80 | if (argc > 1) 81 | { 82 | sscanf(argv[1], "%d", &seconds); 83 | } 84 | 85 | ProcInfo procs[20]; 86 | int proc_count = getprocs(procs, 20, 0); 87 | if (proc_count < 0) 88 | { 89 | printf("Not supported!\n"); 90 | return 0; 91 | } 92 | 93 | ThreadInfo threads[20]; 94 | int thread_count = getthreads(threads, 20, 0); 95 | 96 | printf("Process count: %d\n", proc_count); 97 | printf("PID\tGID\tPARENT\tCPU(%%)\tCMD\n"); 98 | for (size_t i = 0; i < proc_count; i++) 99 | { 100 | ProcInfo* p = procs + i; 101 | 102 | uint32_t usage = get_process_cpu_usage(p->process_id, threads, thread_count); 103 | 104 | printf("%d\t%d\t%d\t%d\t%s\n", p->process_id, p->process_gid, p->parent_process_id, usage, p->name); 105 | } 106 | 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /kernel/fatfs_diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2014 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #include "fatfs_integer.h" 13 | 14 | 15 | /* Status of Disk Functions */ 16 | typedef BYTE DSTATUS; 17 | 18 | /* Results of Disk Functions */ 19 | typedef enum { 20 | RES_OK = 0, /* 0: Successful */ 21 | RES_ERROR, /* 1: R/W Error */ 22 | RES_WRPRT, /* 2: Write Protected */ 23 | RES_NOTRDY, /* 3: Not Ready */ 24 | RES_PARERR /* 4: Invalid Parameter */ 25 | } DRESULT; 26 | 27 | 28 | /*---------------------------------------*/ 29 | /* Prototypes for disk control functions */ 30 | 31 | 32 | DSTATUS disk_initialize (BYTE pdrv); 33 | DSTATUS disk_status (BYTE pdrv); 34 | DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); 35 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); 36 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 37 | 38 | 39 | /* Disk Status Bits (DSTATUS) */ 40 | 41 | #define STA_NOINIT 0x01 /* Drive not initialized */ 42 | #define STA_NODISK 0x02 /* No medium in the drive */ 43 | #define STA_PROTECT 0x04 /* Write protected */ 44 | 45 | 46 | /* Command code for disk_ioctrl fucntion */ 47 | 48 | /* Generic command (Used by FatFs) */ 49 | #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ 50 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ 51 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ 52 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ 53 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ 54 | 55 | /* Generic command (Not used by FatFs) */ 56 | #define CTRL_POWER 5 /* Get/Set power status */ 57 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 58 | #define CTRL_EJECT 7 /* Eject media */ 59 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 60 | 61 | /* MMC/SDC specific ioctl command */ 62 | #define MMC_GET_TYPE 10 /* Get card type */ 63 | #define MMC_GET_CSD 11 /* Get CSD */ 64 | #define MMC_GET_CID 12 /* Get CID */ 65 | #define MMC_GET_OCR 13 /* Get OCR */ 66 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 67 | #define ISDIO_READ 55 /* Read data form SD iSDIO register */ 68 | #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ 69 | #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ 70 | 71 | /* ATA/CF specific ioctl command */ 72 | #define ATA_GET_REV 20 /* Get F/W revision */ 73 | #define ATA_GET_MODEL 21 /* Get model name */ 74 | #define ATA_GET_SN 22 /* Get serial number */ 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /kernel/timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "timer.h" 12 | #include "isr.h" 13 | #include "process.h" 14 | #include "common.h" 15 | 16 | #define TIMER_FREQ 1000 17 | 18 | uint64_t g_system_tick_count = 0; 19 | 20 | uint64_t g_system_date_ms = 0; 21 | 22 | BOOL g_scheduler_enabled = FALSE; 23 | 24 | //called from assembly 25 | void handle_timer_irq(TimerInt_Registers registers) 26 | { 27 | g_system_tick_count++; 28 | 29 | g_system_date_ms++; 30 | 31 | if (g_scheduler_enabled == TRUE) 32 | { 33 | schedule(®isters); 34 | } 35 | } 36 | 37 | uint32_t get_system_tick_count() 38 | { 39 | return (uint32_t)g_system_tick_count; 40 | } 41 | 42 | uint64_t get_system_tick_count64() 43 | { 44 | return g_system_tick_count; 45 | } 46 | 47 | uint32_t get_uptime_seconds() 48 | { 49 | return ((uint32_t)g_system_tick_count) / TIMER_FREQ; 50 | } 51 | 52 | uint64_t get_uptime_seconds64() 53 | { 54 | //TODO: make this real 64 bit 55 | return get_uptime_seconds(); 56 | } 57 | 58 | uint32_t get_uptime_milliseconds() 59 | { 60 | return (uint32_t)g_system_tick_count; 61 | } 62 | 63 | uint64_t get_uptime_milliseconds64() 64 | { 65 | return g_system_tick_count; 66 | } 67 | 68 | void scheduler_enable() 69 | { 70 | g_scheduler_enabled = TRUE; 71 | } 72 | 73 | void scheduler_disable() 74 | { 75 | g_scheduler_enabled = FALSE; 76 | } 77 | 78 | static void timer_init(uint32_t frequency) 79 | { 80 | uint32_t divisor = 1193180 / frequency; 81 | 82 | outb(0x43, 0x36); 83 | 84 | uint8_t l = (uint8_t)(divisor & 0xFF); 85 | uint8_t h = (uint8_t)( (divisor>>8) & 0xFF ); 86 | 87 | outb(0x40, l); 88 | outb(0x40, h); 89 | } 90 | 91 | void timer_initialize() 92 | { 93 | timer_init(TIMER_FREQ); 94 | } 95 | 96 | int32_t clock_getres64(int32_t clockid, struct timespec *res) 97 | { 98 | res->tv_sec = 0; 99 | res->tv_nsec = 1000000; 100 | 101 | return 0; 102 | } 103 | 104 | int32_t clock_gettime64(int32_t clockid, struct timespec *tp) 105 | { 106 | //TODO: clockid 107 | 108 | //TODO: make proper use of 64 bit fields 109 | 110 | uint32_t uptime_milli = g_system_date_ms; 111 | 112 | tp->tv_sec = uptime_milli / TIMER_FREQ; 113 | 114 | uint32_t extra_milli = uptime_milli - tp->tv_sec * 1000; 115 | 116 | tp->tv_nsec = extra_milli * 1000000; 117 | 118 | return 0; 119 | } 120 | 121 | int32_t clock_settime64(int32_t clockid, const struct timespec *tp) 122 | { 123 | //TODO: clockid 124 | 125 | //TODO: make use of tv_nsec 126 | 127 | uint32_t uptime_milli = g_system_date_ms; 128 | 129 | g_system_date_ms = tp->tv_sec * TIMER_FREQ; 130 | 131 | return 0; 132 | } -------------------------------------------------------------------------------- /TinyGL/src/zline.h: -------------------------------------------------------------------------------- 1 | { 2 | int n, dx, dy, sx, pp_inc_1, pp_inc_2; 3 | register int a; 4 | register PIXEL *pp; 5 | #if defined(INTERP_RGB) || TGL_FEATURE_RENDER_BITS == 24 6 | register unsigned int r, g, b; 7 | #endif 8 | #ifdef INTERP_RGB 9 | register unsigned int rinc, ginc, binc; 10 | #endif 11 | #ifdef INTERP_Z 12 | register unsigned short *pz; 13 | int zinc; 14 | register int z, zz; 15 | #endif 16 | 17 | if (p1->y > p2->y || (p1->y == p2->y && p1->x > p2->x)) { 18 | ZBufferPoint *tmp; 19 | tmp = p1; 20 | p1 = p2; 21 | p2 = tmp; 22 | } 23 | sx = zb->xsize; 24 | pp = (PIXEL *) ((char *) zb->pbuf + zb->linesize * p1->y + p1->x * PSZB); 25 | #ifdef INTERP_Z 26 | pz = zb->zbuf + (p1->y * sx + p1->x); 27 | z = p1->z; 28 | #endif 29 | 30 | dx = p2->x - p1->x; 31 | dy = p2->y - p1->y; 32 | #ifdef INTERP_RGB 33 | r = p2->r << 8; 34 | g = p2->g << 8; 35 | b = p2->b << 8; 36 | #elif TGL_FEATURE_RENDER_BITS == 24 37 | /* for 24 bits, we store the colors in different variables */ 38 | r = p2->r >> 8; 39 | g = p2->g >> 8; 40 | b = p2->b >> 8; 41 | #endif 42 | 43 | #ifdef INTERP_RGB 44 | #define RGB(x) x 45 | #if TGL_FEATURE_RENDER_BITS == 24 46 | #define RGBPIXEL pp[0] = r >> 16, pp[1] = g >> 16, pp[2] = b >> 16 47 | #else 48 | #define RGBPIXEL *pp = RGB_TO_PIXEL(r >> 8,g >> 8,b >> 8) 49 | #endif 50 | #else /* INTERP_RGB */ 51 | #define RGB(x) 52 | #if TGL_FEATURE_RENDER_BITS == 24 53 | #define RGBPIXEL pp[0] = r, pp[1] = g, pp[2] = b 54 | #else 55 | #define RGBPIXEL *pp = color 56 | #endif 57 | #endif /* INTERP_RGB */ 58 | 59 | #ifdef INTERP_Z 60 | #define ZZ(x) x 61 | #define PUTPIXEL() \ 62 | { \ 63 | zz=z >> ZB_POINT_Z_FRAC_BITS; \ 64 | if (ZCMP(zz,*pz)) { \ 65 | RGBPIXEL; \ 66 | *pz=zz; \ 67 | } \ 68 | } 69 | #else /* INTERP_Z */ 70 | #define ZZ(x) 71 | #define PUTPIXEL() RGBPIXEL 72 | #endif /* INTERP_Z */ 73 | 74 | #define DRAWLINE(dx,dy,inc_1,inc_2) \ 75 | n=dx;\ 76 | ZZ(zinc=(p2->z-p1->z)/n);\ 77 | RGB(rinc=((p2->r-p1->r) << 8)/n;\ 78 | ginc=((p2->g-p1->g) << 8)/n;\ 79 | binc=((p2->b-p1->b) << 8)/n);\ 80 | a=2*dy-dx;\ 81 | dy=2*dy;\ 82 | dx=2*dx-dy;\ 83 | pp_inc_1 = (inc_1) * PSZB;\ 84 | pp_inc_2 = (inc_2) * PSZB;\ 85 | do {\ 86 | PUTPIXEL();\ 87 | ZZ(z+=zinc);\ 88 | RGB(r+=rinc;g+=ginc;b+=binc);\ 89 | if (a>0) { pp=(PIXEL *)((char *)pp + pp_inc_1); ZZ(pz+=(inc_1)); a-=dx; }\ 90 | else { pp=(PIXEL *)((char *)pp + pp_inc_2); ZZ(pz+=(inc_2)); a+=dy; }\ 91 | } while (--n >= 0); 92 | 93 | /* fin macro */ 94 | 95 | if (dx == 0 && dy == 0) { 96 | PUTPIXEL(); 97 | } else if (dx > 0) { 98 | if (dx >= dy) { 99 | DRAWLINE(dx, dy, sx + 1, 1); 100 | } else { 101 | DRAWLINE(dy, dx, sx + 1, sx); 102 | } 103 | } else { 104 | dx = -dx; 105 | if (dx >= dy) { 106 | DRAWLINE(dx, dy, sx - 1, -1); 107 | } else { 108 | DRAWLINE(dy, dx, sx - 1, sx); 109 | } 110 | } 111 | } 112 | 113 | #undef INTERP_Z 114 | #undef INTERP_RGB 115 | 116 | /* internal defines */ 117 | #undef DRAWLINE 118 | #undef PUTPIXEL 119 | #undef ZZ 120 | #undef RGB 121 | #undef RGBPIXEL 122 | -------------------------------------------------------------------------------- /kernel/log.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "log.h" 12 | #include "common.h" 13 | #include "fs.h" 14 | #include "serial.h" 15 | 16 | static File* g_file = NULL; 17 | static BOOL g_log_serial = FALSE; 18 | 19 | void log_initialize_serial() 20 | { 21 | g_log_serial = TRUE; 22 | 23 | log_printf("Logging initialized for serial!\n"); 24 | } 25 | 26 | void log_initialize(const char* file_name) 27 | { 28 | if (file_name) 29 | { 30 | FileSystemNode* node = fs_get_node(file_name); 31 | 32 | BOOL success = FALSE; 33 | 34 | if (node) 35 | { 36 | g_file = fs_open(node, 0); 37 | 38 | if (g_file) 39 | { 40 | log_printf("Logging initialized for %s!\n", file_name); 41 | success = TRUE; 42 | } 43 | } 44 | 45 | if (!success) 46 | { 47 | log_printf("Logging failed to initialize for %s!!\n", file_name); 48 | } 49 | } 50 | } 51 | 52 | void log_printf(const char *format, ...) 53 | { 54 | char **arg = (char **) &format; 55 | char c; 56 | char buf[20]; 57 | char buffer[512]; 58 | 59 | int buffer_index = 0; 60 | 61 | __builtin_va_list vl; 62 | __builtin_va_start(vl, format); 63 | 64 | while ((c = *format++) != 0) 65 | { 66 | if (buffer_index > 510) 67 | { 68 | break; 69 | } 70 | 71 | if (c != '%') 72 | buffer[buffer_index++] = c; 73 | else 74 | { 75 | char *p; 76 | 77 | c = *format++; 78 | switch (c) 79 | { 80 | case 'x': 81 | buf[0] = '0'; 82 | buf[1] = 'x'; 83 | //itoa (buf + 2, c, *((int *) arg++)); 84 | itoa (buf + 2, c, __builtin_va_arg(vl, int)); 85 | p = buf; 86 | goto string; 87 | break; 88 | case 'd': 89 | case 'u': 90 | //itoa (buf, c, *((int *) arg++)); 91 | itoa (buf, c, __builtin_va_arg(vl, int)); 92 | p = buf; 93 | goto string; 94 | break; 95 | 96 | case 's': 97 | //p = *arg++; 98 | p = __builtin_va_arg(vl, char*); 99 | if (! p) 100 | p = "(null)"; 101 | 102 | string: 103 | while (*p) 104 | buffer[buffer_index++] = (*p++); 105 | break; 106 | 107 | default: 108 | //buffer[buffer_index++] = (*((int *) arg++)); 109 | buffer[buffer_index++] = __builtin_va_arg(vl, int); 110 | break; 111 | } 112 | } 113 | } 114 | 115 | buffer[buffer_index] = '\0'; 116 | 117 | if (g_log_serial) 118 | { 119 | serial_printf(buffer); 120 | } 121 | 122 | if (g_file) 123 | { 124 | fs_write(g_file, strlen(buffer), (uint8_t*)buffer); 125 | } 126 | 127 | __builtin_va_end(vl); 128 | } 129 | -------------------------------------------------------------------------------- /kernel/rootfs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "rootfs.h" 12 | #include "alloc.h" 13 | 14 | static BOOL rootfs_open(File *node, uint32_t flags); 15 | static void rootfs_close(File *file); 16 | static FileSystemNode *rootfs_finddir(FileSystemNode *node, char *name); 17 | static struct FileSystemDirent *rootfs_readdir(FileSystemNode *node, uint32_t index); 18 | static BOOL rootfs_mkdir(FileSystemNode *node, const char *name, uint32_t flags); 19 | 20 | FileSystemNode* rootfs_initialize() 21 | { 22 | FileSystemNode* root = (FileSystemNode*)kmalloc(sizeof(FileSystemNode)); 23 | memset((uint8_t*)root, 0, sizeof(FileSystemNode)); 24 | root->node_type = FT_DIRECTORY; 25 | root->open = rootfs_open; 26 | root->close = rootfs_close; 27 | root->readdir = rootfs_readdir; 28 | root->finddir = rootfs_finddir; 29 | root->mkdir = rootfs_mkdir; 30 | 31 | return root; 32 | } 33 | 34 | static FileSystemDirent g_dirent; 35 | 36 | static BOOL rootfs_open(File *node, uint32_t flags) 37 | { 38 | return TRUE; 39 | } 40 | 41 | static void rootfs_close(File *file) 42 | { 43 | 44 | } 45 | 46 | static struct FileSystemDirent *rootfs_readdir(FileSystemNode *node, uint32_t index) 47 | { 48 | FileSystemNode *n = node->first_child; 49 | uint32_t i = 0; 50 | while (NULL != n) 51 | { 52 | if (index == i) 53 | { 54 | g_dirent.file_type = n->node_type; 55 | g_dirent.inode = n->inode; 56 | strcpy(g_dirent.name, n->name); 57 | 58 | return &g_dirent; 59 | } 60 | n = n->next_sibling; 61 | ++i; 62 | } 63 | 64 | return NULL; 65 | } 66 | 67 | static FileSystemNode *rootfs_finddir(FileSystemNode *node, char *name) 68 | { 69 | FileSystemNode *n = node->first_child; 70 | while (NULL != n) 71 | { 72 | if (strcmp(name, n->name) == 0) 73 | { 74 | return n; 75 | } 76 | n = n->next_sibling; 77 | } 78 | 79 | return NULL; 80 | } 81 | 82 | static BOOL rootfs_mkdir(FileSystemNode *node, const char *name, uint32_t flags) 83 | { 84 | FileSystemNode *n = node->first_child; 85 | while (NULL != n) 86 | { 87 | if (strcmp(name, n->name) == 0) 88 | { 89 | return FALSE; 90 | } 91 | n = n->next_sibling; 92 | } 93 | 94 | FileSystemNode* new_node = (FileSystemNode*)kmalloc(sizeof(FileSystemNode)); 95 | memset((uint8_t*)new_node, 0, sizeof(FileSystemNode)); 96 | strcpy(new_node->name, name); 97 | new_node->node_type = FT_DIRECTORY; 98 | new_node->open = rootfs_open; 99 | new_node->close = rootfs_close; 100 | new_node->readdir = rootfs_readdir; 101 | new_node->finddir = rootfs_finddir; 102 | new_node->mkdir = rootfs_mkdir; 103 | new_node->parent = node; 104 | 105 | if (node->first_child == NULL) 106 | { 107 | node->first_child = new_node; 108 | } 109 | else 110 | { 111 | FileSystemNode *n = node->first_child; 112 | while (NULL != n->next_sibling) 113 | { 114 | n = n->next_sibling; 115 | } 116 | n->next_sibling = new_node; 117 | } 118 | 119 | return TRUE; 120 | } 121 | 122 | -------------------------------------------------------------------------------- /kernel/interrupt.asm: -------------------------------------------------------------------------------- 1 | ; NASM macro 2 | %macro ISR_ERROR_CODE 1 3 | global isr%1 4 | isr%1: 5 | push dword %1 ; push the interrupt number 6 | jmp handle_isr_common 7 | %endmacro 8 | 9 | ; NASM macro 10 | %macro ISR_NO_ERROR_CODE 1 11 | global isr%1 12 | isr%1: 13 | push dword 0 ; push a dummy error code just because to keep struct Registers the same as the above macro 14 | push dword %1 ; push the interrupt number 15 | jmp handle_isr_common 16 | %endmacro 17 | 18 | 19 | ; NASM macro 20 | %macro IRQ 2 21 | global irq%1 22 | irq%1: 23 | push dword 0 ; push a dummy error code just because to keep struct Registers the same again 24 | push dword %2 25 | jmp handle_irq_common 26 | %endmacro 27 | 28 | ISR_NO_ERROR_CODE 0 29 | ISR_NO_ERROR_CODE 1 30 | ISR_NO_ERROR_CODE 2 31 | ISR_NO_ERROR_CODE 3 32 | ISR_NO_ERROR_CODE 4 33 | ISR_NO_ERROR_CODE 5 34 | ISR_NO_ERROR_CODE 6 35 | ISR_NO_ERROR_CODE 7 36 | ISR_ERROR_CODE 8 37 | ISR_NO_ERROR_CODE 9 38 | ISR_ERROR_CODE 10 39 | ISR_ERROR_CODE 11 40 | ISR_ERROR_CODE 12 41 | ISR_ERROR_CODE 13 42 | ISR_ERROR_CODE 14 43 | ISR_NO_ERROR_CODE 15 44 | ISR_NO_ERROR_CODE 16 45 | ISR_NO_ERROR_CODE 17 46 | ISR_NO_ERROR_CODE 18 47 | ISR_NO_ERROR_CODE 19 48 | ISR_NO_ERROR_CODE 20 49 | ISR_NO_ERROR_CODE 21 50 | ISR_NO_ERROR_CODE 22 51 | ISR_NO_ERROR_CODE 23 52 | ISR_NO_ERROR_CODE 24 53 | ISR_NO_ERROR_CODE 25 54 | ISR_NO_ERROR_CODE 26 55 | ISR_NO_ERROR_CODE 27 56 | ISR_NO_ERROR_CODE 28 57 | ISR_NO_ERROR_CODE 29 58 | ISR_NO_ERROR_CODE 30 59 | ISR_NO_ERROR_CODE 31 60 | ISR_NO_ERROR_CODE 128 61 | 62 | ; IRQ0 is handled by irq_timer below 63 | 64 | IRQ 1, 33 65 | IRQ 2, 34 66 | IRQ 3, 35 67 | IRQ 4, 36 68 | IRQ 5, 37 69 | IRQ 6, 38 70 | IRQ 7, 39 71 | IRQ 8, 40 72 | IRQ 9, 41 73 | IRQ 10, 42 74 | IRQ 11, 43 75 | IRQ 12, 44 76 | IRQ 13, 45 77 | IRQ 14, 46 78 | IRQ 15, 47 79 | 80 | %macro SAVE_REGS 0 81 | pushad 82 | push ds ;those registers are 16 bit but they are pushed as 32 bits here 83 | push es 84 | push fs 85 | push gs 86 | 87 | push ebx 88 | mov bx, 0x10 ; load the kernel data segment descriptor 89 | mov ds, bx 90 | mov es, bx 91 | mov fs, bx 92 | mov gs, bx 93 | pop ebx 94 | %endmacro 95 | 96 | %macro RESTORE_REGS 0 97 | pop gs 98 | pop fs 99 | pop es 100 | pop ds 101 | popad 102 | %endmacro 103 | 104 | 105 | extern handle_isr 106 | 107 | handle_isr_common: 108 | SAVE_REGS 109 | call handle_isr 110 | RESTORE_REGS 111 | add esp, 8 ; deallocate the error code and the interrupt number 112 | iret ; pops CS, EIP, EFLAGS and also SS, and ESP if privilege change occurs 113 | 114 | extern handle_irq 115 | 116 | handle_irq_common: 117 | SAVE_REGS 118 | call handle_irq 119 | RESTORE_REGS 120 | add esp, 8 ; deallocate the error code and the interrupt number 121 | iret ; pops CS, EIP, EFLAGS and also SS, and ESP if privilege change occurs 122 | 123 | extern handle_timer_irq 124 | global irq_timer 125 | irq_timer: ; this does not have int no and error code in the stack, so there is no "add esp, 8" 126 | SAVE_REGS 127 | call handle_timer_irq 128 | mov al,0x20 129 | out 0x20,al 130 | RESTORE_REGS 131 | iret 132 | 133 | -------------------------------------------------------------------------------- /kernel/descriptortables.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef DESCRIPTORTABLES_H 12 | #define DESCRIPTORTABLES_H 13 | 14 | #include "common.h" 15 | 16 | #define TLS_SELECTOR 0x30 // GDT index 6, RPL 0 17 | #define TLS_ENTRY_IDX 6 18 | 19 | void descriptor_tables_initialize(); 20 | void set_gdt_entry(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran); 21 | 22 | struct GdtEntry 23 | { 24 | uint16_t limit_low; 25 | uint16_t base_low; 26 | uint8_t base_middle; 27 | uint8_t access; 28 | uint8_t granularity; 29 | uint8_t base_high; 30 | } __attribute__((packed)); 31 | 32 | typedef struct GdtEntry GdtEntry; 33 | 34 | 35 | struct GdtPointer 36 | { 37 | uint16_t limit; 38 | uint32_t base; 39 | } __attribute__((packed)); 40 | 41 | typedef struct GdtPointer GdtPointer; 42 | 43 | 44 | struct IdtEntry 45 | { 46 | uint16_t base_lo; 47 | uint16_t sel; 48 | uint8_t always0; 49 | uint8_t flags; 50 | uint16_t base_hi; 51 | } __attribute__((packed)); 52 | 53 | typedef struct IdtEntry IdtEntry; 54 | 55 | 56 | struct IdtPointer 57 | { 58 | uint16_t limit; 59 | uint32_t base; 60 | } __attribute__((packed)); 61 | 62 | typedef struct IdtPointer IdtPointer; 63 | 64 | struct Tss { 65 | uint16_t previous_task, __previous_task_unused; 66 | uint32_t esp0; 67 | uint16_t ss0, __ss0_unused; 68 | uint32_t esp1; 69 | uint16_t ss1, __ss1_unused; 70 | uint32_t esp2; 71 | uint16_t ss2, __ss2_unused; 72 | uint32_t cr3; 73 | uint32_t eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi; 74 | uint16_t es, __es_unused; 75 | uint16_t cs, __cs_unused; 76 | uint16_t ss, __ss_unused; 77 | uint16_t ds, __ds_unused; 78 | uint16_t fs, __fs_unused; 79 | uint16_t gs, __gs_unused; 80 | uint16_t ldt_selector, __ldt_sel_unused; 81 | uint16_t debug_flag, io_map; 82 | } __attribute__ ((packed)); 83 | 84 | typedef struct Tss Tss; 85 | 86 | 87 | extern void isr0 (); 88 | extern void isr1 (); 89 | extern void isr2 (); 90 | extern void isr3 (); 91 | extern void isr4 (); 92 | extern void isr5 (); 93 | extern void isr6 (); 94 | extern void isr7 (); 95 | extern void isr8 (); 96 | extern void isr9 (); 97 | extern void isr10(); 98 | extern void isr11(); 99 | extern void isr12(); 100 | extern void isr13(); 101 | extern void isr14(); 102 | extern void isr15(); 103 | extern void isr16(); 104 | extern void isr17(); 105 | extern void isr18(); 106 | extern void isr19(); 107 | extern void isr20(); 108 | extern void isr21(); 109 | extern void isr22(); 110 | extern void isr23(); 111 | extern void isr24(); 112 | extern void isr25(); 113 | extern void isr26(); 114 | extern void isr27(); 115 | extern void isr28(); 116 | extern void isr29(); 117 | extern void isr30(); 118 | extern void isr31(); 119 | extern void irq0 (); 120 | extern void irq1 (); 121 | extern void irq2 (); 122 | extern void irq3 (); 123 | extern void irq4 (); 124 | extern void irq5 (); 125 | extern void irq6 (); 126 | extern void irq7 (); 127 | extern void irq8 (); 128 | extern void irq9 (); 129 | extern void irq10(); 130 | extern void irq11(); 131 | extern void irq12(); 132 | extern void irq13(); 133 | extern void irq14(); 134 | extern void irq15(); 135 | extern void isr128(); 136 | 137 | #endif //DESCRIPTORTABLES_H 138 | -------------------------------------------------------------------------------- /kernel/hashtable.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "hashtable.h" 12 | #include "alloc.h" 13 | 14 | typedef struct DataItem 15 | { 16 | uint32_t data; 17 | uint32_t key; 18 | uint8_t used; 19 | } DataItem; 20 | 21 | typedef struct HashTable 22 | { 23 | DataItem* items; 24 | uint32_t capacity; 25 | } HashTable; 26 | 27 | static uint32_t hash_code(HashTable* hashtable, uint32_t key) 28 | { 29 | return key % hashtable->capacity; 30 | } 31 | 32 | HashTable* hashtable_create(uint32_t capacity) 33 | { 34 | HashTable* hashtable = kmalloc(sizeof(HashTable)); 35 | memset((uint8_t*)hashtable, 0, sizeof(HashTable)); 36 | hashtable->capacity = capacity; 37 | hashtable->items = kmalloc(sizeof(DataItem) * capacity); 38 | 39 | return hashtable; 40 | } 41 | 42 | void hashtable_destroy(HashTable* hashtable) 43 | { 44 | kfree(hashtable->items); 45 | kfree(hashtable); 46 | } 47 | 48 | static DataItem* hashtable_search_internal(HashTable* hashtable, uint32_t key) 49 | { 50 | //get the hash 51 | uint32_t hash_index = hash_code(hashtable, key); 52 | 53 | uint32_t counter = 0; 54 | while(counter < hashtable->capacity) 55 | { 56 | if(hashtable->items[hash_index].key == key) 57 | { 58 | if(hashtable->items[hash_index].used == TRUE) 59 | { 60 | return &(hashtable->items[hash_index]); 61 | } 62 | } 63 | 64 | //go to next cell 65 | ++hash_index; 66 | 67 | //wrap around the table 68 | hash_index %= hashtable->capacity; 69 | 70 | ++counter; 71 | } 72 | 73 | return NULL; 74 | } 75 | 76 | BOOL hashtable_search(HashTable* hashtable, uint32_t key, uint32_t* value) 77 | { 78 | DataItem* existing = hashtable_search_internal(hashtable, key); 79 | 80 | if (existing) 81 | { 82 | *value = existing->data; 83 | 84 | return TRUE; 85 | } 86 | 87 | return FALSE; 88 | } 89 | 90 | BOOL hashtable_insert(HashTable* hashtable, uint32_t key, uint32_t data) 91 | { 92 | DataItem* existing = hashtable_search_internal(hashtable, key); 93 | 94 | if (existing) 95 | { 96 | existing->data = data; 97 | 98 | return TRUE; 99 | } 100 | 101 | //get the hash 102 | uint32_t hash_index = hash_code(hashtable, key); 103 | 104 | uint32_t counter = 0; 105 | //move in array until an empty or deleted cell 106 | while(counter < hashtable->capacity) 107 | { 108 | if (hashtable->items[hash_index].used == FALSE) 109 | { 110 | hashtable->items[hash_index].key = key; 111 | hashtable->items[hash_index].data = data; 112 | hashtable->items[hash_index].used = TRUE; 113 | 114 | return TRUE; 115 | } 116 | 117 | 118 | //go to next cell 119 | ++hash_index; 120 | 121 | //wrap around the table 122 | hash_index %= hashtable->capacity; 123 | 124 | ++counter; 125 | } 126 | 127 | return FALSE; 128 | } 129 | 130 | BOOL hashtable_remove(HashTable* hashtable, uint32_t key) 131 | { 132 | DataItem* existing = hashtable_search_internal(hashtable, key); 133 | 134 | if (existing) 135 | { 136 | existing->used = FALSE; 137 | 138 | return TRUE; 139 | } 140 | 141 | return FALSE; 142 | } 143 | -------------------------------------------------------------------------------- /kernel/syscall_getthreads.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "process.h" 12 | #include "common.h" 13 | #include "errno.h" 14 | #include "syscall_getthreads.h" 15 | 16 | 17 | int32_t syscall_getthreads(ThreadInfo* threads, uint32_t max_count, uint32_t flags) 18 | { 19 | if (!check_user_access(threads)) 20 | { 21 | return -EFAULT; 22 | } 23 | 24 | //interrupts are disabled 25 | 26 | memset((uint8_t*)threads, 0, sizeof(ThreadInfo) * max_count); 27 | 28 | ThreadInfo* info = threads; 29 | 30 | Thread* t = thread_get_first(); 31 | uint32_t i = 0; 32 | while (t && i < max_count) 33 | { 34 | info->thread_id = t->threadId; 35 | info->process_id = t->owner->pid; 36 | info->state = t->state; 37 | info->user_mode = t->user_mode; 38 | info->birth_time = t->birth_time; 39 | info->context_switch_count = t->context_switch_count; 40 | info->context_start_time = t->context_start_time; 41 | info->context_end_time = t->context_end_time; 42 | info->consumed_cpu_time_ms = t->consumed_cpu_time_ms; 43 | info->usage_cpu = t->usage_cpu; 44 | info->called_syscall_count = t->called_syscall_count; 45 | 46 | t = t->next; 47 | i++; 48 | info++; 49 | } 50 | 51 | return i; 52 | } 53 | 54 | 55 | static BOOL exists_in_info_list(uint32_t pid, ProcInfo* procs, uint32_t count) 56 | { 57 | ProcInfo* info = procs; 58 | 59 | uint32_t i = 0; 60 | 61 | while (i < count) 62 | { 63 | if (info->process_id == pid) 64 | { 65 | return TRUE; 66 | } 67 | i++; 68 | info++; 69 | } 70 | 71 | return FALSE; 72 | } 73 | 74 | int32_t syscall_getprocs(ProcInfo* procs, uint32_t max_count, uint32_t flags) 75 | { 76 | if (!check_user_access(procs)) 77 | { 78 | return -EFAULT; 79 | } 80 | 81 | //interrupts are disabled 82 | 83 | memset((uint8_t*)procs, 0, sizeof(ProcInfo) * max_count); 84 | 85 | ProcInfo* info = procs; 86 | 87 | Thread* t = thread_get_first(); 88 | uint32_t i = 0; 89 | uint32_t process_count = 0; 90 | while (t && i < max_count) 91 | { 92 | Process* process = t->owner; 93 | 94 | if (!exists_in_info_list(process->pid, procs, process_count)) 95 | { 96 | info->process_id = process->pid; 97 | info->process_gid = process->pgid; 98 | info->parent_process_id = -1; 99 | if (process->parent) 100 | { 101 | info->parent_process_id = process->parent->pid; 102 | } 103 | 104 | for (uint32_t k = 0; k < SOSO_MAX_OPENED_FILES; ++k) 105 | { 106 | File* file = process->fd[k]; 107 | if (file && file->node) 108 | { 109 | info->fd[k] = file->node->node_type; 110 | } 111 | } 112 | 113 | memcpy((uint8_t*)info->name, (const uint8_t*)process->name, SOSO_PROCESS_NAME_MAX); 114 | 115 | //TODO: tty, working_directory 116 | 117 | info++; 118 | process_count++; 119 | } 120 | 121 | 122 | t = t->next; 123 | i++; 124 | } 125 | 126 | return process_count; 127 | } 128 | -------------------------------------------------------------------------------- /kernel/boot.asm: -------------------------------------------------------------------------------- 1 | [BITS 32] 2 | 3 | global _start 4 | global kernel_page_directory 5 | extern kmain 6 | 7 | MBOOT_PAGE_ALIGN equ 1<<0 8 | MBOOT_MEM_INFO equ 1<<1 9 | MBOOT_USE_GFX equ 1<<2 10 | MBOOT_HEADER_MAGIC equ 0x1BADB002 11 | MBOOT_HEADER_FLAGS equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO | MBOOT_USE_GFX 12 | MBOOT_CHECKSUM equ -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS) 13 | 14 | ; 3GB offset for translating physical to virtual addresses 15 | KERNEL_VIRTUAL_BASE equ 0xC0000000 16 | ; Page directory idx of kernel's 4MB PTE 17 | KERNEL_PAGE_NUMBER equ (KERNEL_VIRTUAL_BASE >> 22) 18 | 19 | STACK_SIZE equ 0x1000 ; 4KB stack 20 | 21 | section .data 22 | align 0x1000 23 | ; This PDE identity-maps the first 4MB of 32-bit physical address space 24 | ; bit 7: PS - kernel page is 4MB 25 | ; bit 1: RW - kernel page is R/W 26 | ; bit 0: P - kernel page is present 27 | kernel_page_directory: 28 | ; This page directory entry identity-maps the first 4MB of the 32-bit physical address space. 29 | ; All bits are clear except the following (0x00000083): 30 | ; bit 7: PS The kernel page is 4MB. 31 | ; bit 1: RW The kernel page is read/write. 32 | ; bit 0: P The kernel page is present. 33 | ; This entry must be here -- otherwise the kernel will crash immediately after paging is 34 | ; enabled because it can't fetch the next instruction! It's ok to unmap this page later. 35 | dd 0x00000083 36 | times (KERNEL_PAGE_NUMBER - 1) dd 0 ; Pages before kernel space. 37 | ; This page directory entry defines a 4MB page containing the kernel. 38 | dd 0x00000083 39 | times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0 ; Pages after the kernel image. 40 | 41 | 42 | section .text 43 | align 4 44 | MultiBootHeader: 45 | dd MBOOT_HEADER_MAGIC 46 | dd MBOOT_HEADER_FLAGS 47 | dd MBOOT_CHECKSUM 48 | dd 0x00000000 ; header_addr 49 | dd 0x00000000 ; load_addr 50 | dd 0x00000000 ; load_end_addr 51 | dd 0x00000000 ; bss_end_addr 52 | dd 0x00000000 ; entry_addr 53 | ; Graphics requests 54 | dd 0x00000000 ; 0 = linear graphics 55 | dd 1024 56 | dd 768 57 | dd 32 58 | 59 | ; reserve initial kernel stack space -- that's 16k. 60 | STACKSIZE equ 0x4000 61 | 62 | _start: 63 | mov ecx, (kernel_page_directory - KERNEL_VIRTUAL_BASE) 64 | mov cr3, ecx ; Load page directory 65 | 66 | mov ecx, cr4 67 | or ecx, 0x00000010 ; Set PSE bit in CR4 to enable 4MB pages 68 | mov cr4, ecx 69 | 70 | mov ecx, cr0 71 | or ecx, 0x80000000 ; Set PG bit in CR0 to enable paging 72 | mov cr0, ecx 73 | 74 | ; EIP currently holds physical address, so we need a long jump to 75 | ; the correct virtual address to continue execution in kernel space 76 | lea ecx, [start_higher_half] 77 | jmp ecx ; Absolute jump!! 78 | 79 | start_higher_half: 80 | ; Unmap identity-mapped first 4MB of physical address space 81 | ; mov dword [kernel_page_directory], 0 82 | ; invlpg [0] 83 | 84 | mov esp, kernel_stack_top ; set up stack pointer 85 | push eax ; push header magic 86 | add ebx, KERNEL_VIRTUAL_BASE ; make multiboot header pointer virtual 87 | push ebx ; push header pointer (TODO: hopefully this isn't at an addr > 4MB) 88 | cli ; disable interrupts 89 | call kmain 90 | 91 | cli 92 | .hang: 93 | hlt 94 | jmp .hang 95 | 96 | section .bss 97 | global kernel_stack_bottom 98 | global kernel_stack_top 99 | 100 | kernel_stack_bottom: 101 | resb STACK_SIZE ; reserve 4KB for kernel stack 102 | kernel_stack_top: -------------------------------------------------------------------------------- /TinyGL/src/image_util.c: -------------------------------------------------------------------------------- 1 | #include "zgl.h" 2 | 3 | /* 4 | * image conversion 5 | */ 6 | 7 | void gl_convertRGB_to_5R6G5B(unsigned short *pixmap,unsigned char *rgb, 8 | int xsize,int ysize) 9 | { 10 | int i,n; 11 | unsigned char *p; 12 | 13 | p=rgb; 14 | n=xsize*ysize; 15 | for(i=0;i>3); 17 | p+=3; 18 | } 19 | } 20 | 21 | void gl_convertRGB_to_8A8R8G8B(unsigned int *pixmap, unsigned char *rgb, 22 | int xsize, int ysize) 23 | { 24 | int i,n; 25 | unsigned char *p; 26 | 27 | p=rgb; 28 | n=xsize*ysize; 29 | for(i=0;i> INTERP_NORM_BITS); 47 | } 48 | 49 | 50 | /* 51 | * TODO: more accurate resampling 52 | */ 53 | 54 | void gl_resizeImage(unsigned char *dest,int xsize_dest,int ysize_dest, 55 | unsigned char *src,int xsize_src,int ysize_src) 56 | { 57 | unsigned char *pix,*pix_src; 58 | float x1,y1,x1inc,y1inc; 59 | int xi,yi,j,xf,yf,x,y; 60 | 61 | pix=dest; 62 | pix_src=src; 63 | 64 | x1inc=(float) (xsize_src - 1) / (float) (xsize_dest - 1); 65 | y1inc=(float) (ysize_src - 1) / (float) (ysize_dest - 1); 66 | 67 | y1=0; 68 | for(y=0;y> FRAC_BITS; 123 | yi=y1 >> FRAC_BITS; 124 | pix1=pix_src+(yi*xsize_src+xi)*3; 125 | 126 | pix[0]=pix1[0]; 127 | pix[1]=pix1[1]; 128 | pix[2]=pix1[2]; 129 | 130 | pix+=3; 131 | x1+=x1inc; 132 | } 133 | y1+=y1inc; 134 | } 135 | } 136 | 137 | -------------------------------------------------------------------------------- /TinyGL/src/nglx.c: -------------------------------------------------------------------------------- 1 | /* simple glx like driver for TinyGL and Nano X */ 2 | #include 3 | #include 4 | #include 5 | #include "zgl.h" 6 | 7 | typedef struct { 8 | GLContext *gl_context; 9 | int xsize,ysize; 10 | GR_DRAW_ID drawable; 11 | GR_GC_ID gc; 12 | int pixtype; /* pixel type in TinyGL */ 13 | } TinyNGLXContext; 14 | 15 | NGLXContext nglXCreateContext(NGLXContext shareList, int flags) 16 | { 17 | TinyNGLXContext *ctx; 18 | 19 | if (shareList != NULL) { 20 | gl_fatal_error("No sharing available in TinyGL"); 21 | } 22 | ctx=gl_malloc(sizeof(TinyNGLXContext)); 23 | if (!ctx) 24 | return NULL; 25 | ctx->gl_context=NULL; 26 | return (NGLXContext) ctx; 27 | } 28 | 29 | void glXDestroyContext( NGLXContext ctx1 ) 30 | { 31 | TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1; 32 | if (ctx->gl_context != NULL) { 33 | glClose(); 34 | } 35 | gl_free(ctx); 36 | } 37 | 38 | 39 | /* resize the glx viewport : we try to use the xsize and ysize 40 | given. We return the effective size which is guaranted to be smaller */ 41 | 42 | static int glX_resize_viewport(GLContext *c,int *xsize_ptr,int *ysize_ptr) 43 | { 44 | TinyNGLXContext *ctx; 45 | int xsize,ysize; 46 | 47 | ctx=(TinyNGLXContext *)c->opaque; 48 | 49 | xsize=*xsize_ptr; 50 | ysize=*ysize_ptr; 51 | 52 | /* we ensure that xsize and ysize are multiples of 2 for the zbuffer. 53 | TODO: find a better solution */ 54 | xsize&=~3; 55 | ysize&=~3; 56 | 57 | if (xsize == 0 || ysize == 0) return -1; 58 | 59 | *xsize_ptr=xsize; 60 | *ysize_ptr=ysize; 61 | 62 | ctx->xsize=xsize; 63 | ctx->ysize=ysize; 64 | 65 | /* resize the Z buffer */ 66 | ZB_resize(c->zb,NULL,xsize,ysize); 67 | return 0; 68 | } 69 | 70 | /* we assume here that drawable is a window */ 71 | int nglXMakeCurrent( NGLXDrawable drawable, 72 | NGLXContext ctx1) 73 | { 74 | TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1; 75 | int mode, xsize, ysize; 76 | ZBuffer *zb; 77 | GR_WINDOW_INFO win_info; 78 | 79 | if (ctx->gl_context == NULL) { 80 | /* create the TinyGL context */ 81 | GrGetWindowInfo(drawable, &win_info); 82 | 83 | xsize = win_info.width; 84 | ysize = win_info.height; 85 | 86 | /* currently, we only support 16 bit rendering */ 87 | mode = ZB_MODE_5R6G5B; 88 | zb=ZB_open(xsize,ysize,mode,0,NULL,NULL,NULL); 89 | if (zb == NULL) { 90 | fprintf(stderr, "Error while initializing Z buffer\n"); 91 | exit(1); 92 | } 93 | 94 | ctx->pixtype = MWPF_TRUECOLOR565; 95 | 96 | /* create a gc */ 97 | ctx->gc = GrNewGC(); 98 | 99 | /* initialisation of the TinyGL interpreter */ 100 | glInit(zb); 101 | ctx->gl_context=gl_get_context(); 102 | ctx->gl_context->opaque=(void *) ctx; 103 | ctx->gl_context->gl_resize_viewport=glX_resize_viewport; 104 | 105 | /* set the viewport : we force a call to glX_resize_viewport */ 106 | ctx->gl_context->viewport.xsize=-1; 107 | ctx->gl_context->viewport.ysize=-1; 108 | 109 | glViewport(0, 0, xsize, ysize); 110 | } 111 | 112 | return 1; 113 | } 114 | 115 | void nglXSwapBuffers( NGLXDrawable drawable ) 116 | { 117 | GLContext *gl_context; 118 | TinyNGLXContext *ctx; 119 | 120 | /* retrieve the current NGLXContext */ 121 | gl_context=gl_get_context(); 122 | ctx=(TinyNGLXContext *)gl_context->opaque; 123 | 124 | GrArea(drawable, ctx->gc, 0, 0, ctx->xsize, 125 | ctx->ysize, ctx->gl_context->zb->pbuf, ctx->pixtype); 126 | } 127 | 128 | 129 | -------------------------------------------------------------------------------- /TinyGL/src/misc.c: -------------------------------------------------------------------------------- 1 | #include "zgl.h" 2 | #include "msghandling.h" 3 | 4 | void glopViewport(GLContext *c,GLParam *p) 5 | { 6 | int xsize,ysize,xmin,ymin,xsize_req,ysize_req; 7 | 8 | xmin=p[1].i; 9 | ymin=p[2].i; 10 | xsize=p[3].i; 11 | ysize=p[4].i; 12 | 13 | /* we may need to resize the zbuffer */ 14 | 15 | if (c->viewport.xmin != xmin || 16 | c->viewport.ymin != ymin || 17 | c->viewport.xsize != xsize || 18 | c->viewport.ysize != ysize) { 19 | 20 | xsize_req=xmin+xsize; 21 | ysize_req=ymin+ysize; 22 | 23 | if (c->gl_resize_viewport && 24 | c->gl_resize_viewport(c,&xsize_req,&ysize_req) != 0) { 25 | gl_fatal_error("glViewport: error while resizing display"); 26 | } 27 | 28 | xsize=xsize_req-xmin; 29 | ysize=ysize_req-ymin; 30 | if (xsize <= 0 || ysize <= 0) { 31 | gl_fatal_error("glViewport: size too small"); 32 | } 33 | 34 | tgl_trace("glViewport: %d %d %d %d\n", 35 | xmin, ymin, xsize, ysize); 36 | c->viewport.xmin=xmin; 37 | c->viewport.ymin=ymin; 38 | c->viewport.xsize=xsize; 39 | c->viewport.ysize=ysize; 40 | 41 | c->viewport.updated=1; 42 | } 43 | } 44 | 45 | void glopEnableDisable(GLContext *c,GLParam *p) 46 | { 47 | int code=p[1].i; 48 | int v=p[2].i; 49 | 50 | switch(code) { 51 | case GL_CULL_FACE: 52 | c->cull_face_enabled=v; 53 | break; 54 | case GL_LIGHTING: 55 | c->lighting_enabled=v; 56 | break; 57 | case GL_COLOR_MATERIAL: 58 | c->color_material_enabled=v; 59 | break; 60 | case GL_TEXTURE_2D: 61 | c->texture_2d_enabled=v; 62 | break; 63 | case GL_NORMALIZE: 64 | c->normalize_enabled=v; 65 | break; 66 | case GL_DEPTH_TEST: 67 | c->depth_test = v; 68 | break; 69 | case GL_POLYGON_OFFSET_FILL: 70 | if (v) c->offset_states |= TGL_OFFSET_FILL; 71 | else c->offset_states &= ~TGL_OFFSET_FILL; 72 | break; 73 | case GL_POLYGON_OFFSET_POINT: 74 | if (v) c->offset_states |= TGL_OFFSET_POINT; 75 | else c->offset_states &= ~TGL_OFFSET_POINT; 76 | break; 77 | case GL_POLYGON_OFFSET_LINE: 78 | if (v) c->offset_states |= TGL_OFFSET_LINE; 79 | else c->offset_states &= ~TGL_OFFSET_LINE; 80 | break; 81 | default: 82 | if (code>=GL_LIGHT0 && codecurrent_shade_model=code; 97 | } 98 | 99 | void glopCullFace(GLContext *c,GLParam *p) 100 | { 101 | int code=p[1].i; 102 | c->current_cull_face=code; 103 | } 104 | 105 | void glopFrontFace(GLContext *c,GLParam *p) 106 | { 107 | int code=p[1].i; 108 | c->current_front_face=code; 109 | } 110 | 111 | void glopPolygonMode(GLContext *c,GLParam *p) 112 | { 113 | int face=p[1].i; 114 | int mode=p[2].i; 115 | 116 | switch(face) { 117 | case GL_BACK: 118 | c->polygon_mode_back=mode; 119 | break; 120 | case GL_FRONT: 121 | c->polygon_mode_front=mode; 122 | break; 123 | case GL_FRONT_AND_BACK: 124 | c->polygon_mode_front=mode; 125 | c->polygon_mode_back=mode; 126 | break; 127 | default: 128 | assert(0); 129 | } 130 | } 131 | 132 | void glopHint(GLContext *c,GLParam *p) 133 | { 134 | #if 0 135 | int target=p[1].i; 136 | int mode=p[2].i; 137 | 138 | /* do nothing */ 139 | #endif 140 | } 141 | 142 | void 143 | glopPolygonOffset(GLContext *c, GLParam *p) 144 | { 145 | c->offset_factor = p[1].f; 146 | c->offset_units = p[2].f; 147 | } 148 | -------------------------------------------------------------------------------- /kernel/vbe.h: -------------------------------------------------------------------------------- 1 | #ifndef VBE_H 2 | #define VBE_H 3 | 4 | typedef struct { 5 | /* Mandatory information for all VBE revisions */ 6 | uint16_t ModeAttributes; /**< @brief mode attributes */ 7 | uint8_t WinAAttributes; /**< @brief window A attributes */ 8 | uint8_t WinBAttributes; /**< @brief window B attributes */ 9 | uint16_t WinGranularity; /**< @brief window granularity */ 10 | uint16_t WinSize; /**< @brief window size */ 11 | uint16_t WinASegment; /**< @brief window A start segment */ 12 | uint16_t WinBSegment; /**< @brief window B start segment */ 13 | void* WinFuncPtr; /**< @brief real mode/far pointer to window function */ 14 | uint16_t BytesPerScanLine; /**< @brief bytes per scan line */ 15 | 16 | /* Mandatory information for VBE 1.2 and above */ 17 | 18 | uint16_t XResolution; /**< @brief horizontal resolution in pixels/characters */ 19 | uint16_t YResolution; /**< @brief vertical resolution in pixels/characters */ 20 | uint8_t XCharSize; /**< @brief character cell width in pixels */ 21 | uint8_t YCharSize; /**< @brief character cell height in pixels */ 22 | uint8_t NumberOfPlanes; /**< @brief number of memory planes */ 23 | uint8_t BitsPerPixel; /**< @brief bits per pixel */ 24 | uint8_t NumberOfBanks; /**< @brief number of banks */ 25 | uint8_t MemoryModel; /**< @brief memory model type */ 26 | uint8_t BankSize; /**< @brief bank size in KB */ 27 | uint8_t NumberOfImagePages; /**< @brief number of images */ 28 | uint8_t Reserved1; /**< @brief reserved for page function */ 29 | 30 | /* Direct Color fields (required for direct/6 and YUV/7 memory models) */ 31 | 32 | uint8_t RedMaskSize; /* size of direct color red mask in bits */ 33 | uint8_t RedFieldPosition; /* bit position of lsb of red mask */ 34 | uint8_t GreenMaskSize; /* size of direct color green mask in bits */ 35 | uint8_t GreenFieldPosition; /* bit position of lsb of green mask */ 36 | uint8_t BlueMaskSize; /* size of direct color blue mask in bits */ 37 | uint8_t BlueFieldPosition; /* bit position of lsb of blue mask */ 38 | uint8_t RsvdMaskSize; /* size of direct color reserved mask in bits */ 39 | uint8_t RsvdFieldPosition; /* bit position of lsb of reserved mask */ 40 | uint8_t DirectColorModeInfo; /* direct color mode attributes */ 41 | 42 | /* Mandatory information for VBE 2.0 and above */ 43 | void* PhysBasePtr; /**< @brief physical address for flat memory frame buffer */ 44 | uint8_t Reserved2[4]; /**< @brief Reserved - always set to 0 */ 45 | uint8_t Reserved3[2]; /**< @brief Reserved - always set to 0 */ 46 | 47 | /* Mandatory information for VBE 3.0 and above */ 48 | uint16_t LinBytesPerScanLine; /* bytes per scan line for linear modes */ 49 | uint8_t BnkNumberOfImagePages; /* number of images for banked modes */ 50 | uint8_t LinNumberOfImagePages; /* number of images for linear modes */ 51 | uint8_t LinRedMaskSize; /* size of direct color red mask (linear modes) */ 52 | uint8_t LinRedFieldPosition; /* bit position of lsb of red mask (linear modes) */ 53 | uint8_t LinGreenMaskSize; /* size of direct color green mask (linear modes) */ 54 | uint8_t LinGreenFieldPosition; /* bit position of lsb of green mask (linear modes) */ 55 | uint8_t LinBlueMaskSize; /* size of direct color blue mask (linear modes) */ 56 | uint8_t LinBlueFieldPosition; /* bit position of lsb of blue mask (linear modes ) */ 57 | uint8_t LinRsvdMaskSize; /* size of direct color reserved mask (linear modes) */ 58 | uint8_t LinRsvdFieldPosition; /* bit position of lsb of reserved mask (linear modes) */ 59 | uint32_t MaxPixelClock; /* maximum pixel clock (in Hz) for graphics mode */ 60 | uint8_t Reserved4[190]; /* remainder of ModeInfoBlock */ 61 | } __attribute__((packed)) vbe_mode_info_t; 62 | 63 | #endif // VBE_H 64 | -------------------------------------------------------------------------------- /kernel/fifobuffer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "fifobuffer.h" 12 | #include "alloc.h" 13 | 14 | FifoBuffer* fifobuffer_create(uint32_t capacity) 15 | { 16 | FifoBuffer* fifo = (FifoBuffer*)kmalloc(sizeof(FifoBuffer)); 17 | memset((uint8_t*)fifo, 0, sizeof(FifoBuffer)); 18 | fifo->data = (uint8_t*)kmalloc(capacity); 19 | memset((uint8_t*)fifo->data, 0, capacity); 20 | fifo->capacity= capacity; 21 | 22 | return fifo; 23 | } 24 | 25 | void fifobuffer_destroy(FifoBuffer* fifo_buffer) 26 | { 27 | kfree(fifo_buffer->data); 28 | kfree(fifo_buffer); 29 | } 30 | 31 | void fifobuffer_clear(FifoBuffer* fifo_buffer) 32 | { 33 | fifo_buffer->used_bytes = 0; 34 | fifo_buffer->read_index = 0; 35 | fifo_buffer->write_index = 0; 36 | } 37 | 38 | BOOL fifobuffer_is_empty(FifoBuffer* fifo_buffer) 39 | { 40 | if (0 == fifo_buffer->used_bytes) 41 | { 42 | return TRUE; 43 | } 44 | 45 | return FALSE; 46 | } 47 | 48 | uint32_t fifobuffer_get_size(FifoBuffer* fifo_buffer) 49 | { 50 | return fifo_buffer->used_bytes; 51 | } 52 | 53 | uint32_t fifobuffer_get_capacity(FifoBuffer* fifo_buffer) 54 | { 55 | return fifo_buffer->capacity; 56 | } 57 | 58 | uint32_t fifobuffer_get_free(FifoBuffer* fifo_buffer) 59 | { 60 | return fifo_buffer->capacity - fifo_buffer->used_bytes; 61 | } 62 | 63 | int32_t fifobuffer_enqueue(FifoBuffer* fifo_buffer, uint8_t* data, uint32_t size) 64 | { 65 | if (size == 0) 66 | { 67 | return -1; 68 | } 69 | 70 | uint32_t bytes_available = fifo_buffer->capacity - fifo_buffer->used_bytes; 71 | 72 | uint32_t i = 0; 73 | while (fifo_buffer->used_bytes < fifo_buffer->capacity && i < size) 74 | { 75 | fifo_buffer->data[fifo_buffer->write_index] = data[i++]; 76 | fifo_buffer->used_bytes++; 77 | fifo_buffer->write_index++; 78 | fifo_buffer->write_index %= fifo_buffer->capacity; 79 | } 80 | 81 | return (int32_t)i; 82 | } 83 | 84 | int32_t fifobuffer_dequeue(FifoBuffer* fifo_buffer, uint8_t* data, uint32_t size) 85 | { 86 | if (size == 0) 87 | { 88 | return -1; 89 | } 90 | 91 | if (0 == fifo_buffer->used_bytes) 92 | { 93 | //Buffer is empty 94 | return 0; 95 | } 96 | 97 | uint32_t i = 0; 98 | while (fifo_buffer->used_bytes > 0 && i < size) 99 | { 100 | data[i++] = fifo_buffer->data[fifo_buffer->read_index]; 101 | fifo_buffer->used_bytes--; 102 | fifo_buffer->read_index++; 103 | fifo_buffer->read_index %= fifo_buffer->capacity; 104 | } 105 | 106 | return i; 107 | } 108 | 109 | int32_t fifobuffer_enqueue_from_other(FifoBuffer* fifo_buffer, FifoBuffer* other) 110 | { 111 | uint32_t otherSize = fifobuffer_get_size(other); 112 | 113 | if (otherSize == 0) 114 | { 115 | return 0; 116 | } 117 | 118 | uint32_t bytes_available = fifo_buffer->capacity - fifo_buffer->used_bytes; 119 | 120 | uint32_t i = 0; 121 | while (fifo_buffer->used_bytes < fifo_buffer->capacity && other->used_bytes > 0) 122 | { 123 | fifo_buffer->data[fifo_buffer->write_index] = other->data[other->read_index]; 124 | fifo_buffer->used_bytes++; 125 | fifo_buffer->write_index++; 126 | fifo_buffer->write_index %= fifo_buffer->capacity; 127 | 128 | other->used_bytes--; 129 | other->read_index++; 130 | other->read_index %= other->capacity; 131 | 132 | i++; 133 | } 134 | 135 | return (int32_t)i; 136 | } -------------------------------------------------------------------------------- /kernel/ramdisk.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "ramdisk.h" 12 | #include "alloc.h" 13 | #include "fs.h" 14 | #include "devfs.h" 15 | 16 | typedef struct Ramdisk 17 | { 18 | uint8_t* buffer; 19 | uint32_t size; 20 | BOOL is_preallocated; 21 | } Ramdisk; 22 | 23 | #define RAMDISK_BLOCKSIZE 512 24 | 25 | static BOOL open(File *file, uint32_t flags); 26 | static void close(File *file); 27 | static int32_t read_block(FileSystemNode* node, uint32_t block_number, uint32_t count, uint8_t* buffer); 28 | static int32_t write_block(FileSystemNode* node, uint32_t block_number, uint32_t count, uint8_t* buffer); 29 | static int32_t ioctl(File *node, int32_t request, void * argp); 30 | 31 | BOOL ramdisk_create(const char* devName, uint32_t size, uint8_t* preallocated_buffer) 32 | { 33 | Ramdisk* ramdisk = kmalloc(sizeof(Ramdisk)); 34 | ramdisk->size = size; 35 | if (preallocated_buffer) 36 | { 37 | ramdisk->buffer = preallocated_buffer; 38 | ramdisk->is_preallocated = TRUE; 39 | } 40 | else 41 | { 42 | ramdisk->buffer = kmalloc(size); 43 | ramdisk->is_preallocated = FALSE; 44 | } 45 | 46 | printkf("Creating ramdisk %s (%d bytes) at %x\n", devName, size, ramdisk->buffer); 47 | 48 | Device device; 49 | memset((uint8_t*)&device, 0, sizeof(device)); 50 | strcpy(device.name, devName); 51 | device.device_type = FT_BLOCK_DEVICE; 52 | device.open = open; 53 | device.close = close; 54 | device.read_block = read_block; 55 | device.write_block = write_block; 56 | device.ioctl = ioctl; 57 | device.private_data = ramdisk; 58 | 59 | if (devfs_register_device(&device, TRUE)) 60 | { 61 | return TRUE; 62 | } 63 | 64 | if (ramdisk->is_preallocated == FALSE) 65 | { 66 | kfree(ramdisk->buffer); 67 | } 68 | 69 | kfree(ramdisk); 70 | 71 | return FALSE; 72 | } 73 | 74 | static BOOL open(File *file, uint32_t flags) 75 | { 76 | return TRUE; 77 | } 78 | 79 | static void close(File *file) 80 | { 81 | } 82 | 83 | static int32_t read_block(FileSystemNode* node, uint32_t block_number, uint32_t count, uint8_t* buffer) 84 | { 85 | Ramdisk* ramdisk = (Ramdisk*)node->private_node_data; 86 | 87 | uint32_t location = block_number * RAMDISK_BLOCKSIZE; 88 | uint32_t size = count * RAMDISK_BLOCKSIZE; 89 | 90 | if (location + size > ramdisk->size) 91 | { 92 | return -1; 93 | } 94 | 95 | begin_critical_section(); 96 | 97 | memcpy(buffer, ramdisk->buffer + location, size); 98 | 99 | end_critical_section(); 100 | 101 | return 0; 102 | } 103 | 104 | static int32_t write_block(FileSystemNode* node, uint32_t block_number, uint32_t count, uint8_t* buffer) 105 | { 106 | Ramdisk* ramdisk = (Ramdisk*)node->private_node_data; 107 | 108 | uint32_t location = block_number * RAMDISK_BLOCKSIZE; 109 | uint32_t size = count * RAMDISK_BLOCKSIZE; 110 | 111 | if (location + size > ramdisk->size) 112 | { 113 | return -1; 114 | } 115 | 116 | begin_critical_section(); 117 | 118 | memcpy(ramdisk->buffer + location, buffer, size); 119 | 120 | end_critical_section(); 121 | 122 | return 0; 123 | } 124 | 125 | static int32_t ioctl(File *node, int32_t request, void * argp) 126 | { 127 | Ramdisk* ramdisk = (Ramdisk*)node->node->private_node_data; 128 | 129 | uint32_t* result = (uint32_t*)argp; 130 | 131 | switch (request) 132 | { 133 | case IC_GET_SECTOR_COUNT: 134 | *result = ramdisk->size / RAMDISK_BLOCKSIZE; 135 | return 0; 136 | break; 137 | case IC_GET_SECTOR_SIZE_BYTES: 138 | *result = RAMDISK_BLOCKSIZE; 139 | return 0; 140 | break; 141 | default: 142 | break; 143 | } 144 | 145 | return -1; 146 | } 147 | -------------------------------------------------------------------------------- /kernel/framebuffer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "devfs.h" 12 | #include "device.h" 13 | #include "common.h" 14 | #include "gfx.h" 15 | #include "framebuffer.h" 16 | #include "vmm.h" 17 | #include "process.h" 18 | #include "alloc.h" 19 | 20 | static BOOL fb_open(File *file, uint32_t flags); 21 | static int32_t fb_read(File *file, uint32_t size, uint8_t *buffer); 22 | static int32_t fb_write(File *file, uint32_t size, uint8_t *buffer); 23 | static int32_t fb_ioctl(File *node, int32_t request, void * argp); 24 | static void* fb_mmap(File* file, uint32_t size, uint32_t offset, uint32_t flags); 25 | static BOOL fb_munmap(File* file, void* address, uint32_t size); 26 | 27 | static uint8_t* g_fb_physical = 0; 28 | static uint8_t* g_fb_virtual = 0; 29 | 30 | void framebuffer_initialize(uint8_t* p_address, uint8_t* v_address) 31 | { 32 | g_fb_physical = p_address; 33 | g_fb_virtual = v_address; 34 | 35 | Device device; 36 | memset((uint8_t*)&device, 0, sizeof(Device)); 37 | strcpy(device.name, "fb0"); 38 | device.device_type = FT_CHARACTER_DEVICE; 39 | device.open = fb_open; 40 | device.read = fb_read; 41 | device.write = fb_write; 42 | device.ioctl = fb_ioctl; 43 | device.mmap = fb_mmap; 44 | device.munmap = fb_munmap; 45 | 46 | devfs_register_device(&device, TRUE); 47 | } 48 | 49 | static BOOL fb_open(File *file, uint32_t flags) 50 | { 51 | return TRUE; 52 | } 53 | 54 | static int32_t fb_read(File *file, uint32_t size, uint8_t *buffer) 55 | { 56 | if (size == 0) 57 | { 58 | return 0; 59 | } 60 | 61 | return -1; 62 | } 63 | 64 | static int32_t fb_write(File *file, uint32_t size, uint8_t *buffer) 65 | { 66 | if (size == 0) 67 | { 68 | return 0; 69 | } 70 | 71 | int32_t requested_size = size; 72 | 73 | int32_t length = gfx_get_width() * gfx_get_height() * 4; 74 | 75 | int32_t available_size = length - file->offset; 76 | 77 | if (available_size <= 0) 78 | { 79 | return -1; 80 | } 81 | 82 | int32_t target_size = MIN(requested_size, available_size); 83 | 84 | memcpy(g_fb_virtual + file->offset, buffer, target_size); 85 | 86 | file->offset += target_size; 87 | 88 | return target_size; 89 | } 90 | 91 | static int32_t fb_ioctl(File *node, int32_t request, void * argp) 92 | { 93 | int32_t result = -1; 94 | 95 | switch (request) 96 | { 97 | case FB_GET_WIDTH: 98 | result = gfx_get_width(); 99 | break; 100 | case FB_GET_HEIGHT: 101 | result = gfx_get_height(); 102 | break; 103 | case FB_GET_BITSPERPIXEL: 104 | result = 32; 105 | break; 106 | } 107 | 108 | return result; 109 | } 110 | 111 | static void* fb_mmap(File* file, uint32_t size, uint32_t offset, uint32_t flags) 112 | { 113 | uint32_t framebuffer_size_bytes = gfx_get_width() * gfx_get_height() * gfx_get_bytes_per_pixel(); 114 | uint32_t framebuffer_page_count = PAGE_COUNT(framebuffer_size_bytes); 115 | 116 | uint32_t page_count = PAGE_COUNT(size); 117 | 118 | if (page_count > framebuffer_page_count) 119 | { 120 | return NULL; 121 | } 122 | uint32_t* physical_pages_array = (uint32_t*)kmalloc(page_count * sizeof(uint32_t)); 123 | 124 | 125 | //printkf("FB mmap: size:%d page count:%d\n", size, page_count); 126 | //printkf("FB mmap: start:%x end:%x\n", USER_MMAP_START, USER_MMAP_START + page_count * PAGESIZE_4K); 127 | 128 | for (uint32_t i = 0; i < page_count; ++i) 129 | { 130 | physical_pages_array[i] = (uint32_t)(g_fb_physical) + i * PAGESIZE_4K; 131 | } 132 | 133 | void* result = vmm_map_memory(file->thread->owner, USER_MMAP_START, physical_pages_array, page_count, FALSE); 134 | 135 | kfree(physical_pages_array); 136 | 137 | return result; 138 | } 139 | 140 | static BOOL fb_munmap(File* file, void* address, uint32_t size) 141 | { 142 | return vmm_unmap_memory(file->thread->owner, (uint32_t)address, PAGE_COUNT(size)); 143 | } 144 | -------------------------------------------------------------------------------- /TinyGL/src/zdither.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Highly optimised dithering 16 bits -> 8 bits. 3 | * The formulas were taken in Mesa (Bob Mercier mercier@hollywood.cinenet.net). 4 | */ 5 | 6 | #include 7 | #include 8 | #include "zbuffer.h" 9 | #include 10 | 11 | #if defined(TGL_FEATURE_8_BITS) 12 | 13 | #define _R 5 14 | #define _G 9 15 | #define _B 5 16 | #define _DX 4 17 | #define _DY 4 18 | #define _D (_DX*_DY) 19 | #define _MIX(r,g,b) ( ((g)<<6) | ((b)<<3) | (r) ) 20 | 21 | #define DITHER_TABLE_SIZE (1 << 15) 22 | 23 | #define DITHER_INDEX(r,g,b) ((b) + (g) * _B + (r) * (_B * _G)) 24 | 25 | #define MAXC 256 26 | static int kernel8[_DY*_DX] = { 27 | 0 * MAXC, 8 * MAXC, 2 * MAXC, 10 * MAXC, 28 | 12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC, 29 | 3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC, 30 | 15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC, 31 | }; 32 | 33 | /* we build the color table and the lookup table */ 34 | 35 | void ZB_initDither(ZBuffer *zb,int nb_colors, 36 | unsigned char *color_indexes,int *color_table) 37 | { 38 | int c,r,g,b,i,index,r1,g1,b1; 39 | 40 | if (nb_colors < (_R * _G * _B)) { 41 | fprintf(stderr,"zdither: not enough colors\n"); 42 | exit(1); 43 | } 44 | 45 | for(i=0;inb_colors=nb_colors; 48 | zb->ctable=gl_malloc(nb_colors * sizeof(int)); 49 | 50 | for (r = 0; r < _R; r++) { 51 | for (g = 0; g < _G; g++) { 52 | for (b = 0; b < _B; b++) { 53 | r1=(r*255) / (_R - 1); 54 | g1=(g*255) / (_G - 1); 55 | b1=(b*255) / (_B - 1); 56 | index=DITHER_INDEX(r,g,b); 57 | c=(r1 << 16) | (g1 << 8) | b1; 58 | zb->ctable[index]=c; 59 | color_table[index]=c; 60 | } 61 | } 62 | } 63 | 64 | zb->dctable=gl_malloc( DITHER_TABLE_SIZE ); 65 | 66 | for(i=0;i> 12) & 0x7; 68 | g=(i >> 8) & 0xF; 69 | b=(i >> 3) & 0x7; 70 | index=DITHER_INDEX(r,g,b); 71 | zb->dctable[i]=color_indexes[index]; 72 | } 73 | } 74 | 75 | void ZB_closeDither(ZBuffer *zb) 76 | { 77 | gl_free(zb->ctable); 78 | gl_free(zb->dctable); 79 | } 80 | 81 | #if 0 82 | int ZDither_lookupColor(int r,int g,int b) 83 | { 84 | unsigned char *ctable=zdither_color_table; 85 | return ctable[_MIX(_DITH0(_R, r), _DITH0(_G, g),_DITH0(_B, b))]; 86 | } 87 | #endif 88 | 89 | 90 | #define DITHER_PIXEL2(a) \ 91 | { \ 92 | register int v,t,r,g,c; \ 93 | v=*(unsigned int *)(pp+(a)); \ 94 | g=(v & 0x07DF07DF) + g_d; \ 95 | r=(((v & 0xF800F800) >> 2) + r_d) & 0x70007000; \ 96 | t=r | g; \ 97 | c=ctable[t & 0xFFFF] | (ctable[t >> 16] << 8); \ 98 | *(unsigned short *)(dest+(a))=c; \ 99 | } 100 | 101 | /* NOTE: all the memory access are 16 bit aligned, so if buf or 102 | linesize are not multiple of 2, it cannot work efficiently (or 103 | hang!) */ 104 | 105 | void ZB_ditherFrameBuffer(ZBuffer *zb,unsigned char *buf, 106 | int linesize) 107 | { 108 | int xk,yk,x,y,c1,c2; 109 | unsigned char *dest1; 110 | unsigned short *pp1; 111 | int r_d,g_d,b_d; 112 | unsigned char *ctable=zb->dctable; 113 | register unsigned char *dest; 114 | register unsigned short *pp; 115 | 116 | assert( ((long)buf & 1) == 0 && (linesize & 1) == 0); 117 | 118 | for(yk=0;yk<4;yk++) { 119 | for(xk=0;xk<4;xk+=2) { 120 | #if BYTE_ORDER == BIG_ENDIAN 121 | c1=kernel8[yk*4+xk+1]; 122 | c2=kernel8[yk*4+xk]; 123 | #else 124 | c1=kernel8[yk*4+xk]; 125 | c2=kernel8[yk*4+xk+1]; 126 | #endif 127 | r_d=((c1 << 2) & 0xF800) >> 2; 128 | g_d=(c1 >> 4) & 0x07C0; 129 | b_d=(c1 >> 9) & 0x001F; 130 | 131 | r_d|=(((c2 << 2) & 0xF800) >> 2) << 16; 132 | g_d|=((c2 >> 4) & 0x07C0) << 16; 133 | b_d|=((c2 >> 9) & 0x001F) << 16; 134 | g_d=b_d | g_d; 135 | 136 | dest1=buf + (yk * linesize) + xk; 137 | pp1=zb->pbuf + (yk * zb->xsize) + xk; 138 | 139 | for(y=yk;yysize;y+=4) { 140 | dest=dest1; 141 | pp=pp1; 142 | for(x=xk;xxsize;x+=16) { 143 | 144 | DITHER_PIXEL2(0); 145 | DITHER_PIXEL2(1*4); 146 | DITHER_PIXEL2(2*4); 147 | DITHER_PIXEL2(3*4); 148 | 149 | pp+=16; 150 | dest+=16; 151 | } 152 | dest1+=linesize*4; 153 | pp1+=zb->xsize*4; 154 | } 155 | } 156 | } 157 | } 158 | 159 | #endif 160 | -------------------------------------------------------------------------------- /kernel/syscall_select.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #include "process.h" 12 | #include "timer.h" 13 | #include "common.h" 14 | #include "errno.h" 15 | #include "log.h" 16 | #include "isr.h" 17 | #include "syscall_select.h" 18 | 19 | void select_update(Thread* thread) 20 | { 21 | Process* process = thread->owner; 22 | 23 | int total_ready = 0; 24 | uint32_t count = (uint32_t)thread->select.nfds; 25 | count = MIN(count, SOSO_MAX_OPENED_FILES); 26 | 27 | for (uint32_t fd = 0; fd < count; ++fd) 28 | { 29 | File* file = process->fd[fd]; 30 | 31 | if (file) 32 | { 33 | if (FD_ISSET(fd, &thread->select.read_set)) 34 | { 35 | if (file->node->read_test_ready && file->node->read_test_ready(file)) 36 | { 37 | FD_SET(fd, &thread->select.read_set_result); 38 | 39 | ++total_ready; 40 | } 41 | } 42 | 43 | if (FD_ISSET(fd, &thread->select.write_set)) 44 | { 45 | if (file->node->write_test_ready && file->node->write_test_ready(file)) 46 | { 47 | FD_SET(fd, &thread->select.write_set_result); 48 | 49 | ++total_ready; 50 | } 51 | } 52 | } 53 | } 54 | 55 | if (total_ready > 0) 56 | { 57 | thread->select.result = total_ready; 58 | thread->select.select_state = SS_FINISHED; 59 | } 60 | else if (thread->select.target_time > 0) 61 | { 62 | time_t now = get_uptime_milliseconds64(); 63 | 64 | if (now > thread->select.target_time) 65 | { 66 | thread->select.result = 0; 67 | thread->select.select_state = SS_FINISHED; 68 | } 69 | } 70 | } 71 | 72 | static int select_finish(Thread* thread, fd_set* rfds, fd_set* wfds) 73 | { 74 | if (rfds) 75 | { 76 | *rfds = thread->select.read_set_result; 77 | } 78 | 79 | if (wfds) 80 | { 81 | *wfds = thread->select.write_set_result; 82 | } 83 | 84 | int result = thread->select.result; 85 | memset((uint8_t*)&thread->select, 0, sizeof(thread->select)); 86 | 87 | return result; 88 | } 89 | 90 | int syscall_select(int n, fd_set* rfds, fd_set* wfds, fd_set* efds, struct timeval* tv) 91 | { 92 | if (!check_user_access(rfds)) 93 | { 94 | return -EFAULT; 95 | } 96 | 97 | if (!check_user_access(wfds)) 98 | { 99 | return -EFAULT; 100 | } 101 | 102 | if (!check_user_access(efds)) 103 | { 104 | return -EFAULT; 105 | } 106 | 107 | if (!check_user_access(tv)) 108 | { 109 | return -EFAULT; 110 | } 111 | 112 | Thread* thread = thread_get_current(); 113 | Process* process = thread->owner; 114 | 115 | if (process) 116 | { 117 | thread->select.select_state = SS_STARTED; 118 | thread->select.nfds = n; 119 | FD_ZERO(&thread->select.read_set_result); 120 | FD_ZERO(&thread->select.write_set_result); 121 | 122 | FD_ZERO(&thread->select.read_set); 123 | if (rfds) 124 | { 125 | thread->select.read_set = *rfds; 126 | FD_ZERO(rfds); 127 | } 128 | 129 | FD_ZERO(&thread->select.write_set); 130 | if (wfds) 131 | { 132 | thread->select.write_set = *wfds; 133 | FD_ZERO(wfds); 134 | } 135 | 136 | thread->select.result = -1; 137 | 138 | thread->select.target_time = 0; 139 | if (tv) 140 | { 141 | thread->select.target_time = get_uptime_milliseconds64() + tv->tv_sec * 1000 + tv->tv_usec / 1000; 142 | } 143 | 144 | while (TRUE) 145 | { 146 | disable_interrupts(); 147 | 148 | select_update(thread); 149 | 150 | if (thread->select.select_state == SS_FINISHED) 151 | { 152 | int result = select_finish(thread, rfds, wfds); 153 | 154 | thread_resume(thread); 155 | 156 | return result; 157 | } 158 | 159 | thread_change_state(thread, TS_SELECT, NULL); 160 | enable_interrupts(); 161 | halt(); 162 | } 163 | } 164 | 165 | return -1; 166 | } -------------------------------------------------------------------------------- /TinyGL/src/zbuffer.h: -------------------------------------------------------------------------------- 1 | #ifndef _tgl_zbuffer_h_ 2 | #define _tgl_zbuffer_h_ 3 | 4 | /* 5 | * Z buffer 6 | */ 7 | 8 | #include "zfeatures.h" 9 | 10 | #define ZB_Z_BITS 16 11 | 12 | #define ZB_POINT_Z_FRAC_BITS 14 13 | 14 | #define ZB_POINT_S_MIN ( (1<<13) ) 15 | #define ZB_POINT_S_MAX ( (1<<22)-(1<<13) ) 16 | #define ZB_POINT_T_MIN ( (1<<21) ) 17 | #define ZB_POINT_T_MAX ( (1<<30)-(1<<21) ) 18 | 19 | #define ZB_POINT_RED_MIN ( (1<<10) ) 20 | #define ZB_POINT_RED_MAX ( (1<<16)-(1<<10) ) 21 | #define ZB_POINT_GREEN_MIN ( (1<<9) ) 22 | #define ZB_POINT_GREEN_MAX ( (1<<16)-(1<<9) ) 23 | #define ZB_POINT_BLUE_MIN ( (1<<10) ) 24 | #define ZB_POINT_BLUE_MAX ( (1<<16)-(1<<10) ) 25 | 26 | /* display modes */ 27 | #define ZB_MODE_5R6G5B 1 /* true color 16 bits */ 28 | #define ZB_MODE_INDEX 2 /* color index 8 bits */ 29 | #define ZB_MODE_RGBA 3 /* 32 bit rgba mode */ 30 | #define ZB_MODE_RGB24 4 /* 24 bit rgb mode */ 31 | #define ZB_NB_COLORS 225 /* number of colors for 8 bit display */ 32 | 33 | #if TGL_FEATURE_RENDER_BITS == 15 34 | 35 | #define RGB_TO_PIXEL(r,g,b) \ 36 | ((((r) >> 1) & 0x7c00) | (((g) >> 6) & 0x03e0) | ((b) >> 11)) 37 | typedef unsigned short PIXEL; 38 | /* bytes per pixel */ 39 | #define PSZB 2 40 | /* bits per pixel = (1 << PSZH) */ 41 | #define PSZSH 4 42 | 43 | #elif TGL_FEATURE_RENDER_BITS == 16 44 | 45 | /* 16 bit mode */ 46 | #define RGB_TO_PIXEL(r,g,b) \ 47 | (((r) & 0xF800) | (((g) >> 5) & 0x07E0) | ((b) >> 11)) 48 | typedef unsigned short PIXEL; 49 | #define PSZB 2 50 | #define PSZSH 4 51 | 52 | #elif TGL_FEATURE_RENDER_BITS == 24 53 | 54 | #define RGB_TO_PIXEL(r,g,b) \ 55 | ((((r) << 8) & 0xff0000) | ((g) & 0xff00) | ((b) >> 8)) 56 | typedef unsigned char PIXEL; 57 | #define PSZB 3 58 | #define PSZSH 5 59 | 60 | #elif TGL_FEATURE_RENDER_BITS == 32 61 | 62 | #define RGB_TO_PIXEL(r,g,b) \ 63 | ((((r) << 8) & 0xff0000) | ((g) & 0xff00) | ((b) >> 8)) 64 | typedef unsigned int PIXEL; 65 | #define PSZB 4 66 | #define PSZSH 5 67 | 68 | #else 69 | 70 | #error Incorrect number of bits per pixel 71 | 72 | #endif 73 | 74 | typedef struct { 75 | int xsize,ysize; 76 | int linesize; /* line size, in bytes */ 77 | int mode; 78 | 79 | unsigned short *zbuf; 80 | PIXEL *pbuf; 81 | int frame_buffer_allocated; 82 | 83 | int nb_colors; 84 | unsigned char *dctable; 85 | int *ctable; 86 | PIXEL *current_texture; 87 | } ZBuffer; 88 | 89 | typedef struct { 90 | int x,y,z; /* integer coordinates in the zbuffer */ 91 | int s,t; /* coordinates for the mapping */ 92 | int r,g,b; /* color indexes */ 93 | 94 | float sz,tz; /* temporary coordinates for mapping */ 95 | } ZBufferPoint; 96 | 97 | /* zbuffer.c */ 98 | 99 | ZBuffer *ZB_open(int xsize,int ysize,int mode, 100 | int nb_colors, 101 | unsigned char *color_indexes, 102 | int *color_table, 103 | void *frame_buffer); 104 | 105 | 106 | void ZB_close(ZBuffer *zb); 107 | 108 | void ZB_resize(ZBuffer *zb,void *frame_buffer,int xsize,int ysize); 109 | void ZB_clear(ZBuffer *zb,int clear_z,int z, 110 | int clear_color,int r,int g,int b); 111 | /* linesize is in BYTES */ 112 | void ZB_copyFrameBuffer(ZBuffer *zb,void *buf,int linesize); 113 | 114 | /* zdither.c */ 115 | 116 | void ZB_initDither(ZBuffer *zb,int nb_colors, 117 | unsigned char *color_indexes,int *color_table); 118 | void ZB_closeDither(ZBuffer *zb); 119 | void ZB_ditherFrameBuffer(ZBuffer *zb,unsigned char *dest, 120 | int linesize); 121 | 122 | /* zline.c */ 123 | 124 | void ZB_plot(ZBuffer *zb,ZBufferPoint *p); 125 | void ZB_line(ZBuffer *zb,ZBufferPoint *p1,ZBufferPoint *p2); 126 | void ZB_line_z(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2); 127 | 128 | /* ztriangle.c */ 129 | 130 | void ZB_setTexture(ZBuffer *zb, PIXEL *texture); 131 | 132 | void ZB_fillTriangleFlat(ZBuffer *zb, 133 | ZBufferPoint *p1,ZBufferPoint *p2,ZBufferPoint *p3); 134 | 135 | void ZB_fillTriangleSmooth(ZBuffer *zb, 136 | ZBufferPoint *p1,ZBufferPoint *p2,ZBufferPoint *p3); 137 | 138 | void ZB_fillTriangleMapping(ZBuffer *zb, 139 | ZBufferPoint *p1,ZBufferPoint *p2,ZBufferPoint *p3); 140 | 141 | void ZB_fillTriangleMappingPerspective(ZBuffer *zb, 142 | ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2); 143 | 144 | 145 | typedef void (*ZB_fillTriangleFunc)(ZBuffer *, 146 | ZBufferPoint *,ZBufferPoint *,ZBufferPoint *); 147 | 148 | /* memory.c */ 149 | void gl_free(void *p); 150 | void *gl_malloc(int size); 151 | void *gl_zalloc(int size); 152 | 153 | #endif /* _tgl_zbuffer_h_ */ 154 | -------------------------------------------------------------------------------- /TinyGL/include/zbuffer.h: -------------------------------------------------------------------------------- 1 | #ifndef _tgl_zbuffer_h_ 2 | #define _tgl_zbuffer_h_ 3 | 4 | /* 5 | * Z buffer 6 | */ 7 | 8 | #include "zfeatures.h" 9 | 10 | #define ZB_Z_BITS 16 11 | 12 | #define ZB_POINT_Z_FRAC_BITS 14 13 | 14 | #define ZB_POINT_S_MIN ( (1<<13) ) 15 | #define ZB_POINT_S_MAX ( (1<<22)-(1<<13) ) 16 | #define ZB_POINT_T_MIN ( (1<<21) ) 17 | #define ZB_POINT_T_MAX ( (1<<30)-(1<<21) ) 18 | 19 | #define ZB_POINT_RED_MIN ( (1<<10) ) 20 | #define ZB_POINT_RED_MAX ( (1<<16)-(1<<10) ) 21 | #define ZB_POINT_GREEN_MIN ( (1<<9) ) 22 | #define ZB_POINT_GREEN_MAX ( (1<<16)-(1<<9) ) 23 | #define ZB_POINT_BLUE_MIN ( (1<<10) ) 24 | #define ZB_POINT_BLUE_MAX ( (1<<16)-(1<<10) ) 25 | 26 | /* display modes */ 27 | #define ZB_MODE_5R6G5B 1 /* true color 16 bits */ 28 | #define ZB_MODE_INDEX 2 /* color index 8 bits */ 29 | #define ZB_MODE_RGBA 3 /* 32 bit rgba mode */ 30 | #define ZB_MODE_RGB24 4 /* 24 bit rgb mode */ 31 | #define ZB_NB_COLORS 225 /* number of colors for 8 bit display */ 32 | 33 | #if TGL_FEATURE_RENDER_BITS == 15 34 | 35 | #define RGB_TO_PIXEL(r,g,b) \ 36 | ((((r) >> 1) & 0x7c00) | (((g) >> 6) & 0x03e0) | ((b) >> 11)) 37 | typedef unsigned short PIXEL; 38 | /* bytes per pixel */ 39 | #define PSZB 2 40 | /* bits per pixel = (1 << PSZH) */ 41 | #define PSZSH 4 42 | 43 | #elif TGL_FEATURE_RENDER_BITS == 16 44 | 45 | /* 16 bit mode */ 46 | #define RGB_TO_PIXEL(r,g,b) \ 47 | (((r) & 0xF800) | (((g) >> 5) & 0x07E0) | ((b) >> 11)) 48 | typedef unsigned short PIXEL; 49 | #define PSZB 2 50 | #define PSZSH 4 51 | 52 | #elif TGL_FEATURE_RENDER_BITS == 24 53 | 54 | #define RGB_TO_PIXEL(r,g,b) \ 55 | ((((r) << 8) & 0xff0000) | ((g) & 0xff00) | ((b) >> 8)) 56 | typedef unsigned char PIXEL; 57 | #define PSZB 3 58 | #define PSZSH 5 59 | 60 | #elif TGL_FEATURE_RENDER_BITS == 32 61 | 62 | #define RGB_TO_PIXEL(r,g,b) \ 63 | ((((r) << 8) & 0xff0000) | ((g) & 0xff00) | ((b) >> 8)) 64 | typedef unsigned int PIXEL; 65 | #define PSZB 4 66 | #define PSZSH 5 67 | 68 | #else 69 | 70 | #error Incorrect number of bits per pixel 71 | 72 | #endif 73 | 74 | typedef struct { 75 | int xsize,ysize; 76 | int linesize; /* line size, in bytes */ 77 | int mode; 78 | 79 | unsigned short *zbuf; 80 | PIXEL *pbuf; 81 | int frame_buffer_allocated; 82 | 83 | int nb_colors; 84 | unsigned char *dctable; 85 | int *ctable; 86 | PIXEL *current_texture; 87 | } ZBuffer; 88 | 89 | typedef struct { 90 | int x,y,z; /* integer coordinates in the zbuffer */ 91 | int s,t; /* coordinates for the mapping */ 92 | int r,g,b; /* color indexes */ 93 | 94 | float sz,tz; /* temporary coordinates for mapping */ 95 | } ZBufferPoint; 96 | 97 | /* zbuffer.c */ 98 | 99 | ZBuffer *ZB_open(int xsize,int ysize,int mode, 100 | int nb_colors, 101 | unsigned char *color_indexes, 102 | int *color_table, 103 | void *frame_buffer); 104 | 105 | 106 | void ZB_close(ZBuffer *zb); 107 | 108 | void ZB_resize(ZBuffer *zb,void *frame_buffer,int xsize,int ysize); 109 | void ZB_clear(ZBuffer *zb,int clear_z,int z, 110 | int clear_color,int r,int g,int b); 111 | /* linesize is in BYTES */ 112 | void ZB_copyFrameBuffer(ZBuffer *zb,void *buf,int linesize); 113 | 114 | /* zdither.c */ 115 | 116 | void ZB_initDither(ZBuffer *zb,int nb_colors, 117 | unsigned char *color_indexes,int *color_table); 118 | void ZB_closeDither(ZBuffer *zb); 119 | void ZB_ditherFrameBuffer(ZBuffer *zb,unsigned char *dest, 120 | int linesize); 121 | 122 | /* zline.c */ 123 | 124 | void ZB_plot(ZBuffer *zb,ZBufferPoint *p); 125 | void ZB_line(ZBuffer *zb,ZBufferPoint *p1,ZBufferPoint *p2); 126 | void ZB_line_z(ZBuffer * zb, ZBufferPoint * p1, ZBufferPoint * p2); 127 | 128 | /* ztriangle.c */ 129 | 130 | void ZB_setTexture(ZBuffer *zb, PIXEL *texture); 131 | 132 | void ZB_fillTriangleFlat(ZBuffer *zb, 133 | ZBufferPoint *p1,ZBufferPoint *p2,ZBufferPoint *p3); 134 | 135 | void ZB_fillTriangleSmooth(ZBuffer *zb, 136 | ZBufferPoint *p1,ZBufferPoint *p2,ZBufferPoint *p3); 137 | 138 | void ZB_fillTriangleMapping(ZBuffer *zb, 139 | ZBufferPoint *p1,ZBufferPoint *p2,ZBufferPoint *p3); 140 | 141 | void ZB_fillTriangleMappingPerspective(ZBuffer *zb, 142 | ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2); 143 | 144 | 145 | typedef void (*ZB_fillTriangleFunc)(ZBuffer *, 146 | ZBufferPoint *,ZBufferPoint *,ZBufferPoint *); 147 | 148 | /* memory.c */ 149 | void gl_free(void *p); 150 | void *gl_malloc(int size); 151 | void *gl_zalloc(int size); 152 | 153 | #endif /* _tgl_zbuffer_h_ */ 154 | -------------------------------------------------------------------------------- /kernel/errno.h: -------------------------------------------------------------------------------- 1 | #ifndef ERRNO_H 2 | #define ERRNO_H 3 | 4 | #define EPERM 1 /* Operation not permitted */ 5 | #define ENOENT 2 /* No such file or directory */ 6 | #define ESRCH 3 /* No such process */ 7 | #define EINTR 4 /* Interrupted system call */ 8 | #define EIO 5 /* I/O error */ 9 | #define ENXIO 6 /* No such device or address */ 10 | #define E2BIG 7 /* Argument list too long */ 11 | #define ENOEXEC 8 /* Exec format error */ 12 | #define EBADF 9 /* Bad file number */ 13 | #define ECHILD 10 /* No child processes */ 14 | #define EAGAIN 11 /* Try again */ 15 | #define ENOMEM 12 /* Out of memory */ 16 | #define EACCES 13 /* Permission denied */ 17 | #define EFAULT 14 /* Bad address */ 18 | #define ENOTBLK 15 /* Block device required */ 19 | #define EBUSY 16 /* Device or resource busy */ 20 | #define EEXIST 17 /* File exists */ 21 | #define EXDEV 18 /* Cross-device link */ 22 | #define ENODEV 19 /* No such device */ 23 | #define ENOTDIR 20 /* Not a directory */ 24 | #define EISDIR 21 /* Is a directory */ 25 | #define EINVAL 22 /* Invalid argument */ 26 | #define ENFILE 23 /* File table overflow */ 27 | #define EMFILE 24 /* Too many open files */ 28 | #define ENOTTY 25 /* Not a typewriter */ 29 | #define ETXTBSY 26 /* Text file busy */ 30 | #define EFBIG 27 /* File too large */ 31 | #define ENOSPC 28 /* No space left on device */ 32 | #define ESPIPE 29 /* Illegal seek */ 33 | #define EROFS 30 /* Read-only file system */ 34 | #define EMLINK 31 /* Too many links */ 35 | #define EPIPE 32 /* Broken pipe */ 36 | #define EDOM 33 /* Math argument out of domain of func */ 37 | #define ERANGE 34 /* Math result not representable */ 38 | 39 | #define EDEADLK 35 40 | #define ENAMETOOLONG 36 41 | #define ENOLCK 37 42 | #define ENOSYS 38 43 | #define ENOTEMPTY 39 44 | #define ELOOP 40 45 | #define EWOULDBLOCK EAGAIN 46 | #define ENOMSG 42 47 | #define EIDRM 43 48 | #define ECHRNG 44 49 | #define EL2NSYNC 45 50 | #define EL3HLT 46 51 | #define EL3RST 47 52 | #define ELNRNG 48 53 | #define EUNATCH 49 54 | #define ENOCSI 50 55 | #define EL2HLT 51 56 | #define EBADE 52 57 | #define EBADR 53 58 | #define EXFULL 54 59 | #define ENOANO 55 60 | #define EBADRQC 56 61 | #define EBADSLT 57 62 | #define EDEADLOCK EDEADLK 63 | #define EBFONT 59 64 | #define ENOSTR 60 65 | #define ENODATA 61 66 | #define ETIME 62 67 | #define ENOSR 63 68 | #define ENONET 64 69 | #define ENOPKG 65 70 | #define EREMOTE 66 71 | #define ENOLINK 67 72 | #define EADV 68 73 | #define ESRMNT 69 74 | #define ECOMM 70 75 | #define EPROTO 71 76 | #define EMULTIHOP 72 77 | #define EDOTDOT 73 78 | #define EBADMSG 74 79 | #define EOVERFLOW 75 80 | #define ENOTUNIQ 76 81 | #define EBADFD 77 82 | #define EREMCHG 78 83 | #define ELIBACC 79 84 | #define ELIBBAD 80 85 | #define ELIBSCN 81 86 | #define ELIBMAX 82 87 | #define ELIBEXEC 83 88 | #define EILSEQ 84 89 | #define ERESTART 85 90 | #define ESTRPIPE 86 91 | #define EUSERS 87 92 | #define ENOTSOCK 88 //File descriptor socket is not a socket. 93 | #define EDESTADDRREQ 89 94 | #define EMSGSIZE 90 95 | #define EPROTOTYPE 91 96 | #define ENOPROTOOPT 92 97 | #define EPROTONOSUPPORT 93 98 | #define ESOCKTNOSUPPORT 94 99 | #define EOPNOTSUPP 95 100 | #define ENOTSUP EOPNOTSUPP 101 | #define EPFNOSUPPORT 96 102 | #define EAFNOSUPPORT 97 103 | #define EADDRINUSE 98 104 | #define EADDRNOTAVAIL 99 105 | #define ENETDOWN 100 106 | #define ENETUNREACH 101 107 | #define ENETRESET 102 108 | #define ECONNABORTED 103 109 | #define ECONNRESET 104 110 | #define ENOBUFS 105 111 | #define EISCONN 106 112 | #define ENOTCONN 107 113 | #define ESHUTDOWN 108 114 | #define ETOOMANYREFS 109 115 | #define ETIMEDOUT 110 116 | #define ECONNREFUSED 111 117 | #define EHOSTDOWN 112 118 | #define EHOSTUNREACH 113 119 | #define EALREADY 114 120 | #define EINPROGRESS 115 121 | #define ESTALE 116 122 | #define EUCLEAN 117 123 | #define ENOTNAM 118 124 | #define ENAVAIL 119 125 | #define EISNAM 120 126 | #define EREMOTEIO 121 127 | #define EDQUOT 122 128 | #define ENOMEDIUM 123 129 | #define EMEDIUMTYPE 124 130 | #define ECANCELED 125 131 | #define ENOKEY 126 132 | #define EKEYEXPIRED 127 133 | #define EKEYREVOKED 128 134 | #define EKEYREJECTED 129 135 | #define EOWNERDEAD 130 136 | #define ENOTRECOVERABLE 131 137 | #define ERFKILL 132 138 | #define EHWPOISON 133 139 | 140 | #endif //ERRNO_H -------------------------------------------------------------------------------- /TinyGL/src/init.c: -------------------------------------------------------------------------------- 1 | #include "zgl.h" 2 | 3 | GLContext *gl_ctx; 4 | 5 | 6 | void initSharedState(GLContext *c) 7 | { 8 | GLSharedState *s=&c->shared_state; 9 | s->lists=gl_zalloc(sizeof(GLList *) * MAX_DISPLAY_LISTS); 10 | s->texture_hash_table= 11 | gl_zalloc(sizeof(GLTexture *) * TEXTURE_HASH_TABLE_SIZE); 12 | 13 | alloc_texture(c,0); 14 | } 15 | 16 | void endSharedState(GLContext *c) 17 | { 18 | GLSharedState *s=&c->shared_state; 19 | int i; 20 | 21 | for(i=0;ilists); 25 | 26 | gl_free(s->texture_hash_table); 27 | } 28 | 29 | 30 | void glInit(void *zbuffer1) 31 | { 32 | ZBuffer *zbuffer=(ZBuffer *)zbuffer1; 33 | GLContext *c; 34 | GLViewport *v; 35 | int i; 36 | 37 | c=gl_zalloc(sizeof(GLContext)); 38 | gl_ctx=c; 39 | 40 | c->zb=zbuffer; 41 | 42 | /* allocate GLVertex array */ 43 | c->vertex_max = POLYGON_MAX_VERTEX; 44 | c->vertex = gl_malloc(POLYGON_MAX_VERTEX*sizeof(GLVertex)); 45 | 46 | /* viewport */ 47 | v=&c->viewport; 48 | v->xmin=0; 49 | v->ymin=0; 50 | v->xsize=zbuffer->xsize; 51 | v->ysize=zbuffer->ysize; 52 | v->updated=1; 53 | 54 | /* shared state */ 55 | initSharedState(c); 56 | 57 | /* lists */ 58 | 59 | c->exec_flag=1; 60 | c->compile_flag=0; 61 | c->print_flag=0; 62 | 63 | c->in_begin=0; 64 | 65 | /* lights */ 66 | for(i=0;ilights[i]; 68 | l->ambient=gl_V4_New(0,0,0,1); 69 | l->diffuse=gl_V4_New(1,1,1,1); 70 | l->specular=gl_V4_New(1,1,1,1); 71 | l->position=gl_V4_New(0,0,1,0); 72 | l->norm_position=gl_V3_New(0,0,1); 73 | l->spot_direction=gl_V3_New(0,0,-1); 74 | l->norm_spot_direction=gl_V3_New(0,0,-1); 75 | l->spot_exponent=0; 76 | l->spot_cutoff=180; 77 | l->attenuation[0]=1; 78 | l->attenuation[1]=0; 79 | l->attenuation[2]=0; 80 | l->enabled=0; 81 | } 82 | c->first_light=NULL; 83 | c->ambient_light_model=gl_V4_New(0.2,0.2,0.2,1); 84 | c->local_light_model=0; 85 | c->lighting_enabled=0; 86 | c->light_model_two_side = 0; 87 | 88 | /* default materials */ 89 | for(i=0;i<2;i++) { 90 | GLMaterial *m=&c->materials[i]; 91 | m->emission=gl_V4_New(0,0,0,1); 92 | m->ambient=gl_V4_New(0.2,0.2,0.2,1); 93 | m->diffuse=gl_V4_New(0.8,0.8,0.8,1); 94 | m->specular=gl_V4_New(0,0,0,1); 95 | m->shininess=0; 96 | } 97 | c->current_color_material_mode=GL_FRONT_AND_BACK; 98 | c->current_color_material_type=GL_AMBIENT_AND_DIFFUSE; 99 | c->color_material_enabled=0; 100 | 101 | /* textures */ 102 | glInitTextures(c); 103 | 104 | /* default state */ 105 | c->current_color.X=1.0; 106 | c->current_color.Y=1.0; 107 | c->current_color.Z=1.0; 108 | c->current_color.W=1.0; 109 | c->longcurrent_color[0] = 65535; 110 | c->longcurrent_color[1] = 65535; 111 | c->longcurrent_color[2] = 65535; 112 | 113 | c->current_normal.X=1.0; 114 | c->current_normal.Y=0.0; 115 | c->current_normal.Z=0.0; 116 | c->current_normal.W=0.0; 117 | 118 | c->current_edge_flag=1; 119 | 120 | c->current_tex_coord.X=0; 121 | c->current_tex_coord.Y=0; 122 | c->current_tex_coord.Z=0; 123 | c->current_tex_coord.W=1; 124 | 125 | c->polygon_mode_front=GL_FILL; 126 | c->polygon_mode_back=GL_FILL; 127 | 128 | c->current_front_face=0; /* 0 = GL_CCW 1 = GL_CW */ 129 | c->current_cull_face=GL_BACK; 130 | c->current_shade_model=GL_SMOOTH; 131 | c->cull_face_enabled=0; 132 | 133 | /* clear */ 134 | c->clear_color.v[0]=0; 135 | c->clear_color.v[1]=0; 136 | c->clear_color.v[2]=0; 137 | c->clear_color.v[3]=0; 138 | c->clear_depth=0; 139 | 140 | /* selection */ 141 | c->render_mode=GL_RENDER; 142 | c->select_buffer=NULL; 143 | c->name_stack_size=0; 144 | 145 | /* matrix */ 146 | c->matrix_mode=0; 147 | 148 | c->matrix_stack_depth_max[0]=MAX_MODELVIEW_STACK_DEPTH; 149 | c->matrix_stack_depth_max[1]=MAX_PROJECTION_STACK_DEPTH; 150 | c->matrix_stack_depth_max[2]=MAX_TEXTURE_STACK_DEPTH; 151 | 152 | for(i=0;i<3;i++) { 153 | c->matrix_stack[i]=gl_zalloc(c->matrix_stack_depth_max[i] * sizeof(M4)); 154 | c->matrix_stack_ptr[i]=c->matrix_stack[i]; 155 | } 156 | 157 | glMatrixMode(GL_PROJECTION); 158 | glLoadIdentity(); 159 | glMatrixMode(GL_TEXTURE); 160 | glLoadIdentity(); 161 | glMatrixMode(GL_MODELVIEW); 162 | glLoadIdentity(); 163 | 164 | c->matrix_model_projection_updated=1; 165 | 166 | /* opengl 1.1 arrays */ 167 | c->client_states = 0; 168 | 169 | /* opengl 1.1 polygon offset */ 170 | c->offset_states = 0; 171 | 172 | /* clear the resize callback function pointer */ 173 | c->gl_resize_viewport = NULL; 174 | 175 | /* specular buffer */ 176 | c->specbuf_first = NULL; 177 | c->specbuf_used_counter = 0; 178 | c->specbuf_num_buffers = 0; 179 | 180 | /* depth test */ 181 | c->depth_test = 0; 182 | } 183 | 184 | void glClose(void) 185 | { 186 | GLContext *c=gl_get_context(); 187 | endSharedState(c); 188 | gl_free(c); 189 | } 190 | -------------------------------------------------------------------------------- /userspace/nx/progs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define MWINCLUDECOLORS 14 | #include "nano-X.h" 15 | #include "nxdraw.h" 16 | 17 | extern char **environ; 18 | 19 | GR_WINDOW_ID g_window_id; 20 | GR_GC_ID g_graphics_context; 21 | GR_FONT_ID g_font; 22 | 23 | GR_SCREEN_INFO g_screen_info; 24 | GR_FONT_INFO g_font_info; 25 | 26 | 27 | GR_WINDOW_INFO g_window_info; 28 | GR_GC_INFO g_gc_info; 29 | 30 | #define BUTTON_WIDTH 60 31 | 32 | void start_term() 33 | { 34 | int pid = fork(); 35 | if (pid == 0) 36 | { 37 | char* argv[2]; 38 | argv[0] = "/initrd/term"; 39 | argv[1] = NULL; 40 | execv(argv[0], argv); 41 | 42 | //if returns, it means it cannot start the process so exiting the child. 43 | exit(1); 44 | } 45 | } 46 | 47 | void start_doom() 48 | { 49 | int pid = fork(); 50 | if (pid == 0) 51 | { 52 | chdir("/initrd"); 53 | 54 | char* argv[2]; 55 | argv[0] = "/initrd/doom"; 56 | argv[1] = NULL; 57 | execv(argv[0], argv); 58 | 59 | //if returns, it means it cannot start the process so exiting the child. 60 | exit(1); 61 | } 62 | } 63 | 64 | int main(int argc, char **argv) 65 | { 66 | GR_WM_PROPERTIES props; 67 | 68 | if (GrOpen() < 0) 69 | { 70 | GrError("cannot open graphics\n"); 71 | exit(1); 72 | } 73 | 74 | GrGetScreenInfo(&g_screen_info); 75 | 76 | g_font = GrCreateFontEx(GR_FONT_SYSTEM_FIXED, 0, 0, NULL); 77 | 78 | GrGetFontInfo(g_font, &g_font_info); 79 | int winw = 200; 80 | int winh = 80; 81 | 82 | g_window_id = GrNewWindow(GR_ROOT_WINDOW_ID, 600, 10, winw, winh, 0, LTGRAY, BLACK); 83 | 84 | props.title = "Programs"; 85 | props.flags = GR_WM_FLAGS_TITLE | GR_WM_FLAGS_PROPS; 86 | props.props = GR_WM_PROPS_CAPTION; 87 | GrSetWMProperties(g_window_id, &props); 88 | 89 | GrSelectEvents(g_window_id, GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_EXPOSURE | 90 | GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_CLOSE_REQ); 91 | GrMapWindow(g_window_id); 92 | 93 | GrMoveWindow(g_window_id, 600, 10); 94 | 95 | GrGetWindowInfo(g_window_id,&g_window_info); 96 | 97 | g_graphics_context = GrNewGC(); 98 | GrSetGCFont(g_graphics_context, g_font); 99 | 100 | GrSetGCForeground(g_graphics_context, BLACK); 101 | GrSetGCBackground(g_graphics_context, GRAY); 102 | GrGetGCInfo(g_graphics_context, &g_gc_info); 103 | 104 | GR_WM_PROPERTIES props_button; 105 | props_button.flags = GR_WM_FLAGS_PROPS; 106 | props_button.props = GR_WM_PROPS_CAPTION; 107 | 108 | GR_WINDOW_ID button_terminal = GrNewWindow(g_window_id, 10, 10, BUTTON_WIDTH, 20, 0, GRAY, BLACK); 109 | GrSetWMProperties(button_terminal, &props_button); 110 | GrMapWindow(button_terminal); 111 | GrSelectEvents(button_terminal, GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP | GR_EVENT_MASK_EXPOSURE); 112 | 113 | GR_WINDOW_ID button_doom = GrNewWindow(g_window_id, 80, 10, BUTTON_WIDTH, 20, 0, GRAY, BLACK); 114 | GrSetWMProperties(button_doom, &props_button); 115 | GrMapWindow(button_doom); 116 | GrSelectEvents(button_doom, GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP | GR_EVENT_MASK_EXPOSURE); 117 | 118 | 119 | GR_EVENT wevent; 120 | GR_EVENT_KEYSTROKE *kp = NULL; 121 | GR_EVENT_BUTTON *button_event = NULL; 122 | GR_EVENT_EXPOSURE *exp_event = NULL; 123 | while (1) 124 | { 125 | GrGetNextEvent(&wevent); 126 | 127 | switch(wevent.type) 128 | { 129 | case GR_EVENT_TYPE_CLOSE_REQ: 130 | //GrClose(); 131 | //exit(0); 132 | break; 133 | case GR_EVENT_TYPE_EXPOSURE: 134 | exp_event = (GR_EVENT_EXPOSURE*)&wevent; 135 | if (exp_event->wid == button_terminal) 136 | { 137 | nxDraw3dUpDownState(button_terminal, 0, 0, BUTTON_WIDTH, 20, FALSE); 138 | GrText(button_terminal, g_graphics_context, 6, 15, "Terminal", -1, GR_TFASCII); 139 | } 140 | else if (exp_event->wid == button_doom) 141 | { 142 | nxDraw3dUpDownState(button_doom, 0, 0, BUTTON_WIDTH, 20, FALSE); 143 | GrText(button_doom, g_graphics_context, 18, 15, "Doom", -1, GR_TFASCII); 144 | } 145 | break; 146 | 147 | case GR_EVENT_TYPE_KEY_DOWN: 148 | kp = (GR_EVENT_KEYSTROKE *)&wevent; 149 | 150 | break; 151 | case GR_EVENT_TYPE_BUTTON_DOWN: 152 | button_event = (GR_EVENT_BUTTON*)&wevent; 153 | if (button_event->wid == button_terminal) 154 | { 155 | nxDraw3dUpDownState(button_terminal, 0, 0, BUTTON_WIDTH, 20, TRUE); 156 | start_term(); 157 | } 158 | else if (button_event->wid == button_doom) 159 | { 160 | nxDraw3dUpDownState(button_doom, 0, 0, BUTTON_WIDTH, 20, TRUE); 161 | start_doom(); 162 | } 163 | break; 164 | case GR_EVENT_TYPE_BUTTON_UP: 165 | button_event = (GR_EVENT_BUTTON*)&wevent; 166 | if (button_event->wid == button_terminal || button_event->wid == button_doom) 167 | { 168 | nxDraw3dUpDownState(button_event->wid, 0, 0, BUTTON_WIDTH, 20, FALSE); 169 | } 170 | break; 171 | break; 172 | } 173 | 174 | } 175 | return 0; 176 | } -------------------------------------------------------------------------------- /kernel/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD 2-Clause License 3 | * 4 | * Copyright (c) 2017, ozkl 5 | * All rights reserved. 6 | * 7 | * This file is licensed under the BSD 2-Clause License. 8 | * See the LICENSE file in the project root for full license information. 9 | */ 10 | 11 | #ifndef COMMON_H 12 | #define COMMON_H 13 | 14 | #include "stdint.h" 15 | 16 | #define enable_interrupts() asm volatile("sti") 17 | #define disable_interrupts() asm volatile("cli") 18 | #define halt() asm volatile("hlt") 19 | 20 | #define BOOL uint8_t 21 | #define TRUE 1 22 | #define FALSE 0 23 | #define NULL 0 24 | #define CHECK_BIT(value, pos) ((value) & (1 << (pos))) 25 | 26 | #define BITMAP_DEFINE(bitmap, size) uint8_t bitmap[size/8] 27 | #define BITMAP_SET(bitmap, index) bitmap[((uint32_t)index)/8] |= (1 << (((uint32_t) index)%8)) 28 | #define BITMAP_UNSET(bitmap, index) bitmap[((uint32_t)index)/8] &= ~(1 << (((uint32_t) index)%8)) 29 | #define BITMAP_CHECK(bitmap, index) (bitmap[((uint32_t) index)/8] & (1 << (((uint32_t) index)%8))) 30 | 31 | #define KERNEL_VIRTUAL_BASE 0xC0000000 32 | 33 | 34 | 35 | extern uint32_t g_modules_end_physical; 36 | 37 | 38 | extern uint32_t g_kern_heap_begin; 39 | 40 | #define PAGING_FLAG 0x80000000 // CR0 - bit 31 41 | #define PSE_FLAG 0x00000010 // CR4 - bit 4 //For 4M page support. 42 | #define PG_PRESENT 0x00000001 // page directory / table 43 | #define PG_WRITE 0x00000002 44 | #define PG_USER 0x00000004 45 | #define PG_4MB 0x00000080 46 | #define PG_OWNED 0x00000200 // We use 9th bit for bookkeeping of owned pages (9-11th bits are available for OS) 47 | #define PAGESIZE_4K 0x00001000 48 | #define PAGESIZE_4M 0x00400000 49 | #define RAM_AS_4K_PAGES 0x100000 50 | #define RAM_AS_4M_PAGES 1024 51 | #define PAGE_COUNT(bytes) (((bytes-1) / PAGESIZE_4K) + 1) 52 | #define PAGE_INDEX_4K(addr) ((addr) >> 12) 53 | #define PAGE_INDEX_4M(addr) ((addr) >> 22) 54 | 55 | #define KERN_STACK_SIZE PAGESIZE_4K 56 | 57 | 58 | #define USER_MMAP_START 0x80000000 //This is just for mapping starts searching vmem from here not to conflict with brk. 59 | #define USER_BRK_START 0x70000000 60 | #define MEMORY_END 0xFFC00000 //After this address is not usable. Because Page Tables sit there! 61 | 62 | #define GFX_MEMORY 0xFF400000 63 | 64 | #define KERN_HEAP_END GFX_MEMORY 65 | 66 | #define USER_STACK 0xA0000000 67 | 68 | void outb(uint16_t port, uint8_t value); 69 | void outw(uint16_t port, uint16_t value); 70 | uint8_t inb(uint16_t port); 71 | uint16_t inw(uint16_t port); 72 | 73 | #define PANIC(msg) panic(msg, __FILE__, __LINE__); 74 | #define WARNING(msg) warning(msg, __FILE__, __LINE__); 75 | #define ASSERT(b) ((b) ? (void)0 : panic_assert(__FILE__, __LINE__, #b)) 76 | 77 | #define MIN(a,b) (((a)<(b))?(a):(b)) 78 | #define MAX(a,b) (((a)>(b))?(a):(b)) 79 | 80 | void warning(const char *message, const char *file, uint32_t line); 81 | void panic(const char *message, const char *file, uint32_t line); 82 | void panic_assert(const char *file, uint32_t line, const char *desc); 83 | 84 | void* memset(uint8_t *dest, uint8_t val, uint32_t len); 85 | void* memcpy(uint8_t *dest, const uint8_t *src, uint32_t len); 86 | void* memmove(void* dest, const void* src, uint32_t n); 87 | int memcmp(const void* p1, const void* p2, uint32_t c); 88 | 89 | int strcmp(const char *str1, const char *str2); 90 | int strncmp(const char *str1, const char *str2, int length); 91 | char *strcpy(char *dest, const char *src); 92 | char *strcpy_nonnull(char *dest, const char *src); 93 | char *strncpy(char *dest, const char *src, uint32_t num); 94 | char* strncpy_null(char *dest, const char *src, uint32_t num); 95 | char* strcat(char *dest, const char *src); 96 | int strlen(const char *src); 97 | char * strstr(const char * haystack, const char * needle); 98 | char* strchr(const char* str, int ch); 99 | int str_first_index_of(const char *src, char c); 100 | void get_parent_path(const char *input, char *output, size_t output_size); 101 | void get_basename(const char *input, char *output, size_t output_size); 102 | int snprintf(char* buffer, uint32_t buffer_size, const char *format, ...); 103 | 104 | void printkf(const char *format, ...); 105 | 106 | int atoi(char *str); 107 | void itoa(char *buf, int base, int d); 108 | long strtol(const char *nptr, char **endptr, int base); 109 | 110 | uint32_t rand(); 111 | 112 | uint32_t read_eip(); 113 | uint32_t read_esp(); 114 | uint32_t read_cr3(); 115 | uint32_t get_cpu_flags(); 116 | BOOL is_interrupts_enabled(); 117 | 118 | void begin_critical_section(); 119 | void end_critical_section(); 120 | 121 | BOOL check_user_access(void* pointer); 122 | BOOL check_user_access_string_array(char *const array[]); 123 | 124 | 125 | #define isalpha(a) ((((unsigned)(a)|32)-'a') < 26) 126 | #define isdigit(a) (((unsigned)(a)-'0') < 10) 127 | #define islower(a) (((unsigned)(a)-'a') < 26) 128 | #define isupper(a) (((unsigned)(a)-'A') < 26) 129 | #define isprint(a) (((unsigned)(a)-0x20) < 0x5f) 130 | #define isgraph(a) (((unsigned)(a)-0x21) < 0x5e) 131 | #define isspace(a) (a == ' ' || (unsigned)a-'\t' < 5) 132 | #define iscntrl(a) ((unsigned)a < 0x20 || a == 0x7f) 133 | #define tolower(a) ((a)|0x20) 134 | #define toupper(a) ((a)&0x5f) 135 | 136 | #endif // COMMON_H 137 | --------------------------------------------------------------------------------