├── README.md └── sgos2 ├── Console.lnk ├── Makefile ├── api ├── Makefile ├── alloc.c ├── api.c ├── apiImplement.h ├── clock.c ├── ctype.c ├── file.c ├── init.c ├── math.c ├── printf.c ├── process.c ├── service.c ├── sleep.c ├── string.c ├── time.c └── vsprintf.c ├── apps ├── Makefile ├── acpi │ ├── Makefile │ ├── acpi.h │ ├── driver.cpp │ ├── main.cpp │ ├── port.cpp │ └── port.h ├── apps_vm.txt ├── cat.exe ├── console │ ├── Makefile │ ├── console.cpp │ ├── console.h │ ├── main.cpp │ ├── port.cpp │ └── port.h ├── env.exe ├── fs │ ├── Makefile │ ├── buffer.cpp │ ├── buffer.h │ ├── device.cpp │ ├── device.h │ ├── fat32.c │ ├── fat32.h │ ├── fsmanager.cpp │ ├── fsservice.h │ ├── main.cpp │ ├── rootfs.cpp │ ├── unicode.c │ ├── unicode.h │ ├── vfs.cpp │ └── vfs.txt ├── hd │ ├── Makefile │ ├── hd.h │ ├── lba.cpp │ ├── main.cpp │ ├── port.cpp │ └── port.h ├── hello │ ├── Makefile │ └── main.cpp ├── keyboard │ ├── Makefile │ ├── dispatch.cpp │ ├── driver.cpp │ ├── keyboard.h │ ├── keymap_us.h │ ├── main.cpp │ ├── port.cpp │ └── port.h ├── msg │ ├── Makefile │ ├── bxml.c │ ├── msg.c │ ├── name.c │ └── operation.c ├── msys │ ├── Makefile │ ├── argv.c │ ├── crt.c │ ├── cygwin.h │ ├── debug.c │ ├── debug.h │ ├── environ.c │ ├── exec.c │ ├── exit.c │ ├── features.h │ ├── file.c │ ├── float.c │ ├── fork.c │ ├── fstat.c │ ├── getpid.c │ ├── getpwd.c │ ├── handle.c │ ├── handle.h │ ├── kill.c │ ├── link.c │ ├── misc.c │ ├── msys.c │ ├── reent.h │ ├── sbrk.c │ ├── stat.c │ ├── sysconf.c │ ├── time.c │ ├── tty.c │ ├── uname.c │ ├── unistd.h │ ├── virtual.c │ └── wait.c ├── servicemanager │ ├── Makefile │ └── main.cpp ├── speaker │ ├── Makefile │ ├── main.cpp │ ├── port.cpp │ ├── port.h │ └── speaker.cpp ├── startup │ ├── Makefile │ └── main.c ├── system │ ├── Makefile │ ├── base │ │ └── string.cpp │ ├── message │ │ └── messenger.cpp │ ├── service │ │ ├── bioscall.cpp │ │ ├── map.cpp │ │ ├── name.cpp │ │ └── port.cpp │ └── threading │ │ └── thread.cpp ├── time │ ├── Makefile │ ├── biostime.cpp │ ├── biostime.h │ ├── main.cpp │ ├── port.cpp │ └── port.h ├── uname.exe ├── vesa │ ├── Makefile │ ├── int86.cpp │ ├── main.cpp │ ├── port.cpp │ ├── port.h │ ├── vesa.cpp │ └── vesa.h ├── wadvapi │ ├── Makefile │ ├── advapi.c │ ├── debug.c │ ├── debug.h │ ├── fun.txt │ └── security.c ├── wkernel │ ├── Makefile │ ├── backup.c │ ├── badptr.c │ ├── beep.c │ ├── char.c │ ├── comm.c │ ├── console.c │ ├── debug.c │ ├── debug.h │ ├── dir.c │ ├── dll.c │ ├── emul_handle.c │ ├── env.c │ ├── error.c │ ├── event.c │ ├── file.c │ ├── filetime.c │ ├── find.c │ ├── handle.c │ ├── kernel.c │ ├── kernel.h │ ├── mapping.c │ ├── mem.c │ ├── mutex.c │ ├── object.c │ ├── performance.c │ ├── pipe.c │ ├── process.c │ ├── sleep.c │ ├── systeminfo.c │ ├── tape.c │ ├── thread.c │ ├── time.c │ ├── tls.c │ ├── util.c │ ├── version.c │ ├── volume.c │ └── windbg.c ├── wntdll │ ├── Makefile │ ├── debug.c │ ├── debug.h │ └── fun.txt └── wprocess │ ├── Makefile │ ├── main.cpp │ ├── module.cpp │ ├── module.h │ ├── parser.cpp │ ├── pe.h │ ├── peloader.cpp │ ├── wprocess.cpp │ └── wprocess.h ├── crt ├── Makefile ├── alloca.S ├── ctors.S ├── exception.cpp ├── exit.cpp ├── init.cpp ├── memory.cpp └── virtual.cpp ├── doc ├── BXML.doc ├── SGOS2内核代码命名约定.doc ├── SGOS2内核内存管理.doc ├── SGOS2可执行文件结构说明.doc ├── SGOS2开发文档1.9.doc └── SGOS2程序创建与运行过程.doc ├── include ├── api.h ├── bxml.h ├── c++ │ ├── basestring.h │ ├── messenger.h │ ├── service.h │ ├── system.h │ └── thread.h ├── ctype.h ├── errno.h ├── msg.h ├── pthread.h ├── queue.h ├── sema.h ├── sgos.h ├── sgos_version.h ├── stdarg.h ├── stdio.h ├── stdlib.h ├── string.h ├── time.h └── types.h ├── kernel ├── Makefile ├── arch │ └── i386 │ │ ├── Makefile │ │ ├── clock │ │ └── rtc.c │ │ ├── cpu │ │ ├── fastcall.c │ │ ├── lock.c │ │ ├── switch.S │ │ ├── sysenter.S │ │ ├── threading.c │ │ └── vm86.c │ │ ├── debug │ │ └── dbgx86.c │ │ ├── init │ │ ├── init.c │ │ └── multiboot.S │ │ ├── io │ │ ├── port.c │ │ └── terminal.c │ │ ├── irq │ │ ├── interrupt.S │ │ ├── irq.c │ │ └── isr.c │ │ ├── kernel.ld │ │ ├── math │ │ └── fpu.c │ │ ├── misc │ │ └── mmop.c │ │ └── mmu │ │ ├── fault.c │ │ ├── gdt.c │ │ ├── map.c │ │ └── page.c ├── include │ ├── allocator.h │ ├── apidef.h │ ├── arch_i386 │ │ ├── arch.h │ │ ├── gdt_const.h │ │ ├── io.h │ │ ├── lock.h │ │ ├── mmop.h │ │ ├── msr.h │ │ ├── multiboot.h │ │ ├── terminal.h │ │ └── types.h │ ├── bigblock.h │ ├── ctype.h │ ├── ipc.h │ ├── kd.h │ ├── ke.h │ ├── loader.h │ ├── mm.h │ ├── module.h │ ├── rtl.h │ ├── stdarg.h │ ├── time.h │ └── tm.h ├── ipc │ ├── message.c │ └── semaphore.c ├── kd │ ├── debug.c │ └── format.c ├── lib │ ├── bits.c │ ├── ctype.c │ ├── kqueue.c │ ├── time.c │ └── vsprintf.c ├── mm │ ├── allocator.c │ ├── globalmemory.c │ ├── kernelmemory.c │ ├── physical.c │ ├── space.c │ ├── usermemory.c │ └── virtual.c ├── start │ ├── elfloader.c │ ├── hwintr.c │ ├── kstart.c │ ├── load.c │ ├── pe.h │ ├── peloader.c │ ├── sysinfo.c │ └── system.c └── tm │ ├── sched.c │ ├── sys.c │ └── thread.c ├── qemu ├── bios.bin ├── vgabios-cirrus.bin └── vgabios.bin ├── scripts ├── bochsrc.bxrc ├── gdb ├── menu.lst └── startup.txt ├── setenv.bat ├── sgos2.7z └── tools ├── ld2 ├── bxml.c ├── bxml.h ├── elf.h ├── ld2.c ├── ld2.exe └── pe.h ├── vm86 ├── Makefile ├── vm86.asm └── vm86.com └── wf ├── Makefile ├── buffer.c ├── buffer.h ├── fat32.c ├── fat32.h ├── unicode.c ├── unicode.db ├── unicode.h ├── wf ├── wf.exe └── wfat.c /README.md: -------------------------------------------------------------------------------- 1 | # SGOS 2 | 3 | 一个多任务的操作系统,可运行在Intel x86 32位的计算机上。 4 | 5 | ## 存在的意义 6 | 7 | 作为深入了解计算机系统工作原理的学习资料,非常有用!称为大神,需走此路。 8 | 9 | ## 一个问题 10 | 11 | 你觉得是先有编译器还是先有操作系统? 12 | -------------------------------------------------------------------------------- /sgos2/Console.lnk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/Console.lnk -------------------------------------------------------------------------------- /sgos2/Makefile: -------------------------------------------------------------------------------- 1 | #Makefile for SGOS2 2 | 3 | # OS type. 4 | ifndef OSTYPE 5 | export OSTYPE := $(shell uname) 6 | endif 7 | 8 | # Tool set 9 | ifeq ($(OSTYPE), Linux) 10 | export RM := rm -f 11 | export EXE_SUFFIX := 12 | else 13 | export RM := rm -f 14 | export EXE_SUFFIX := .exe 15 | endif 16 | 17 | # Check the build tool 18 | define wf-if-not-exist 19 | $(if $(wildcard $1),,@make -C tools/wf) 20 | endef 21 | 22 | .PHONY : all check run bochsdbg bochs windbg debug clean 23 | # Target 24 | all: check grub 25 | make -C crt 26 | make -C kernel 27 | #make -C apps 28 | @echo OK! 29 | 30 | grub: scripts/menu.lst 31 | tools/wf/wf sgos2.img -src scripts/menu.lst -dest boot/grub/menu.lst 32 | tools/wf/wf sgos2.img -src scripts/startup.txt -dest sgos/startup.txt 33 | tools/wf/wf sgos2.img -src tools/vm86/vm86.com -dest sgos/vm86.com 34 | 35 | check: 36 | $(call wf-if-not-exist, tools/wf/wf$(EXE_SUFFIX)) 37 | 38 | run: 39 | qemu -m 64 -L ./qemu sgos2.img -soundhw pcspk 40 | 41 | bochsdbg: 42 | bochsdbg -q -f scripts/bochsrc.bxrc 43 | 44 | bochs: 45 | bochs -q -f scripts/bochsrc.bxrc 46 | 47 | windbg: 48 | start insight kernel/kernel & 49 | start qemu -no-kqemu -m 64 -s -S -L ./qemu sgos2.img & 50 | 51 | kernel32: 52 | start insight apps/wkernel/kernel32.dll & 53 | start qemu -no-kqemu -m 64 -s -S -L ./qemu sgos2.img & 54 | 55 | debug: 56 | qemu -no-kqemu -m 32 -s -S -L ./qemu sgos2.img & 57 | gdb --command scripts/gdb kernel/kernel 58 | 59 | clean: 60 | make -C crt clean 61 | make -C kernel clean 62 | make -C apps clean 63 | 64 | -------------------------------------------------------------------------------- /sgos2/api/Makefile: -------------------------------------------------------------------------------- 1 | #sgos api 2 | 3 | CCFLAGS = -g -Werror -I../include -I../../include -fno-builtin -ffreestanding -fleading-underscore 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = api 9 | 10 | CC = gcc $(CCFLAGS) 11 | LDLIB = ld -r $(LDFLAGS) 12 | LD = ld --image-base 0x7FF00000 -shared $(LDFLAGS) 13 | OD = objdump $(ODFLAGS) 14 | WF = ../tools/wf/wf 15 | IMAGE = ../sgos2.img 16 | 17 | OBJECTS = $(patsubst %.c,%.o, $(wildcard *.c)) 18 | 19 | all: $(PROGRAM).dll 20 | 21 | $(PROGRAM).dll: $(OBJECTS) 22 | $(LDLIB) $(OBJECTS) -o$(PROGRAM).a 23 | $(LD) $(OBJECTS) -o$(PROGRAM).dll 24 | $(OD) $(PROGRAM).dll > $(PROGRAM).dmp 25 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 26 | 27 | # Standard Procedures 28 | .c.o: 29 | $(CC) -c -o $@ $< 30 | 31 | clean: 32 | $(RM) $(OBJECTS) 33 | $(RM) $(PROGRAM).a 34 | $(RM) $(PROGRAM).dmp 35 | $(RM) $(PROGRAM).dll -------------------------------------------------------------------------------- /sgos2/api/clock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | uint _GetTickCount() 6 | { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /sgos2/api/ctype.c: -------------------------------------------------------------------------------- 1 | //ported from SGOS1 2 | 3 | #include 4 | 5 | char _ctmp; 6 | unsigned char _ctype[] = {0x00, /* EOF */ 7 | _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ 8 | _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ 9 | _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ 10 | _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */ 11 | _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */ 12 | _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */ 13 | _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */ 14 | _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */ 15 | _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */ 16 | _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */ 17 | _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */ 18 | _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */ 19 | _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */ 20 | _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */ 21 | _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */ 22 | _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */ 23 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */ 24 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */ 25 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 160-175 */ 26 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 176-191 */ 27 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 192-207 */ 28 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 208-223 */ 29 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 224-239 */ 30 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* 240-255 */ 31 | 32 | -------------------------------------------------------------------------------- /sgos2/api/init.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | //从fs里获得用户态线程信息块 6 | ThreadInformation* GetCurrentThreadInformation() 7 | { 8 | size_t addr; 9 | __asm__ __volatile__ ( 10 | "movl %%fs:(0x18), %%eax" 11 | : "=a" (addr) : ); 12 | return (ThreadInformation*) addr; \ 13 | } 14 | 15 | ProcessInformation* GetCurrentProcessInformation() 16 | { 17 | ThreadInformation* ti = GetCurrentThreadInformation(); 18 | if( ti ) 19 | return ti->ProcessInformation; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /sgos2/api/math.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #define RAND_MAX 65536 4 | 5 | static unsigned short rand_seed = 0; 6 | 7 | /* srand: 8 | * Seed initialization routine for rand() replacement. 9 | */ 10 | void srand(int seed) 11 | { 12 | rand_seed = seed; 13 | } 14 | 15 | /* _rand: 16 | * Simple rand() replacement with guaranteed randomness in the lower 16 bits. 17 | */ 18 | int rand(void) 19 | { 20 | rand_seed = (rand_seed + 1) * 1103515245 + 12345; 21 | return rand_seed; 22 | } 23 | 24 | int pow(int a,int b) 25 | { 26 | int rs=1; 27 | while(b--) 28 | rs=rs*a; 29 | return rs; 30 | } 31 | -------------------------------------------------------------------------------- /sgos2/api/printf.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | int printf(const char *fmt, ...) 7 | { 8 | va_list args; 9 | int i; 10 | char buffer[1024]; 11 | va_start(args, fmt); 12 | i=vsprintf(buffer,fmt,args); 13 | Api_Print(buffer, i); 14 | va_end(args); 15 | return i; 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /sgos2/api/service.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static ServiceInformation* siList = (ServiceInformation*)0; 7 | 8 | static void GetServiceListPtr() 9 | { 10 | SystemInformation* si = (SystemInformation*)SysGetSystemInformation(); 11 | if( !si ){ 12 | printf("[Service]Failed to get system information.\n"); 13 | return; 14 | } 15 | siList = si->ServiceList; 16 | } 17 | 18 | int SmNotifyService( uint serviceId, uint eventFlag, const char* name ) 19 | { 20 | uint tid = SmGetServiceThreadByName( "ServiceManager" ); 21 | if( !tid ){ 22 | printf("Failed to get ServiceManager thread.\n"); 23 | return -ERR_UNKNOWN; 24 | } 25 | Message msg = { tid, 0, Service_Notify}; 26 | int result; 27 | msg.Arguments[0] = serviceId; 28 | msg.Arguments[1] = eventFlag; 29 | msg.Arguments[2] = SysGetCurrentThreadId(); 30 | strcpy( (char*)&msg.Arguments[3], name ); 31 | Api_Send( &msg, 0 ); 32 | result = Api_Receive( &msg, 3000 ); 33 | if( result == -ERR_TIMEOUT ) 34 | printf("## Error: ServiceManager no reply\n"); 35 | if( result < 0 ) 36 | return result; 37 | return msg.Code; 38 | } 39 | 40 | int SmRemoveService( uint serviceId ) 41 | { 42 | int tid = SmGetServiceThreadById( serviceId ); 43 | int ret = 0; 44 | SendMessage( tid, Service_Remove, &serviceId, NULL, NULL, NULL, &ret ); 45 | return ret; 46 | } 47 | 48 | uint SmGetServiceThreadById( uint serviceId ) 49 | { 50 | if( !siList ) 51 | GetServiceListPtr(); 52 | if( serviceId< SI_MAX && siList[serviceId].ServiceId ) 53 | return siList[serviceId].ThreadId; 54 | return 0; 55 | } 56 | 57 | uint SmGetServiceThreadByName( const char* name ) 58 | { 59 | int i; 60 | if( !siList ) 61 | GetServiceListPtr(); 62 | for( i=0; i 2 | #include 3 | #include 4 | 5 | 6 | void sleep(unsigned int ms) 7 | { 8 | SysSleepThread(ms); 9 | } 10 | -------------------------------------------------------------------------------- /sgos2/apps/Makefile: -------------------------------------------------------------------------------- 1 | #Makefile for SGOS2:services 2 | 3 | 4 | all: 5 | make -C../crt 6 | make -C../api 7 | make -Cfs 8 | make -Chd 9 | make -Cservicemanager 10 | make -Chello 11 | make -Cwprocess 12 | make -Cstartup 13 | make -Cspeaker 14 | make -Cacpi 15 | make -Ckeyboard 16 | make -Cvesa 17 | make -Ctime 18 | make -Cwkernel 19 | make -Cwadvapi 20 | make -Cmsys 21 | clean: 22 | make -C../crt clean 23 | make -C../api clean 24 | make -Cfs clean 25 | make -Chd clean 26 | make -Cservicemanager clean 27 | make -Chello clean 28 | make -Cwprocess clean 29 | make -Cstartup clean 30 | make -Cspeaker clean 31 | make -Cacpi clean 32 | make -Ckeyboard clean 33 | make -Cvesa clean 34 | make -Ctime clean 35 | make -Cwkernel clean 36 | make -Cwadvapi clean 37 | make -Cmsys clean 38 | 39 | -------------------------------------------------------------------------------- /sgos2/apps/acpi/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = acpi 9 | LDFLAGS += ../../crt/crt.a 10 | LDFLAGS += ../../api/api.a 11 | 12 | CXX = g++ $(CXXFLAGS) 13 | LD = ld $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | LD2 = ../../tools/ld2/ld2 16 | WF = ../../tools/wf/wf 17 | IMAGE = ../../sgos2.img 18 | 19 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 20 | 21 | all: $(PROGRAM).exe 22 | 23 | $(PROGRAM).exe: $(OBJECTS) 24 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 25 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 26 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 27 | 28 | # Standard Procedures 29 | .cpp.o: 30 | $(CXX) -c -o $@ $< 31 | 32 | clean: 33 | $(RM) $(OBJECTS) 34 | $(RM) $(PROGRAM).dmp 35 | $(RM) $(PROGRAM).exe 36 | -------------------------------------------------------------------------------- /sgos2/apps/acpi/acpi.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACPI_H 2 | #define _ACPI_H 3 | 4 | #define Acpi_PowerOff 0x80000000 5 | 6 | int InitializeAcpi(); 7 | void AcpiPowerOff(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /sgos2/apps/acpi/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "acpi.h" 6 | 7 | void parse( Message& msg ) 8 | { 9 | int ret; 10 | switch( msg.Command ){ 11 | case Acpi_PowerOff: 12 | AcpiPowerOff(); 13 | break; 14 | default: 15 | printf("[Acpi]Unknown command: %x\n", msg.Command ); 16 | msg.Code = -ERR_WRONGARG; 17 | } 18 | ReplyMessage( &msg ); 19 | } 20 | 21 | int startService() 22 | { 23 | //接收消息 24 | Message msg; 25 | int id; 26 | //初始化驱动程序 27 | if( InitializeAcpi() < 0 ) 28 | SysExitSpace((uint)-1); 29 | //注册线程名称,其它程序便可以向此服务线程发送消息 30 | id = SmNotifyService( 0, 0, "Acpi" ); 31 | if( id < 0 ){ 32 | printf("[Acpi]add service failed.\n"); 33 | SysExitSpace((uint)-1); 34 | } 35 | printf("[Acpi]Starting Acpi service ...\n"); 36 | for(;;){ 37 | //Pending for messages 38 | int result = WaitMessage(&msg); 39 | if( result < 0 ){ 40 | printf("[Acpi]Failed to receive message: result = %d\n", result ); 41 | continue; 42 | } 43 | msg.Code = 0; 44 | parse( msg ); 45 | } 46 | SmRemoveService( id ); 47 | return 0; 48 | } 49 | 50 | int main() 51 | { 52 | return startService(); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /sgos2/apps/acpi/port.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //IO操作 4 | uchar inbyte( ushort port ) 5 | { 6 | register t_8 ret; 7 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 8 | return ret; 9 | } 10 | 11 | ushort inword( ushort port ) 12 | { 13 | register t_16 ret; 14 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 15 | return ret; 16 | } 17 | 18 | uint indword( uint port ) 19 | { 20 | register t_16 ret; 21 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 22 | return ret; 23 | } 24 | 25 | void outbyte( ushort port, uchar v ) 26 | { 27 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( v ) , "d"( port ) ); 28 | } 29 | 30 | void outword( ushort port, ushort v ) 31 | { 32 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( v ) , "d"( port ) ); 33 | } 34 | 35 | void outdword( ushort port, uint v ) 36 | { 37 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( v ) , "d"( port ) ); 38 | } 39 | -------------------------------------------------------------------------------- /sgos2/apps/acpi/port.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H 2 | #define _IO_H 3 | 4 | //IO操作 5 | uchar inbyte( ushort port ); 6 | ushort inword( ushort port ); 7 | uint indword( uint port ); 8 | void outbyte( ushort port, uchar v ); 9 | void outword( ushort port, ushort v ); 10 | void outdword( ushort port, uint v ); 11 | #endif 12 | -------------------------------------------------------------------------------- /sgos2/apps/apps_vm.txt: -------------------------------------------------------------------------------- 1 | # 动态链接库虚拟内存加载地址登记 2 | # 使用不同的加载地址可以减少重定位消耗的CPU cycles 3 | # DLL Virtual Address Registration 4 | # Used for reducing unnecessary CPU cycles 5 | 6 | 7 | Library LoadAddress Description 8 | 9 | api 0x7FF00000 System Calls & Program Entrance 10 | libc 0x7FE00000 Standard C Library 11 | msg 0x7FD00000 Easy Message Library 12 | system 0x7FC00000 C++ System Library 13 | wkernel 0x7FB00000 Kernel32 Emulator 14 | wadvapi 0x7FA00000 Win32 Advanced API 15 | wntdll 0x7F900000 Win32 NT DLL 16 | msys 0x7F000000 Minimal System 17 | -------------------------------------------------------------------------------- /sgos2/apps/cat.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/apps/cat.exe -------------------------------------------------------------------------------- /sgos2/apps/console/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = console 9 | LDFLAGS += ../../crt/crt.a 10 | LDFLAGS += ../../api/api.a 11 | 12 | CXX = g++ $(CXXFLAGS) 13 | LD = ld $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | LD2 = ../../tools/ld2/ld2 16 | WF = ../../tools/wf/wf 17 | IMAGE = ../../sgos2.img 18 | 19 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 20 | 21 | all: $(PROGRAM).exe 22 | 23 | $(PROGRAM).exe: $(OBJECTS) 24 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 25 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 26 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 27 | 28 | # Standard Procedures 29 | .cpp.o: 30 | $(CXX) -c -o $@ $< 31 | 32 | clean: 33 | $(RM) $(OBJECTS) 34 | $(RM) $(PROGRAM).dmp 35 | $(RM) $(PROGRAM).exe 36 | -------------------------------------------------------------------------------- /sgos2/apps/console/console.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONSOLE_H 2 | #define _CONSOLE_H 3 | 4 | #include 5 | 6 | #define INPUT_BUFFER_SIZE 256 7 | 8 | class SimpleConsole{ 9 | private: 10 | ushort *video; 11 | ushort *buffer; 12 | uint inputQueue[INPUT_BUFFER_SIZE]; 13 | int ifront, irear; 14 | int charWidth; 15 | int width, height; 16 | int cursorX, cursorY; 17 | int charStyle; 18 | 19 | void ScrollUp(); 20 | void NextLine(); 21 | void MoveCursor(); 22 | public: 23 | SimpleConsole(); 24 | ~SimpleConsole(); 25 | int Initialize( int dev, int screen ); 26 | void MoveCursorTo( int x, int y ); 27 | void ClearScreen(); 28 | void OutputChar( int ch ); 29 | void OutputChar( int ch, int x, int y ); 30 | int InputChar(); 31 | void PutKeyChar( int ch ); 32 | void ClearInput(); 33 | void RefreshBuffer(); 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /sgos2/apps/console/port.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //IO操作 4 | uchar inbyte( ushort port ) 5 | { 6 | register t_8 ret; 7 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 8 | return ret; 9 | } 10 | 11 | ushort inword( ushort port ) 12 | { 13 | register t_16 ret; 14 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 15 | return ret; 16 | } 17 | 18 | uint indword( uint port ) 19 | { 20 | register t_16 ret; 21 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 22 | return ret; 23 | } 24 | 25 | void outbyte( ushort port, uchar v ) 26 | { 27 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( v ) , "d"( port ) ); 28 | } 29 | 30 | void outword( ushort port, ushort v ) 31 | { 32 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( v ) , "d"( port ) ); 33 | } 34 | 35 | void outdword( ushort port, uint v ) 36 | { 37 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( v ) , "d"( port ) ); 38 | } 39 | -------------------------------------------------------------------------------- /sgos2/apps/console/port.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H 2 | #define _IO_H 3 | 4 | //IO操作 5 | uchar inbyte( ushort port ); 6 | ushort inword( ushort port ); 7 | uint indword( uint port ); 8 | void outbyte( ushort port, uchar v ); 9 | void outword( ushort port, ushort v ); 10 | void outdword( ushort port, uint v ); 11 | #endif 12 | -------------------------------------------------------------------------------- /sgos2/apps/env.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/apps/env.exe -------------------------------------------------------------------------------- /sgos2/apps/fs/Makefile: -------------------------------------------------------------------------------- 1 | #sgos module 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | CCFLAGS = -g -Werror -I../../include -nostdlib -fno-builtin -fleading-underscore 5 | LDFLAGS = -shared 6 | ODFLAGS = -S 7 | 8 | #here defines the program information 9 | PROGRAM = filesystem 10 | LDFLAGS += ../../crt/crt.a ../../api/api.a 11 | 12 | CXX = g++ $(CXXFLAGS) 13 | CC = gcc $(CCFLAGS) 14 | LD = ld $(LDFLAGS) 15 | OD = objdump $(ODFLAGS) 16 | LD2 = ../../tools/ld2/ld2 17 | WF = ../../tools/wf/wf 18 | IMAGE = ../../sgos2.img 19 | 20 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) \ 21 | $(patsubst %.c,%.o, $(wildcard *.c)) 22 | 23 | all: $(PROGRAM).exe 24 | 25 | $(PROGRAM).exe: $(OBJECTS) 26 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 27 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 28 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 29 | 30 | # Standard Procedures 31 | .cpp.o: 32 | $(CXX) -c -o $@ $< 33 | .c.o: 34 | $(CC) -c -o $@ $< 35 | 36 | clean: 37 | $(RM) $(OBJECTS) 38 | $(RM) $(PROGRAM).dmp 39 | $(RM) $(PROGRAM).exe 40 | -------------------------------------------------------------------------------- /sgos2/apps/fs/buffer.h: -------------------------------------------------------------------------------- 1 | #ifndef _BUFFER_H_ 2 | #define _BUFFER_H_ 3 | 4 | #include 5 | #include 6 | #include "fsservice.h" 7 | 8 | // 缓冲区块结构 9 | typedef struct BUFFER{ 10 | struct BUFFER *next, *prev; 11 | // 指向所属设备 12 | device_t *device; 13 | // 指向块号 14 | uint block; 15 | // 块数据 16 | uchar *data; 17 | // 是否修改过 18 | uchar dirty; 19 | // 引用计数 20 | ushort reference; 21 | // 创建时间,依次判断该缓冲区块是否抛弃 22 | time_t ctime; 23 | }buffer_t; 24 | 25 | // 磁盘缓冲区初始化 26 | EXTERN int buffer_init( size_t buf_mem ); 27 | // 获取缓冲区 28 | EXTERN buffer_t* buffer_get( device_t* dev, size_t block ); 29 | // 释放缓冲区 30 | EXTERN void buffer_put( buffer_t* buf ); 31 | // 写入所有修改过的缓冲区 32 | EXTERN void buffer_sync(); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /sgos2/apps/fs/device.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/apps/fs/device.h -------------------------------------------------------------------------------- /sgos2/apps/fs/fsmanager.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "buffer.h" 7 | #include "fsservice.h" 8 | 9 | // 10 | #define MAX_FS 16 11 | fs_t * fsList[MAX_FS]; 12 | int fsCount = 0; 13 | 14 | //安装一个文件系统类型 15 | int fs_register( fs_t* fs ) 16 | { 17 | if( !fs ) 18 | return -ERR_WRONGARG; 19 | if( fsCount >= MAX_FS ){ 20 | printf("[vfs]too many fses.\n"); 21 | return -ERR_NOMEM; 22 | } 23 | fsList[fsCount++] = fs; 24 | return 0; 25 | } 26 | 27 | //卸载文件系统类型 28 | int fs_unregister( fs_t* fs ) 29 | { 30 | for(int i=0; idevFile = falloc(); 43 | if( root ){ 44 | ++ root->reference; 45 | flinkto( dev->devFile, root ); 46 | } 47 | // 尝试安装这个文件系统 48 | if( fs->setup && fs->setup( dev->devFile, dev )==0 ){ 49 | //安装成功。 50 | dev->fs = fs; 51 | return 1; 52 | } 53 | ffree( dev->devFile ); 54 | dev->devFile = 0; 55 | return 0; 56 | } 57 | 58 | //探测并安装文件系统 59 | fs_t* fs_detect( device_t* dev ) 60 | { 61 | for(int i=0; ifs; 64 | return 0; 65 | } 66 | 67 | //文件系统服务初始化 68 | int fs_init() 69 | { 70 | fsCount = 0; 71 | memset( &fsList, 0 , sizeof(fsList) ); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /sgos2/apps/fs/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include "buffer.h" 6 | #include "fsservice.h" 7 | 8 | 9 | int main() 10 | { 11 | int ret; 12 | //磁盘缓冲区初始化 13 | ret = buffer_init( (1<<20) ); 14 | if( ret<0 ){ 15 | printf("[vfs]buffer_init failed :%d\n", ret ); 16 | return ret; 17 | } 18 | //设备服务初始化 19 | ret = device_init(); 20 | if( ret<0 ){ 21 | printf("[vfs]device_init failed :%d\n", ret ); 22 | return ret; 23 | } 24 | //动态文件系统装载服务初始化 25 | ret = fs_init(); 26 | if( ret<0 ){ 27 | printf("[vfs]fs_init failed :%d\n", ret ); 28 | return ret; 29 | } 30 | //虚拟文件系统服务初始化, install rootfs!! 31 | ret = vfs_init(); 32 | if( ret<0 ){ 33 | printf("[vfs]rootfs_init failed :%d\n", ret ); 34 | return ret; 35 | } 36 | extern void device_startService(); 37 | device_startService(); 38 | // 39 | SysExitThread(0); 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /sgos2/apps/fs/unicode.c: -------------------------------------------------------------------------------- 1 | //Huang Guan 2 | //Unicode 3 | //2007-8-16 22:44 Created 4 | //gdxxhg@gmail.com 5 | 6 | #include 7 | #include 8 | #include "unicode.h" 9 | 10 | #define UNICODE_DATABASE "unicode.db" 11 | static char* unicodeDB; 12 | 13 | static int LoadUnicodeDatabase(const char* fileName) 14 | { 15 | int len; 16 | unicodeDB = (char*)0; 17 | return 0; 18 | } 19 | 20 | int unicode_init() 21 | { 22 | if( !unicodeDB ) 23 | LoadUnicodeDatabase( UNICODE_DATABASE ); 24 | return 0; 25 | } 26 | 27 | int unicode_decode( wchar_t* input, int inputLen, char* output, int outputLen ) 28 | { 29 | int i, j; 30 | for( i=0, j=0; i+1 5 | 6 | typedef ushort wchar_t; 7 | 8 | int unicode_init(); 9 | int unicode_decode( wchar_t* input, int inputLen, char* output, int outputLen ); 10 | int unicode_encode( unsigned char* input, int inputLen, wchar_t* output, int outputLen ); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /sgos2/apps/hd/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = harddisk 9 | LDFLAGS += ../../crt/crt.a 10 | LDFLAGS += ../../api/api.a 11 | 12 | CXX = g++ $(CXXFLAGS) 13 | LD = ld $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | LD2 = ../../tools/ld2/ld2 16 | WF = ../../tools/wf/wf 17 | IMAGE = ../../sgos2.img 18 | 19 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 20 | 21 | all: $(PROGRAM).exe 22 | 23 | $(PROGRAM).exe: $(OBJECTS) 24 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 25 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 26 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 27 | 28 | # Standard Procedures 29 | .cpp.o: 30 | $(CXX) -c -o $@ $< 31 | 32 | clean: 33 | $(RM) $(OBJECTS) 34 | $(RM) $(PROGRAM).dmp 35 | $(RM) $(PROGRAM).exe 36 | -------------------------------------------------------------------------------- /sgos2/apps/hd/hd.h: -------------------------------------------------------------------------------- 1 | //sgos2 2 | #ifndef _HD_H 3 | #define _HD_H 4 | 5 | #include 6 | 7 | //硬盘端口,状态码及分区表 8 | #define HD_DATA 0x1f0 9 | #define HD_CMD 0x3f6 10 | #define HD_STATUS 0x1f7 11 | #define HD_COMMAND HD_STATUS 12 | //状态码 13 | #define STAT_ERR 0x01 14 | #define STAT_INDEX 0x02 15 | #define STAT_ECC 0x04 16 | #define STAT_DRQ 0x08 17 | #define STAT_SEEK 0x10 18 | #define STAT_WRERR 0x20 19 | #define STAT_READY 0x40 20 | #define STAT_BUSY 0x80 21 | //命令 22 | #define WIN_RESTORE 0x10 23 | #define WIN_READ 0x20 24 | #define WIN_WRITE 0x30 25 | #define WIN_VERIFY 0x40 26 | #define WIN_FORMAT 0x50 27 | #define WIN_INIT 0x60 28 | #define WIN_SEEK 0x70 29 | #define WIN_DIAGNOSE 0x90 30 | #define WIN_SPECIFY 0x91 31 | 32 | 33 | //硬盘分区表 34 | typedef struct PARTITION 35 | { 36 | t_8 bootable; 37 | t_8 head; 38 | t_16 sector_cylinder; /* 0-5 bit -- sector,6-15 bit -- cylinder */ 39 | t_8 sysID; 40 | t_8 endHead; 41 | t_16 endSectorCylinder; /* 0-5 bit -- sector,6-15 bit -- cylinder */ 42 | t_32 skipSectors; 43 | t_32 allSectors; 44 | }__attribute__((packed)) PARTITION; 45 | 46 | 47 | //主设备号 3 48 | //RM to LINEAR 从带段和偏移式的地址转换为内核线性地址 49 | #define R2L(addr) ( (((uint)addr & 0xFFFF0000) >> 12) + ((t_32)addr & 0xFFFF)) 50 | 51 | #define MAX_HD 2 //最多两个硬盘 52 | //硬盘参数表 53 | typedef struct HD_PARAMS{ 54 | t_16 cyls; //柱面数 55 | t_8 heads; //磁头数 56 | t_16 null0; // 57 | t_16 WPC; //预补偿柱面号 58 | t_8 null1; // 59 | t_8 controlByte; //控制字节 60 | t_8 null2; 61 | t_8 null3; 62 | t_8 null4; 63 | t_16 lzone; //磁头登陆柱面号 64 | t_8 SPT; //每次道扇区数 65 | t_8 null5; 66 | }__attribute__((packed)) HD_PARAMS; 67 | 68 | //读端口数据 69 | #define READ_WORDS( port, buf, n) \ 70 | __asm__("cld;rep;insw"::"d"(port),"D"(buf),"c"(n)) 71 | //写端口数据,n个16位数据 72 | #define WRITE_WORDS( port, buf, n) \ 73 | asm("cld;rep;outsw"::"d"(port),"S"(buf),"c"(n)) 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /sgos2/apps/hd/port.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //IO操作 4 | uchar inbyte( ushort port ) 5 | { 6 | register t_8 ret; 7 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 8 | return ret; 9 | } 10 | 11 | ushort inword( ushort port ) 12 | { 13 | register t_16 ret; 14 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 15 | return ret; 16 | } 17 | 18 | uint indword( uint port ) 19 | { 20 | register t_16 ret; 21 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 22 | return ret; 23 | } 24 | 25 | void outbyte( ushort port, uchar v ) 26 | { 27 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( v ) , "d"( port ) ); 28 | } 29 | 30 | void outword( ushort port, ushort v ) 31 | { 32 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( v ) , "d"( port ) ); 33 | } 34 | 35 | void outdword( ushort port, uint v ) 36 | { 37 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( v ) , "d"( port ) ); 38 | } 39 | -------------------------------------------------------------------------------- /sgos2/apps/hd/port.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H 2 | #define _IO_H 3 | 4 | //IO操作 5 | uchar inbyte( ushort port ); 6 | ushort inword( ushort port ); 7 | uint indword( uint port ); 8 | void outbyte( ushort port, uchar v ); 9 | void outword( ushort port, ushort v ); 10 | void outdword( ushort port, uint v ); 11 | #endif 12 | -------------------------------------------------------------------------------- /sgos2/apps/hello/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = ../../api/api.dll 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = hello 9 | 10 | CXX = g++ $(CXXFLAGS) 11 | LD = ld $(LDFLAGS) 12 | OD = objdump $(ODFLAGS) 13 | LD2 = ../../tools/ld2/ld2 14 | WF = ../../tools/wf/wf 15 | IMAGE = ../../sgos2.img 16 | 17 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) \ 18 | ../../crt/crt.a 19 | 20 | all: $(PROGRAM).exe 21 | 22 | $(PROGRAM).exe: $(OBJECTS) 23 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 24 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 25 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 26 | 27 | # Standard Procedures 28 | .c.o: 29 | $(CXX) -c -o $@ $< 30 | 31 | clean: 32 | $(RM) $(OBJECTS) 33 | $(RM) $(PROGRAM).dmp 34 | $(RM) $(PROGRAM).exe 35 | -------------------------------------------------------------------------------- /sgos2/apps/hello/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void beep() 6 | { 7 | Message msg; 8 | int ret; 9 | uint tid = SmGetServiceThreadByName("Speaker"); 10 | if( tid == 0 ){ 11 | printf("[hello] Speaker service not found.\n"); 12 | return; 13 | } 14 | msg.ThreadId = tid; 15 | msg.Command = 3; 16 | ret = Api_Send(&msg, 0); 17 | if( ret < 0 ){ 18 | printf("[hello] Failed to send command to service.\n"); 19 | return ; 20 | } 21 | Api_Receive(&msg, 3000); 22 | //Omit the result. 23 | } 24 | 25 | int main() 26 | { 27 | printf("[hello]Program started.\n"); 28 | //Wait for Speaker Service to start. 29 | SysSleepThread(1000); 30 | beep(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /sgos2/apps/keyboard/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = keyboard 9 | LDFLAGS += ../../crt/crt.a 10 | LDFLAGS += ../../api/api.a 11 | 12 | CXX = g++ $(CXXFLAGS) 13 | LD = ld $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | LD2 = ../../tools/ld2/ld2 16 | WF = ../../tools/wf/wf 17 | IMAGE = ../../sgos2.img 18 | 19 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 20 | 21 | all: $(PROGRAM).exe 22 | 23 | $(PROGRAM).exe: $(OBJECTS) 24 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 25 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 26 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 27 | 28 | # Standard Procedures 29 | .cpp.o: 30 | $(CXX) -c -o $@ $< 31 | 32 | clean: 33 | $(RM) $(OBJECTS) 34 | $(RM) $(PROGRAM).dmp 35 | $(RM) $(PROGRAM).exe 36 | -------------------------------------------------------------------------------- /sgos2/apps/keyboard/dispatch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "keyboard.h" 6 | 7 | #define MAX_HANDLER 16 8 | 9 | struct EventHandler{ 10 | uint threadId; 11 | uint mode; 12 | uint command; 13 | }; 14 | 15 | 16 | static EventHandler handlerList[MAX_HANDLER]; 17 | static int handlerListCount=0; 18 | 19 | 20 | static int SendEvent( uint tid, uint cmd, uint keycode, uint ascii, uint flag ) 21 | { 22 | Message msg; 23 | memset( &msg, 0, sizeof(msg) ); 24 | msg.ThreadId = tid; 25 | msg.Command = cmd; 26 | msg.Arguments[0] = keycode; 27 | msg.Arguments[1] = ascii; 28 | msg.Arguments[2] = flag; 29 | return Api_Send( &msg, 0 ); 30 | } 31 | 32 | void DispatchKeyboardMessage( uint keycode, uint ascii, uint flag ) 33 | { 34 | int i; 35 | for( i=0; i 2 | #include 3 | #include 4 | #include 5 | #include "keyboard.h" 6 | 7 | void parse( Message& msg ) 8 | { 9 | int ret; 10 | switch( msg.Command ){ 11 | case System_Interrupt: 12 | HandleKeyboardInterrupt( msg.Arguments[0] ); 13 | break; 14 | case Keyboard_SetHandler: 15 | if( msg.Arguments[0]==1 ) 16 | AddHandler( msg.ThreadId, msg.Arguments[1], msg.Arguments[2] ); 17 | else 18 | DelHandler( msg.ThreadId ); 19 | break; 20 | case Keyboard_Configure: 21 | printf("[Keyboard] Configuration is not implemented.\n"); 22 | break; 23 | default: 24 | printf("[Keyboard]Unknown command: %x\n", msg.Command ); 25 | msg.Code = -ERR_WRONGARG; 26 | } 27 | } 28 | 29 | int startService() 30 | { 31 | //接收消息 32 | Message msg; 33 | int id; 34 | //注册线程名称,其它程序便可以向此服务线程发送消息 35 | id = SmNotifyService( 0, 0, "Keyboard" ); 36 | if( id < 0 ){ 37 | printf("[Keyboard]add service failed.\n"); 38 | SysExitSpace((uint)-1); 39 | } 40 | //初始化驱动程序 41 | InitializeKeyboard(); 42 | printf("[Keyboard]Starting Keyboard service ...\n"); 43 | for(;;){ 44 | //Pending for messages 45 | int result = WaitMessage(&msg); 46 | if( result < 0 ){ 47 | printf("[Keyboard]Failed to receive message: result = %d\n", result ); 48 | continue; 49 | } 50 | msg.Code = 0; 51 | parse( msg ); 52 | } 53 | SmRemoveService( id ); 54 | return 0; 55 | } 56 | 57 | int main() 58 | { 59 | return startService(); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /sgos2/apps/keyboard/port.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //IO操作 4 | uchar inbyte( ushort port ) 5 | { 6 | register t_8 ret; 7 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 8 | return ret; 9 | } 10 | 11 | ushort inword( ushort port ) 12 | { 13 | register t_16 ret; 14 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 15 | return ret; 16 | } 17 | 18 | uint indword( uint port ) 19 | { 20 | register t_16 ret; 21 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 22 | return ret; 23 | } 24 | 25 | void outbyte( ushort port, uchar v ) 26 | { 27 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( v ) , "d"( port ) ); 28 | } 29 | 30 | void outword( ushort port, ushort v ) 31 | { 32 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( v ) , "d"( port ) ); 33 | } 34 | 35 | void outdword( ushort port, uint v ) 36 | { 37 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( v ) , "d"( port ) ); 38 | } 39 | -------------------------------------------------------------------------------- /sgos2/apps/keyboard/port.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H 2 | #define _IO_H 3 | 4 | //IO操作 5 | uchar inbyte( ushort port ); 6 | ushort inword( ushort port ); 7 | uint indword( uint port ); 8 | void outbyte( ushort port, uchar v ); 9 | void outword( ushort port, ushort v ); 10 | void outdword( ushort port, uint v ); 11 | #endif 12 | -------------------------------------------------------------------------------- /sgos2/apps/msg/Makefile: -------------------------------------------------------------------------------- 1 | #sgos api 2 | 3 | CCFLAGS = -g -Werror -I../include -I../../include -fno-builtin -ffreestanding -fleading-underscore 4 | LDFLAGS = -shared 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = msg 9 | LDFLAGS += --image-base 0x7FD00000 ../libc/libc.dll ../api/api.dll 10 | 11 | CC = gcc $(CCFLAGS) 12 | LD = ld $(LDFLAGS) 13 | OD = objdump $(ODFLAGS) 14 | LD2 = ../../tools/ld2/ld2 15 | WF = ../../tools/wf/wf 16 | IMAGE = ../../sgos2.img 17 | 18 | OBJECTS = $(patsubst %.c,%.o, $(wildcard *.c)) 19 | 20 | all: $(PROGRAM).bxm 21 | 22 | $(PROGRAM).bxm: $(OBJECTS) 23 | $(LD) $(OBJECTS) -o$(PROGRAM).dll 24 | $(OD) $(PROGRAM).dll > $(PROGRAM).dmp 25 | $(LD2) $(PROGRAM).dll $@ 26 | $(WF) $(IMAGE) -src $@ -dest sgos2/$@ 27 | 28 | # Standard Procedures 29 | .c.o: 30 | $(CC) -c -o $@ $< 31 | 32 | clean: 33 | $(RM) $(OBJECTS) 34 | $(RM) $(PROGRAM).bxm 35 | $(RM) $(PROGRAM).dmp 36 | $(RM) $(PROGRAM).dll -------------------------------------------------------------------------------- /sgos2/apps/msg/name.c: -------------------------------------------------------------------------------- 1 | //Namespace 2 | 3 | #include 4 | #include 5 | 6 | int create_name( uint thread, const char* name ) 7 | { 8 | return sys_namespace_create( thread, (char*)name ); 9 | } 10 | 11 | int delete_name( uint thread, const char* name ) 12 | { 13 | return sys_namespace_delete( thread, (char*)name ); 14 | } 15 | 16 | uint match_name( const char* name ) 17 | { 18 | return sys_namespace_match( (char*)name ); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /sgos2/apps/msys/Makefile: -------------------------------------------------------------------------------- 1 | #sgos api 2 | 3 | CCFLAGS = -g -Werror -I../include -I../../include -fno-builtin -ffreestanding -fleading-underscore 4 | LDFLAGS = ../../api/api.dll 5 | LDFLAGS += newlib/*.o 6 | ODFLAGS = -S 7 | 8 | #here defines the program information 9 | PROGRAM = msys-1.0 10 | 11 | CC = gcc $(CCFLAGS) 12 | LDLIB = ld -r $(LDFLAGS) 13 | LD = ld --image-base 0x7F000000 -shared $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | WF = ../../tools/wf/wf 16 | IMAGE = ../../sgos2.img 17 | 18 | OBJECTS = $(patsubst %.c,%.o, $(wildcard *.c)) 19 | 20 | all: $(PROGRAM).dll 21 | 22 | $(PROGRAM).dll: $(OBJECTS) 23 | $(LD) $(OBJECTS) -o$(PROGRAM).dll 24 | $(OD) $(PROGRAM).dll > $(PROGRAM).dmp 25 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 26 | 27 | # Standard Procedures 28 | .c.o: 29 | $(CC) -c -o $@ $< 30 | 31 | clean: 32 | $(RM) $(OBJECTS) 33 | $(RM) $(PROGRAM).dmp 34 | $(RM) $(PROGRAM).dll 35 | -------------------------------------------------------------------------------- /sgos2/apps/msys/argv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "debug.h" 3 | 4 | void build_argv (char *cmd, char ***argv_ptr, int *argc, char sep) 5 | { 6 | if( !cmd ){ 7 | DBG("cmd==NULL"); 8 | return; 9 | } 10 | char* p, *q; 11 | *argc = 1; 12 | int i=0; 13 | for(p=cmd; *p; p++ ) 14 | if( *p == sep ) 15 | (*argc)++; 16 | *argv_ptr = (char**)malloc( sizeof(char*) * (*argc+1) ); 17 | if( !*argv_ptr ){ 18 | DBG("cannot allocate memory. size=0x%x", sizeof(char*) * (*argc+1) ); 19 | return; 20 | } 21 | for(p=q=cmd; ; p++ ){ 22 | if( *p==sep || !*p ){ 23 | int len = (int)p-(int)q; 24 | (*argv_ptr)[i] = (char*)malloc( len + 1 ); 25 | if( (*argv_ptr)[i] == NULL ) 26 | return; 27 | memcpy( (*argv_ptr)[i], q, len ); 28 | (*argv_ptr)[i][len] = '\0'; 29 | i++; 30 | if( !*p ) 31 | break; 32 | q = p+1; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /sgos2/apps/msys/crt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "cygwin.h" 5 | #include "debug.h" 6 | #include "reent.h" 7 | 8 | /* Pointer into application's static data */ 9 | struct per_process __cygwin_user_data; 10 | #define user_data (&__cygwin_user_data) 11 | 12 | char*** main_environ; 13 | 14 | struct _reent reent_data = _REENT_INIT(reent_data); 15 | 16 | void __main (void) 17 | { 18 | } 19 | 20 | uint cygwin_internal( int type ) 21 | { 22 | switch( type ){ 23 | case CW_USER_DATA: 24 | memset( user_data, 0, sizeof( __cygwin_user_data ) ); 25 | if( user_data->resourcelocks==NULL ) 26 | user_data->resourcelocks = (void*)0x26666666; 27 | if( user_data->threadinterface==NULL ) 28 | user_data->threadinterface = (void*)0x36666666; 29 | if( user_data->impure_ptr==NULL ) 30 | user_data->impure_ptr = (void*)&reent_data; 31 | DBG("return : %X" , user_data ); 32 | return (uint)user_data; 33 | } 34 | } 35 | 36 | 37 | void dll_crt0__FP11per_process(struct per_process* u) 38 | { 39 | ProcessInformation* pi = GetCurrentProcessInformation(); 40 | int argc, envc; 41 | char** argv, **envv; 42 | char* cmd; 43 | u = user_data; 44 | main_environ = user_data->envptr; 45 | //init environ 46 | build_argv( pi->EnvironmentVariables, &envv, &envc, ':' ); 47 | *main_environ = envv; 48 | //init command arguments 49 | cmd = pi->CommandLine; 50 | DBG("cmdline: %s", cmd ); 51 | build_argv( cmd, &argv, &argc, ' ' ); 52 | u->main( argc, argv, *main_environ ); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /sgos2/apps/msys/debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * debug.c 3 | * 4 | * SGOS debugger. 5 | * 6 | * Copyright (C) 2008 Huang Guan 7 | * 8 | * 2008-01-31 Created. 9 | * 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | void print_error(char* file, char* function, int line, const char *fmt, ...) 18 | { 19 | va_list args; 20 | char printbuf[1024]; 21 | int i; 22 | va_start(args, fmt); 23 | i=vsprintf( printbuf, fmt, args ); 24 | printbuf[i] = 0; 25 | va_end(args); 26 | Api_Print( "[", 1); 27 | Api_Print( file, strlen(file) ); 28 | Api_Print( "]", 1); 29 | Api_Print( function, strlen(function) ); 30 | Api_Print( ": ", 2); 31 | Api_Print( printbuf, i ); 32 | Api_Print( "\n", 1); 33 | } 34 | 35 | void ASSERT(int a) 36 | { 37 | if( !a ){ 38 | Api_Print("Assertion failed at somewhere.\n", 100); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /sgos2/apps/msys/debug.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEBUG_H 2 | #define _DEBUG_H 3 | 4 | #include 5 | #include 6 | 7 | //#define RELEASE 8 | 9 | #ifndef RELEASE 10 | #define DBG(args ...) \ 11 | print_error( __FILE__, (char*)__func__, __LINE__, ##args ) 12 | #else 13 | #define DBG(args ...) 14 | //#define DBG printf 15 | #endif 16 | #define MSG printf 17 | void print_error(char* file, char* function, int line, const char *fmt, ...); 18 | 19 | #define NOT_IMPLEMENTED() DBG("## Not implemented.") 20 | 21 | #endif //_DEBUG_H 22 | 23 | -------------------------------------------------------------------------------- /sgos2/apps/msys/environ.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "debug.h" 4 | #include "cygwin.h" 5 | 6 | -------------------------------------------------------------------------------- /sgos2/apps/msys/exec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int execve(const char * filename,char * const argv[ ],char * const envp[ ]) 7 | { 8 | int pid, i; 9 | char* e=NULL, *t =NULL; 10 | t = (char*)malloc( PAGE_SIZE ); 11 | //FIXME: overflow... 12 | strcpy( t, filename ); 13 | for( i=0; argv && argv[i]; i++ ){ 14 | strcat( t, " " ); 15 | strcat( t, argv[i] ); 16 | } 17 | e = (char*)malloc( ENVIRONMENT_STRING_SIZE ); 18 | if( !e ) 19 | goto bed; 20 | e[0] = '\0'; 21 | for( i=0; envp && envp[i]; i++ ){ 22 | strcat( e, envp[i] ); 23 | strcat( t, ";" ); 24 | } 25 | if( PsCreateProcess( t, e, &pid ) < 0 ) 26 | goto bed; 27 | return pid; 28 | bed: 29 | if( e ) free( e ); 30 | if( t ) free( t ); 31 | return -ERR_UNKNOWN; 32 | } 33 | 34 | int execvp(const char *file ,char * const argv []) 35 | { 36 | return execve( file, argv, NULL ); 37 | } 38 | -------------------------------------------------------------------------------- /sgos2/apps/msys/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | void _exit() 6 | { 7 | SysExitThread(0); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /sgos2/apps/msys/file.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | #define _FREAD 0x0001 /* read enabled */ 6 | #define _FWRITE 0x0002 /* write enabled */ 7 | #define _FAPPEND 0x0008 /* append (writes guaranteed at the end) */ 8 | #define _FCREAT 0x0200 /* open with file create */ 9 | #define _FTRUNC 0x0400 /* open with truncation */ 10 | #define _FEXCL 0x0800 /* error on open if file exists */ 11 | 12 | #define O_RDONLY 0 /* +1 == FREAD */ 13 | #define O_WRONLY 1 /* +1 == FWRITE */ 14 | #define O_RDWR 2 /* +1 == FREAD|FWRITE */ 15 | #define O_APPEND _FAPPEND 16 | #define O_CREAT _FCREAT 17 | #define O_TRUNC _FTRUNC 18 | #define O_EXCL _FEXCL 19 | void close( int fd ) 20 | { 21 | DBG("close %d", fd ); 22 | FsCloseFile( (FILEBUF*)fd ); 23 | } 24 | 25 | long lseek( int fd, long offset, int fromwhere) 26 | { 27 | DBG("lseek %x offset:%d", fd, offset ); 28 | return FsSetFilePointer( (FILEBUF*)fd, offset, fromwhere ); 29 | } 30 | 31 | int open( const char* path, int oflag, int mode ) 32 | { 33 | int myflag=0, mymode=0; 34 | DBG("open %s oflag:%x", path, oflag ); 35 | if( oflag& _FREAD ) 36 | mymode |= FILE_READ; 37 | if( oflag& _FWRITE ) 38 | mymode |= FILE_WRITE; 39 | if( oflag& _FAPPEND ) 40 | myflag |= FILE_FLAG_APPEND; 41 | if( oflag& _FCREAT ) 42 | myflag |= FILE_FLAG_CREATE; 43 | 44 | FILEBUF* fb = FsOpenFile( path, myflag, mymode ); 45 | if( fb ) 46 | return (int)fb; 47 | return -1; 48 | } 49 | 50 | 51 | int _write(int fd, void *buf, int nbyte) 52 | { 53 | DBG("_write %x %d", fd, nbyte ); 54 | if( fd==STDOUT ) 55 | return Api_Print( buf, nbyte ); 56 | return FsWriteFile( (FILEBUF*)fd, buf, nbyte ); 57 | } 58 | 59 | int write(int fd, void *buf, int nbyte) 60 | { 61 | return _write( fd, buf, nbyte ); 62 | } 63 | 64 | int read(int fd, void *buf, int nbyte) 65 | { 66 | return _read( fd, buf, nbyte ); 67 | } 68 | 69 | int _read(int fd, void *buf, int nbyte) 70 | { 71 | DBG("read bytes: %d", nbyte ); 72 | return FsReadFile( (FILEBUF*)fd, buf, nbyte ); 73 | } 74 | 75 | -------------------------------------------------------------------------------- /sgos2/apps/msys/float.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | #define errno (*__errno()) 6 | extern int* __errno (void); 7 | extern double BIGX; 8 | extern double SMALLX; 9 | double BIGX; 10 | double SMALLX; 11 | typedef const union 12 | { 13 | long l; 14 | float f; 15 | } ufloat; 16 | typedef union 17 | { 18 | float value; 19 | uint32_t word; 20 | } ieee_float_shape_type; 21 | ufloat z_infinity_f; 22 | 23 | /* Get a 32 bit int from a float. */ 24 | 25 | #define GET_FLOAT_WORD(i,d) \ 26 | do { \ 27 | ieee_float_shape_type gf_u; \ 28 | gf_u.value = (d); \ 29 | (i) = gf_u.word; \ 30 | } while (0) 31 | 32 | /* Set a float from a 32 bit int. */ 33 | #define SET_FLOAT_WORD(d,i) \ 34 | do { \ 35 | ieee_float_shape_type sf_u; \ 36 | sf_u.word = (i); \ 37 | (d) = sf_u.value; \ 38 | } while (0) 39 | 40 | float infinityf() 41 | { 42 | float x; 43 | SET_FLOAT_WORD(x,0x7f800000); 44 | return x; 45 | } 46 | 47 | double infinity() 48 | { 49 | return (double) infinityf(); 50 | } 51 | 52 | float powf (float x, float y) 53 | { 54 | NOT_IMPLEMENTED(); 55 | return x; 56 | } 57 | 58 | #ifdef _DOUBLE_IS_32BITS 59 | 60 | double pow (double x, double y) 61 | { 62 | NOT_IMPLEMENTED(); 63 | return (double) powf ((float) x, (float) y); 64 | } 65 | 66 | #endif /* defined(_DOUBLE_IS_32BITS) */ 67 | -------------------------------------------------------------------------------- /sgos2/apps/msys/fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | int fork() 6 | { 7 | NOT_IMPLEMENTED(); 8 | } 9 | -------------------------------------------------------------------------------- /sgos2/apps/msys/fstat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "debug.h" 5 | 6 | int fstat(int fd, struct _stat *buf) 7 | { 8 | FILEBUF* fb; 9 | DBG("fd=%d", fd ); 10 | memset( buf, 0, sizeof(*buf) ); 11 | if( fd == STDOUT ) 12 | return 0; 13 | fb = (FILEBUF*) fd; 14 | 15 | buf->st_mode |= S_IRWXU; // for fat system 16 | buf->st_mode |= _IFMT; //is a file. 17 | if( fb->attr & FILE_ATTR_DIR ) 18 | buf->st_mode |= _IFDIR; //is a directory 19 | else 20 | buf->st_mode |= _IFREG; //regular file. 21 | buf->st_mode |= _IFBLK | _IFCHR; //char dev & block dev 22 | 23 | buf->st_ino = fd; 24 | buf->st_dev = 0; 25 | buf->st_rdev = 0; 26 | buf->st_nlink = 1; 27 | buf->st_uid = 0; 28 | buf->st_gid = 0; 29 | buf->st_size = fb->filesize; 30 | buf->st_blksize = PAGE_SIZE; 31 | buf->st_blocks = buf->st_size%buf->st_blksize==0 ? buf->st_size/buf->st_blksize : buf->st_size/buf->st_blksize+1; 32 | 33 | buf->st_ctime = fb->ctime; 34 | buf->st_mtime = fb->mtime; 35 | buf->st_atime = fb->mtime; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /sgos2/apps/msys/getpid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | int getpid() 6 | { 7 | return PsGetCurrentProcessId(); 8 | } 9 | -------------------------------------------------------------------------------- /sgos2/apps/msys/getpwd.c: -------------------------------------------------------------------------------- 1 | #include "debug.h" 2 | 3 | char *getcwd(char *buf, int n) 4 | { 5 | DBG("buf=%x", buf ); 6 | return "/"; 7 | } 8 | -------------------------------------------------------------------------------- /sgos2/apps/msys/handle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | #include "handle.h" 5 | 6 | #define MAX_HANDLES_PER_PROCESS 1024 7 | #define HANDLE_MAGIC 0xFBFC1314 8 | 9 | typedef struct HandleInformationSet{ 10 | uint magic; 11 | struct HandleInformationSet* prev, *next; 12 | int type; 13 | uint data; 14 | uint mask; 15 | uint flag; 16 | }HandleInformation; 17 | 18 | 19 | uint create_handle( int type, uint data ) 20 | { 21 | ProcessInformation* pi = GetCurrentProcessInformation(); 22 | if( pi ){ 23 | HandleInformation* hi = (HandleInformation*)malloc( sizeof(HandleInformation) ); 24 | if( hi==NULL ) 25 | return 0; 26 | hi->type = type; 27 | hi->data = data; 28 | hi->magic = HANDLE_MAGIC; 29 | hi->mask = hi->flag = 0; 30 | //## Lock here 31 | hi->next = pi->HandleSet; 32 | hi->prev = 0; 33 | if( pi->HandleSet ) 34 | ((HandleInformation*)pi->HandleSet)->prev = hi; 35 | pi->HandleSet = hi; 36 | //## Unlock 37 | return (uint)hi; 38 | } 39 | return 0; 40 | } 41 | 42 | uint get_handle_type(uint h ) 43 | { 44 | switch( h ){ 45 | case STDIN: 46 | case STDOUT: 47 | case STDERR: 48 | return HANDLE_CHAR; 49 | } 50 | HandleInformation* hi = (HandleInformation*)h; 51 | if( !hi || hi->magic != HANDLE_MAGIC ) 52 | return HANDLE_UNKNOWN; 53 | return hi->type; 54 | } 55 | 56 | void close_handle( uint h ) 57 | { 58 | ProcessInformation* pi = GetCurrentProcessInformation(); 59 | HandleInformation* hi = (HandleInformation*)h; 60 | if( !hi || hi->magic != HANDLE_MAGIC ) 61 | return ; 62 | switch( hi->type ){ 63 | case HANDLE_FILE: 64 | FsCloseFile( (void*)hi->data ); 65 | break; 66 | case HANDLE_CHAR: 67 | break; 68 | default: 69 | DBG("Unknown handle type."); 70 | } 71 | //## Lock here 72 | if( hi->next ) 73 | hi->next->prev = hi->prev; 74 | if( hi->prev ) 75 | hi->prev->next = hi->next; 76 | else 77 | pi->HandleSet = hi->next; 78 | //## Unlock here 79 | } 80 | 81 | uint get_handle_data( uint h ) 82 | { 83 | HandleInformation* hi = (HandleInformation*)h; 84 | if( !hi || hi->magic != HANDLE_MAGIC ) 85 | return ; 86 | return hi->data; 87 | } 88 | 89 | -------------------------------------------------------------------------------- /sgos2/apps/msys/handle.h: -------------------------------------------------------------------------------- 1 | #ifndef _HANDLE_ 2 | 3 | #include 4 | 5 | enum HANDLE_TYPE{ 6 | HANDLE_UNKNOWN = 0, 7 | HANDLE_DISK = 0x0001, 8 | HANDLE_CHAR = 0x0002, 9 | HANDLE_PIPE = 0x0003, 10 | HANDLE_MODULE = 0x0004, 11 | HANDLE_FILE, 12 | HANDLE_PROCESS, 13 | HANDLE_THREAD, 14 | HANDLE_EVENT 15 | }; 16 | 17 | uint create_handle( int type, uint data ); 18 | void close_handle( uint h ); 19 | uint get_handle_data( uint h ); 20 | uint get_handle_type(uint h ); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /sgos2/apps/msys/kill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | int kill(int pid,int sig) 6 | { 7 | return PsTerminateProcess( pid, sig ); 8 | } 9 | -------------------------------------------------------------------------------- /sgos2/apps/msys/link.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | int link( const char* path1, const char* path2 ) 6 | { 7 | NOT_IMPLEMENTED(); 8 | return -1; 9 | } 10 | 11 | int unlink( const char* path ) 12 | { 13 | NOT_IMPLEMENTED(); 14 | return -1; 15 | } 16 | -------------------------------------------------------------------------------- /sgos2/apps/msys/misc.c: -------------------------------------------------------------------------------- 1 | #include "debug.H" 2 | 3 | void _REENT_SMALL_CHECK_INIT() 4 | { 5 | NOT_IMPLEMENTED(); 6 | } 7 | -------------------------------------------------------------------------------- /sgos2/apps/msys/msys.c: -------------------------------------------------------------------------------- 1 | 2 | int a() 3 | { 4 | } 5 | -------------------------------------------------------------------------------- /sgos2/apps/msys/sbrk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "debug.h" 5 | 6 | char* sbrk(int incr) 7 | { 8 | NOT_IMPLEMENTED(); 9 | return NULL; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /sgos2/apps/msys/stat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "debug.h" 5 | 6 | int stat(const char *path, struct _stat *buffer ) 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | -------------------------------------------------------------------------------- /sgos2/apps/msys/sysconf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "debug.h" 5 | #include "unistd.h" 6 | 7 | size_t getpagesize() 8 | { 9 | return PAGE_SIZE; 10 | } 11 | 12 | /* sysconf: POSIX 4.8.1.1 */ 13 | /* Allows a portable app to determine quantities of resources or 14 | presence of an option at execution time. */ 15 | long int 16 | sysconf (int in) 17 | { 18 | switch (in) 19 | { 20 | case _SC_ARG_MAX: 21 | /* FIXME: what's the right value? _POSIX_ARG_MAX is only 4K */ 22 | return KB(4); 23 | case _SC_OPEN_MAX: 24 | //FIXME 25 | return 4096; 26 | case _SC_PAGESIZE: 27 | return PAGE_SIZE; 28 | case _SC_CLK_TCK: 29 | //FIXME 30 | return 10; 31 | case _SC_JOB_CONTROL: 32 | return _POSIX_JOB_CONTROL; 33 | case _SC_CHILD_MAX: 34 | //FIXME 35 | return 1024; 36 | case _SC_NGROUPS_MAX: 37 | //FIXME 38 | return 1024; 39 | case _SC_SAVED_IDS: 40 | return _POSIX_SAVED_IDS; 41 | case _SC_VERSION: 42 | return _POSIX_VERSION; 43 | case _SC_NPROCESSORS_CONF: 44 | case _SC_NPROCESSORS_ONLN: 45 | //FIXME 46 | return 1; 47 | /*FALLTHRU*/ 48 | case _SC_PHYS_PAGES: 49 | case _SC_AVPHYS_PAGES: 50 | //FIXME 51 | return MB(64)/PAGE_SIZE; 52 | } 53 | DBG("not implemeted in=%d", in ); 54 | /* Invalid input or unimplemented sysconf name */ 55 | return -1; 56 | } 57 | -------------------------------------------------------------------------------- /sgos2/apps/msys/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "debug.h" 5 | #include 6 | 7 | clock_t times(struct tms *buf) 8 | { 9 | return -1; 10 | } 11 | 12 | int gettimeofday ( struct timeval * tv , struct timezone * tz ) 13 | { 14 | return -1; 15 | } 16 | -------------------------------------------------------------------------------- /sgos2/apps/msys/tty.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | int isatty( int id ) 6 | { 7 | DBG("id=%d", id ); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /sgos2/apps/msys/uname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "debug.h" 3 | #include "cygwin.h" 4 | 5 | struct utsname 6 | { 7 | char sysname[20]; 8 | char nodename[20]; 9 | char release[20]; 10 | char version[20]; 11 | char machine[20]; 12 | }; 13 | 14 | int uname( struct utsname* name ) 15 | { 16 | memset( name, 0, sizeof(*name) ); 17 | sprintf( name->sysname, "MSYS32_SGOS-%d.%d", OS_MAJOR_VERSION, OS_MINOR_VERSION ); 18 | strcpy( name->nodename, "unknown" ); 19 | sprintf( name->release, "GCC%.16s", __VERSION__ ); 20 | sprintf( name->version, "%s", __DATE__ ); 21 | strcpy( name->machine, "unknown" ); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /sgos2/apps/msys/virtual.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | #define MEM_LARGE_PAGES 0x20000000 6 | #define MEM_PHYSICAL 0x400000 7 | #define MEM_TOP_DOWN 0x100000 8 | #define MEM_WRITE_WATCH 0x200000 9 | 10 | typedef struct _MEMORY_BASIC_INFORMATION { 11 | void* BaseAddress; 12 | void* AllocationBase; 13 | uint AllocationProtect; 14 | size_t RegionSize; 15 | uint State; 16 | uint Protect; 17 | uint Type; 18 | } MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION; 19 | 20 | size_t WINAPI VirtualAlloc( void* addr, size_t siz, uint type, uint protect ) 21 | { 22 | uint flag=0; 23 | size_t ret; 24 | if( siz % PAGE_SIZE ) { 25 | siz = PAGE_ALIGN(siz); 26 | } 27 | if( protect&MEM_TOP_DOWN ) 28 | flag |= ALLOC_HIGHMEM; 29 | if( protect&MEM_PHYSICAL ) 30 | flag |= ALLOC_VIRTUAL; 31 | else 32 | flag |= ALLOC_LAZY; 33 | if( addr == NULL ) 34 | ret = (size_t)SysAllocateMemory( SysGetCurrentSpaceId(), siz, MEMORY_ATTR_WRITE, flag ); 35 | else 36 | ret = (size_t)addr; 37 | DBG("addr: %x ret: %x size: %x", addr, ret, siz ); 38 | return ret; 39 | } 40 | 41 | size_t WINAPI LocalAlloc( uint flag, size_t siz ) 42 | { 43 | return VirtualAlloc( NULL, siz, 0, 0 ); 44 | } 45 | 46 | void WINAPI VirtualFree( const void* p, size_t siz, uint type ) 47 | { 48 | SysFreeMemory( SysGetCurrentSpaceId(), (void*)p ); 49 | } 50 | 51 | size_t WINAPI VirtualQuery( const void* addr, MEMORY_BASIC_INFORMATION* info , size_t len ) 52 | { 53 | size_t beg, end; 54 | uint attr, flag; 55 | if( SysQueryAddress( SysGetCurrentSpaceId(), (size_t)addr, &beg, &end, &attr, &flag ) < 0 ) 56 | return 0; 57 | if( sizeof(MEMORY_BASIC_INFORMATION)>len ) 58 | return 0; 59 | info->BaseAddress = (void*)addr; 60 | info->AllocationBase = (void*)beg; 61 | info->AllocationProtect = 0; 62 | info->RegionSize = end-beg; 63 | info->State = 0; 64 | info->Type = 0; 65 | return sizeof(MEMORY_BASIC_INFORMATION); 66 | } 67 | 68 | -------------------------------------------------------------------------------- /sgos2/apps/msys/wait.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | int wait(int* state) 6 | { 7 | NOT_IMPLEMENTED(); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /sgos2/apps/servicemanager/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = servicemanager 9 | LDFLAGS += ../../crt/crt.a ../../api/api.a 10 | 11 | CXX = g++ $(CXXFLAGS) 12 | LD = ld $(LDFLAGS) 13 | OD = objdump $(ODFLAGS) 14 | LD2 = ../../tools/ld2/ld2 15 | WF = ../../tools/wf/wf 16 | IMAGE = ../../sgos2.img 17 | 18 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 19 | 20 | all: $(PROGRAM).exe 21 | 22 | $(PROGRAM).exe: $(OBJECTS) 23 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 24 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 25 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 26 | 27 | # Standard Procedures 28 | .c.o: 29 | $(CXX) -c -o $@ $< 30 | 31 | clean: 32 | $(RM) $(OBJECTS) 33 | $(RM) $(PROGRAM).dmp 34 | $(RM) $(PROGRAM).exe 35 | -------------------------------------------------------------------------------- /sgos2/apps/speaker/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = speaker 9 | LDFLAGS += ../../crt/crt.a 10 | LDFLAGS += ../../api/api.dll 11 | 12 | CXX = g++ $(CXXFLAGS) 13 | LD = ld $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | WF = ../../tools/wf/wf 16 | IMAGE = ../../sgos2.img 17 | 18 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 19 | 20 | all: $(PROGRAM).exe 21 | 22 | $(PROGRAM).exe: $(OBJECTS) 23 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 24 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 25 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 26 | 27 | # Standard Procedures 28 | .cpp.o: 29 | $(CXX) -c -o $@ $< 30 | 31 | clean: 32 | $(RM) $(OBJECTS) 33 | $(RM) $(PROGRAM).dmp 34 | $(RM) $(PROGRAM).exe 35 | -------------------------------------------------------------------------------- /sgos2/apps/speaker/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void beep(); 7 | void test(); 8 | void play(double freq, unsigned int delay); 9 | 10 | void ServiceMessageLoop() 11 | { 12 | Message msg; 13 | int result; 14 | printf("[Speaker]Service started.\n"); 15 | for( ;; ){ 16 | //Remember to reset msg every time. 17 | memset( &msg, 0, sizeof(msg) ); 18 | result = WaitMessage( &msg ); 19 | if( result < 0 ){ 20 | printf("[Speaker] Failed in WaitMessage: result = %d\n", result ); 21 | continue; 22 | } 23 | msg.Code = 0; //set it to success 24 | switch( msg.Command ){ 25 | case 1: 26 | test(); 27 | break; 28 | case 2: 29 | play( msg.Arguments[0], msg.Arguments[1] ); 30 | break; 31 | case 3: 32 | beep(); 33 | break; 34 | default: 35 | printf("[Speaker]Unknown command: %x\n", msg.Command ); 36 | } 37 | ReplyMessage( &msg ); 38 | } 39 | } 40 | 41 | 42 | int main() 43 | { 44 | int id; 45 | //注册线程名称,其它程序便可以向此服务线程发送消息 46 | id = SmNotifyService( 0, 0, "Speaker" ); 47 | if( id < 0 ){ 48 | printf("[Speaker]add service failed.\n"); 49 | return(-1); 50 | } 51 | //Do Service Loop 52 | ServiceMessageLoop(); 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /sgos2/apps/speaker/port.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //IO操作 4 | uchar inbyte( ushort port ) 5 | { 6 | register t_8 ret; 7 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 8 | return ret; 9 | } 10 | 11 | ushort inword( ushort port ) 12 | { 13 | register t_16 ret; 14 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 15 | return ret; 16 | } 17 | 18 | uint indword( uint port ) 19 | { 20 | register t_16 ret; 21 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 22 | return ret; 23 | } 24 | 25 | void outbyte( ushort port, uchar v ) 26 | { 27 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( v ) , "d"( port ) ); 28 | } 29 | 30 | void outword( ushort port, ushort v ) 31 | { 32 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( v ) , "d"( port ) ); 33 | } 34 | 35 | void outdword( ushort port, uint v ) 36 | { 37 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( v ) , "d"( port ) ); 38 | } 39 | -------------------------------------------------------------------------------- /sgos2/apps/speaker/port.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H 2 | #define _IO_H 3 | 4 | //IO操作 5 | uchar inbyte( ushort port ); 6 | ushort inword( ushort port ); 7 | uint indword( uint port ); 8 | void outbyte( ushort port, uchar v ); 9 | void outword( ushort port, ushort v ); 10 | void outdword( ushort port, uint v ); 11 | #endif 12 | -------------------------------------------------------------------------------- /sgos2/apps/speaker/speaker.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "port.h" 4 | 5 | static void startSound(unsigned int freq) 6 | { 7 | unsigned int Divisor; 8 | if( freq==0 ) 9 | return; 10 | Divisor= 1193180 / freq; 11 | 12 | outbyte(0x43, 0xB6); 13 | outbyte(0x42, Divisor & 0xFF); 14 | outbyte(0x42, Divisor >> 8); 15 | 16 | outbyte(0x61, inbyte(0x61) | 3); 17 | } 18 | 19 | static void stopSound() 20 | { 21 | outbyte(0x61, inbyte(0x61) & ~3); 22 | } 23 | 24 | void play(double freq, unsigned int delay) 25 | { 26 | startSound((unsigned int)freq); 27 | SysSleepThread(delay); 28 | stopSound(); 29 | } 30 | 31 | void test() 32 | { 33 | play(261.626,300); //C4 34 | play(293.665,300); //D4 35 | play(329.628,300); //E4 36 | play(329.628,1000); //E4 37 | play(329.628,300); //E4 38 | play(349.228,300); //F4 39 | play(391.995,300); //G4 40 | play(391.995,800); //G4 41 | play(391.995,300); //G4 42 | play(349.228,300); //F4 43 | play(329.628,300); //E4 44 | play(293.665,1000); //D4 45 | play(293.665,300); //D4 46 | play(329.628,300); //E4 47 | play(261.626,1000); //C4 48 | 49 | } 50 | 51 | void beep() 52 | { 53 | play(392,100); //G4 54 | } 55 | -------------------------------------------------------------------------------- /sgos2/apps/startup/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = startup 9 | LDFLAGS += ../../crt/crt.a 10 | LDFLAGS += ../../api/api.dll 11 | 12 | CXX = gcc $(CXXFLAGS) 13 | LD = ld $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | LD2 = ../../tools/ld2/ld2 16 | WF = ../../tools/wf/wf 17 | IMAGE = ../../sgos2.img 18 | 19 | OBJECTS = $(patsubst %.c,%.o, $(wildcard *.c)) 20 | 21 | all: $(PROGRAM).exe 22 | 23 | $(PROGRAM).exe: $(OBJECTS) 24 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 25 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 26 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 27 | 28 | # Standard Procedures 29 | .c.o: 30 | $(CXX) -c -o $@ $< 31 | 32 | clean: 33 | $(RM) $(OBJECTS) 34 | $(RM) $(PROGRAM).dmp 35 | $(RM) $(PROGRAM).exe 36 | -------------------------------------------------------------------------------- /sgos2/apps/startup/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void run( const char* p ) 5 | { 6 | uint pid; 7 | if( !*p ) 8 | return; 9 | printf("[startup] running %s\n", p ); 10 | int ret = PsCreateProcess( p, "OS=SGOS2", &pid ); 11 | if( ret < 0 ) 12 | printf("[startup] Failed to run %s\n", p ); 13 | } 14 | 15 | void parse( char* buf ) 16 | { 17 | char* p, *q; 18 | for( p=q=buf; ; q++){ 19 | if( *q=='\0' || *q=='#' ){ 20 | *q = '\0'; 21 | run( p ); 22 | break; 23 | } 24 | if( *q=='\r' || *q=='\n' ){ 25 | *q='\0'; 26 | run( p ); 27 | p = q+1; 28 | } 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | FILEBUF* fb=0; 35 | char buf[4096]; 36 | int ret; 37 | char *path = "/diskc/sgos2/startup.txt"; 38 | fb = FsOpenFile( path, FILE_READ, 0); 39 | if( fb == NULL ){ 40 | printf("[startup] Cannot open file %s\n", path ); 41 | } 42 | ret = FsReadFile(fb, (uchar*)buf, 4096 ); 43 | FsCloseFile( fb ); 44 | if( ret < 0 ) 45 | return ret; 46 | buf[ret]=0; 47 | parse( buf ); 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /sgos2/apps/system/Makefile: -------------------------------------------------------------------------------- 1 | #sgos System Class 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = -shared 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = system 9 | LDFLAGS += --image-base 0x7FC00000 ../libc/libc.dll ../msg/msg.dll ../api/api.dll 10 | 11 | CXX = g++ $(CXXFLAGS) 12 | LD = ld $(LDFLAGS) 13 | OD = objdump $(ODFLAGS) 14 | LD2 = ../../tools/ld2/ld2 15 | WF = ../../tools/wf/wf 16 | IMAGE = ../../sgos2.img 17 | 18 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) \ 19 | $(patsubst %.cpp,%.o, $(wildcard base/*.cpp)) \ 20 | $(patsubst %.cpp,%.o, $(wildcard message/*.cpp)) \ 21 | $(patsubst %.cpp,%.o, $(wildcard threading/*.cpp)) \ 22 | $(patsubst %.cpp,%.o, $(wildcard service/*.cpp)) 23 | 24 | all: $(PROGRAM).bxm 25 | 26 | $(PROGRAM).bxm: $(OBJECTS) 27 | $(LD) $(OBJECTS) -o$(PROGRAM).dll 28 | $(OD) $(PROGRAM).dll > $(PROGRAM).dmp 29 | $(LD2) $(PROGRAM).dll $@ 30 | $(WF) $(IMAGE) -src $@ -dest sgos2/$@ 31 | 32 | # Standard Procedures 33 | .cpp.o: 34 | $(CXX) -c -o $@ $< 35 | 36 | clean: 37 | $(RM) $(OBJECTS) 38 | $(RM) $(PROGRAM).bxm 39 | $(RM) $(PROGRAM).dmp 40 | $(RM) $(PROGRAM).dll 41 | -------------------------------------------------------------------------------- /sgos2/apps/system/service/bioscall.cpp: -------------------------------------------------------------------------------- 1 | // 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace System{ 8 | namespace Service{ 9 | int CallBIOS( int interrupt, int& eax, int& ebx, int& ecx, int& edx ) 10 | { 11 | THREAD_CONTEXT tc; 12 | int ret; 13 | memset( &tc, 0, sizeof(THREAD_CONTEXT) ); 14 | tc.eax = eax; 15 | tc.ebx = ebx; 16 | tc.ecx = ecx; 17 | tc.edx = edx; 18 | ret = sys_bios_call( interrupt, &tc, sizeof(THREAD_CONTEXT) ); 19 | eax = tc.eax; 20 | ebx = tc.ebx; 21 | ecx = tc.ecx; 22 | edx = tc.edx; 23 | return ret; 24 | } 25 | 26 | int CallBIOS( int interrupt, void* tc ) 27 | { 28 | return sys_bios_call( interrupt, tc, sizeof(THREAD_CONTEXT) ); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /sgos2/apps/system/service/map.cpp: -------------------------------------------------------------------------------- 1 | // Memory Map 2 | #include 3 | #include 4 | #include 5 | 6 | namespace System{ 7 | namespace Service{ 8 | int MapMemory( size_t vaddr, size_t paddr, size_t siz, uint flag ) 9 | { 10 | return sys_vm_map( vaddr, paddr, siz, flag ); 11 | } 12 | 13 | int UnmapMemory( size_t vaddr, size_t siz ) 14 | { 15 | return sys_vm_map( vaddr, 0, 0, MAP_UNMAP ); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sgos2/apps/system/service/name.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace System{ 5 | } 6 | -------------------------------------------------------------------------------- /sgos2/apps/system/service/port.cpp: -------------------------------------------------------------------------------- 1 | // X86 IO Operations 2 | #include 3 | 4 | namespace System{ 5 | namespace Service{ 6 | 7 | //IO操作 8 | uchar inbyte( ushort port ) 9 | { 10 | register t_8 ret; 11 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 12 | return ret; 13 | } 14 | 15 | ushort inword( ushort port ) 16 | { 17 | register t_16 ret; 18 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 19 | return ret; 20 | } 21 | 22 | uint indword( uint port ) 23 | { 24 | register t_16 ret; 25 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 26 | return ret; 27 | } 28 | 29 | void outbyte( ushort port, uchar v ) 30 | { 31 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( v ) , "d"( port ) ); 32 | } 33 | 34 | void outword( ushort port, ushort v ) 35 | { 36 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( v ) , "d"( port ) ); 37 | } 38 | 39 | void outdword( ushort port, uint v ) 40 | { 41 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( v ) , "d"( port ) ); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /sgos2/apps/system/threading/thread.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include //SGOS2 native API 3 | #include 4 | 5 | namespace System{ 6 | 7 | /* 静态函数 */ 8 | //返回当前线程 9 | Thread Thread::ThisThread() 10 | { 11 | Thread thread; 12 | thread.handle = (uint)sys_thread_self(); 13 | return thread; 14 | } 15 | 16 | //当前线程等待 17 | void Thread::Sleep(uint ms) 18 | { 19 | sys_thread_wait(ms); 20 | } 21 | 22 | //结束当前线程 23 | void Thread::Exit(uint code) 24 | { 25 | sys_thread_exit(code); 26 | } 27 | 28 | //查找一个线程 29 | Thread Thread::find( const char* name ) 30 | { 31 | uint hdl; 32 | Thread thread; 33 | hdl = match_name( name ); 34 | if( hdl == 0 ){ 35 | //An exception should happen here... 36 | } 37 | thread.handle = hdl; 38 | return thread; 39 | } 40 | 41 | Thread::Thread() 42 | { 43 | this->procedure = NULL; 44 | this->exitCode = 0; 45 | this->handle = 0; 46 | } 47 | 48 | Thread::Thread(void* proc) 49 | { 50 | this->procedure = proc; 51 | this->exitCode = 0; 52 | this->handle = 0; 53 | } 54 | 55 | Thread::~Thread() 56 | { 57 | //detach the thread?? 58 | } 59 | 60 | /* 非静态函数 */ 61 | // 终止线程 62 | void Thread::abort() 63 | { 64 | sys_thread_kill( this->handle, -1 ); 65 | } 66 | 67 | // 开始线程 68 | void Thread::resume() 69 | { 70 | sys_thread_resume( this->handle ); 71 | } 72 | 73 | void Thread::start() 74 | { 75 | int ret; 76 | ret = sys_thread_create( (uint)this->procedure, &this->handle ); 77 | if( ret<0 ) 78 | return; 79 | resume(); 80 | } 81 | 82 | // 挂起线程 83 | void Thread::suspend() 84 | { 85 | sys_thread_suspend( this->handle ); 86 | } 87 | 88 | // 等待线程结束 89 | void Thread::join() 90 | { 91 | //not implemented yet! 92 | } 93 | 94 | // 获取句柄 95 | uint Thread::getHandle() 96 | { 97 | return this->handle; 98 | } 99 | 100 | //命名系统 101 | int Thread::createName( const char* name ) 102 | { 103 | return create_name( this->handle, name ); 104 | } 105 | 106 | void Thread::deleteName( const char* name ) 107 | { 108 | delete_name( this->handle, name ); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /sgos2/apps/time/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = time 9 | LDFLAGS += ../../crt/crt.a 10 | LDFLAGS += ../../api/api.a 11 | 12 | CXX = g++ $(CXXFLAGS) 13 | LD = ld $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | LD2 = ../../tools/ld2/ld2 16 | WF = ../../tools/wf/wf 17 | IMAGE = ../../sgos2.img 18 | 19 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 20 | 21 | all: $(PROGRAM).exe 22 | 23 | $(PROGRAM).exe: $(OBJECTS) 24 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 25 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 26 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 27 | 28 | # Standard Procedures 29 | .cpp.o: 30 | $(CXX) -c -o $@ $< 31 | 32 | clean: 33 | $(RM) $(OBJECTS) 34 | $(RM) $(PROGRAM).dmp 35 | $(RM) $(PROGRAM).exe 36 | -------------------------------------------------------------------------------- /sgos2/apps/time/biostime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define BCD_TO_HEX(bcd) ((bcd)&0xf)+(((bcd)>>4)&0xf)*10 7 | 8 | size_t mapAddress; 9 | ThreadContext vmRegs={0}; 10 | 11 | int Vm86Call( void* c ) 12 | { 13 | return Api_InvokeBiosService( 0x1A, c, sizeof(ThreadContext) ); 14 | } 15 | 16 | int GetBiosTime(int &h, int &m, int &s) 17 | { 18 | vmRegs.eax = 0x0200; 19 | if( Vm86Call( &vmRegs ) < 0 ) 20 | return -1; 21 | h = BCD_TO_HEX(vmRegs.ecx>>8); 22 | m = BCD_TO_HEX(vmRegs.ecx&0xff); 23 | s = BCD_TO_HEX(vmRegs.edx>>8); 24 | return 0; 25 | } 26 | 27 | int GetBiosDate(int &c, int &y, int &m, int &d) 28 | { 29 | vmRegs.eax = 0x0400; 30 | if( Vm86Call( &vmRegs ) < 0 ) 31 | return -1; 32 | c = BCD_TO_HEX(vmRegs.ecx>>8); 33 | y = BCD_TO_HEX(vmRegs.ecx&0xff); 34 | m = BCD_TO_HEX(vmRegs.edx>>8); 35 | d = BCD_TO_HEX(vmRegs.edx&0xff); 36 | return 0; 37 | } 38 | 39 | int GetDateTime( struct tm &t ) 40 | { 41 | int c, y, mo, d, h, m, s; 42 | GetBiosDate( c, y, mo, d ); 43 | GetBiosTime( h, m, s ); 44 | t.tm_sec = s; 45 | t.tm_min = m; 46 | t.tm_hour = h; 47 | t.tm_mday = d; 48 | t.tm_mon = mo; 49 | t.tm_year = y; 50 | t.tm_mon --; 51 | return 0; 52 | } 53 | 54 | time_t GetUnixTime() 55 | { 56 | struct tm t; 57 | GetDateTime(t); 58 | return mktime(&t); 59 | } 60 | 61 | 62 | int InitializeBiosTime() 63 | { 64 | uint sid = SysGetCurrentSpaceId(); 65 | mapAddress = (size_t)SysAllocateMemory( sid, MB(1), 0, ALLOC_VIRTUAL ); 66 | if( mapAddress==0 ){ 67 | printf("[time]failed to allocate 1 mb memory.\n"); 68 | return -ERR_NOMEM; 69 | } 70 | if( SysMapMemory( sid, mapAddress, MB(1), 0, 0, MAP_ADDRESS ) < 0 ){ 71 | printf("[time]failed to map 1 mb memory.\n"); 72 | return -ERR_NOMEM; 73 | } 74 | time_t unixTime = GetUnixTime(); 75 | char timestr[100]; 76 | strtime(&unixTime, timestr); 77 | printf("[time]System time: %s\n", timestr ); 78 | return 0; 79 | } 80 | 81 | -------------------------------------------------------------------------------- /sgos2/apps/time/biostime.h: -------------------------------------------------------------------------------- 1 | #ifndef _BIOSTIME_H 2 | #define _BIOSTIME_H 3 | 4 | #include 5 | 6 | #define Time_GetTimeString 0x00000101 7 | #define Time_SetLocale 0x00000102 8 | #define Time_GetLocale 0x00000103 9 | #define Time_GetUnixTime 0x00000104 10 | #define Time_GetTimeOfDay 0x00000105 11 | #define Time_SetTimeOfDay 0x00000106 12 | 13 | 14 | int InitializeBiosTime(); 15 | time_t GetUnixTime(); 16 | int GetDateTime( struct tm &t ); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /sgos2/apps/time/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "biostime.h" 6 | 7 | void parse( Message& msg ) 8 | { 9 | int ret; 10 | switch( msg.Command ){ 11 | default: 12 | printf("[time]Unknown command: %x\n", msg.Command ); 13 | msg.Code = -ERR_WRONGARG; 14 | } 15 | ReplyMessage( &msg ); 16 | } 17 | 18 | int startService() 19 | { 20 | //接收消息 21 | Message msg; 22 | int id; 23 | //初始化驱动程序 24 | if( InitializeBiosTime() < 0 ) 25 | SysExitSpace((uint)-1); 26 | //注册线程名称,其它程序便可以向此服务线程发送消息 27 | id = SmNotifyService( 0, 0, "Time" ); 28 | if( id < 0 ){ 29 | printf("[time]add service failed.\n"); 30 | SysExitSpace((uint)-1); 31 | } 32 | printf("[Time]Starting Time service ...\n"); 33 | for(;;){ 34 | //Pending for messages 35 | int result = WaitMessage(&msg); 36 | if( result < 0 ){ 37 | printf("[Time]Failed to receive message: result = %d\n", result ); 38 | continue; 39 | } 40 | msg.Code = 0; 41 | parse( msg ); 42 | } 43 | SmRemoveService( id ); 44 | return 0; 45 | } 46 | 47 | int main() 48 | { 49 | return startService(); 50 | } 51 | 52 | -------------------------------------------------------------------------------- /sgos2/apps/time/port.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //IO操作 4 | uchar inbyte( ushort port ) 5 | { 6 | register t_8 ret; 7 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 8 | return ret; 9 | } 10 | 11 | ushort inword( ushort port ) 12 | { 13 | register t_16 ret; 14 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 15 | return ret; 16 | } 17 | 18 | uint indword( uint port ) 19 | { 20 | register t_16 ret; 21 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 22 | return ret; 23 | } 24 | 25 | void outbyte( ushort port, uchar v ) 26 | { 27 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( v ) , "d"( port ) ); 28 | } 29 | 30 | void outword( ushort port, ushort v ) 31 | { 32 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( v ) , "d"( port ) ); 33 | } 34 | 35 | void outdword( ushort port, uint v ) 36 | { 37 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( v ) , "d"( port ) ); 38 | } 39 | -------------------------------------------------------------------------------- /sgos2/apps/time/port.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H 2 | #define _IO_H 3 | 4 | //IO操作 5 | uchar inbyte( ushort port ); 6 | ushort inword( ushort port ); 7 | uint indword( uint port ); 8 | void outbyte( ushort port, uchar v ); 9 | void outword( ushort port, ushort v ); 10 | void outdword( ushort port, uint v ); 11 | #endif 12 | -------------------------------------------------------------------------------- /sgos2/apps/uname.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/apps/uname.exe -------------------------------------------------------------------------------- /sgos2/apps/vesa/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = vesa 9 | LDFLAGS += ../../crt/crt.a 10 | LDFLAGS += ../../api/api.a 11 | 12 | CXX = g++ $(CXXFLAGS) 13 | LD = ld $(LDFLAGS) 14 | OD = objdump $(ODFLAGS) 15 | LD2 = ../../tools/ld2/ld2 16 | WF = ../../tools/wf/wf 17 | IMAGE = ../../sgos2.img 18 | 19 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 20 | 21 | all: $(PROGRAM).exe 22 | 23 | $(PROGRAM).exe: $(OBJECTS) 24 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 25 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 26 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 27 | 28 | # Standard Procedures 29 | .cpp.o: 30 | $(CXX) -c -o $@ $< 31 | 32 | clean: 33 | $(RM) $(OBJECTS) 34 | $(RM) $(PROGRAM).dmp 35 | $(RM) $(PROGRAM).exe 36 | -------------------------------------------------------------------------------- /sgos2/apps/vesa/int86.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "vesa.h" 4 | 5 | 6 | int VideoBiosCall( ThreadContext* context ) 7 | { 8 | return Api_InvokeBiosService( 0x10, (void*)context, sizeof(*context) ); 9 | } 10 | -------------------------------------------------------------------------------- /sgos2/apps/vesa/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "vesa.h" 6 | 7 | void parse( Message& msg ) 8 | { 9 | int ret; 10 | switch( msg.Command ){ 11 | case Video_SetGraphicalMode: 12 | msg.Code = SetGraphicalMode(); 13 | break; 14 | case Video_SetCharacterMode: 15 | msg.Code = SetCharacterMode(); 16 | break; 17 | case Video_SetVideoMode: 18 | msg.Code = SetVideoMode( msg.Arguments[0], msg.Arguments[1], msg.Arguments[2] ); 19 | break; 20 | case Video_GetVideoMode: 21 | msg.Code = GetVideoMode( msg.Arguments[0], msg.Arguments[1], msg.Arguments[2] ); 22 | break; 23 | case Video_GetVideoAddress: 24 | msg.Code = GetVideoAddress( msg.Arguments[0], msg.Arguments[1] ); 25 | break; 26 | default: 27 | printf("[Vesa]Unknown command: %x\n", msg.Command ); 28 | msg.Code = -ERR_WRONGARG; 29 | } 30 | ReplyMessage( &msg ); 31 | } 32 | 33 | int startService() 34 | { 35 | //接收消息 36 | Message msg; 37 | int id; 38 | //注册线程名称,其它程序便可以向此服务线程发送消息 39 | id = SmNotifyService( 0, 0, "Vesa" ); 40 | if( id < 0 ){ 41 | printf("[Vesa]add service failed.\n"); 42 | SysExitSpace((uint)-1); 43 | } 44 | //初始化驱动程序 45 | InitializeVesa(); 46 | printf("[Vesa]Starting Vesa service ...\n"); 47 | for(;;){ 48 | //Pending for messages 49 | int result = WaitMessage(&msg); 50 | if( result < 0 ){ 51 | printf("[Vesa]Failed to receive message: result = %d\n", result ); 52 | continue; 53 | } 54 | msg.Code = 0; 55 | parse( msg ); 56 | } 57 | SmRemoveService( id ); 58 | return 0; 59 | } 60 | 61 | int main() 62 | { 63 | return startService(); 64 | } 65 | 66 | -------------------------------------------------------------------------------- /sgos2/apps/vesa/port.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //IO操作 4 | uchar inbyte( ushort port ) 5 | { 6 | register t_8 ret; 7 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 8 | return ret; 9 | } 10 | 11 | ushort inword( ushort port ) 12 | { 13 | register t_16 ret; 14 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 15 | return ret; 16 | } 17 | 18 | uint indword( uint port ) 19 | { 20 | register t_16 ret; 21 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 22 | return ret; 23 | } 24 | 25 | void outbyte( ushort port, uchar v ) 26 | { 27 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( v ) , "d"( port ) ); 28 | } 29 | 30 | void outword( ushort port, ushort v ) 31 | { 32 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( v ) , "d"( port ) ); 33 | } 34 | 35 | void outdword( ushort port, uint v ) 36 | { 37 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( v ) , "d"( port ) ); 38 | } 39 | -------------------------------------------------------------------------------- /sgos2/apps/vesa/port.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H 2 | #define _IO_H 3 | 4 | //IO操作 5 | uchar inbyte( ushort port ); 6 | ushort inword( ushort port ); 7 | uint indword( uint port ); 8 | void outbyte( ushort port, uchar v ); 9 | void outword( ushort port, ushort v ); 10 | void outdword( ushort port, uint v ); 11 | #endif 12 | -------------------------------------------------------------------------------- /sgos2/apps/wadvapi/Makefile: -------------------------------------------------------------------------------- 1 | #sgos api 2 | 3 | CCFLAGS = -g -Werror -I../include -I../../include -fno-builtin -ffreestanding -fleading-underscore 4 | LDFLAGS = ../../api/api.dll --add-stdcall-alias 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = advapi32 9 | 10 | CC = gcc $(CCFLAGS) 11 | LDLIB = ld -r $(LDFLAGS) 12 | LD = ld --image-base 0x7FA00000 -shared $(LDFLAGS) 13 | OD = objdump $(ODFLAGS) 14 | WF = ../../tools/wf/wf 15 | IMAGE = ../../sgos2.img 16 | 17 | OBJECTS = $(patsubst %.c,%.o, $(wildcard *.c)) 18 | 19 | all: $(PROGRAM).dll 20 | 21 | $(PROGRAM).dll: $(OBJECTS) 22 | $(LD) $(OBJECTS) -o$(PROGRAM).dll 23 | $(OD) $(PROGRAM).dll > $(PROGRAM).dmp 24 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 25 | 26 | # Standard Procedures 27 | .c.o: 28 | $(CC) -c -o $@ $< 29 | 30 | clean: 31 | $(RM) $(OBJECTS) 32 | $(RM) $(PROGRAM).a 33 | $(RM) $(PROGRAM).dmp 34 | $(RM) $(PROGRAM).dll -------------------------------------------------------------------------------- /sgos2/apps/wadvapi/advapi.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/apps/wadvapi/advapi.c -------------------------------------------------------------------------------- /sgos2/apps/wadvapi/debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * debug.c 3 | * 4 | * SGOS debugger. 5 | * 6 | * Copyright (C) 2008 Huang Guan 7 | * 8 | * 2008-01-31 Created. 9 | * 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | void print_error(char* file, char* function, int line, const char *fmt, ...) 17 | { 18 | va_list args; 19 | char printbuf[512]; 20 | int i; 21 | va_start(args, fmt); 22 | i=vsprintf( printbuf, fmt, args ); 23 | printbuf[i] = 0; 24 | va_end(args); 25 | printf("[%s]%s(%d): %s\n", file, function, line, printbuf); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /sgos2/apps/wadvapi/debug.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEBUG_H 2 | #define _DEBUG_H 3 | 4 | #include 5 | #include 6 | 7 | //#define RELEASE 8 | 9 | #ifndef RELEASE 10 | #define DBG(args ...) \ 11 | print_error( __FILE__, (char*)__func__, __LINE__, ##args ) 12 | #else 13 | #define DBG(args ...) 14 | //#define DBG printf 15 | #endif 16 | #define MSG printf 17 | void print_error(char* file, char* function, int line, const char *fmt, ...); 18 | 19 | #define NOT_IMPLEMENTED() DBG("## Not implemented.") 20 | 21 | #endif //_DEBUG_H 22 | 23 | -------------------------------------------------------------------------------- /sgos2/apps/wadvapi/fun.txt: -------------------------------------------------------------------------------- 1 | AccessCheck 2 | AddAccessAllowedAce 3 | AddAccessDeniedAce 4 | AddAce 5 | AllocateLocallyUniqueId 6 | CopySid 7 | CreateProcessAsUserW 8 | CryptAcquireContextA 9 | CryptGenRandom 10 | CryptReleaseContext 11 | DeregisterEventSource 12 | DuplicateTokenEx 13 | EqualPrefixSid 14 | EqualSid 15 | FindFirstFreeAce 16 | GetAce 17 | GetKernelObjectSecurity 18 | GetLengthSid 19 | GetSecurityDescriptorDacl 20 | GetSecurityDescriptorGroup 21 | GetSecurityDescriptorOwner 22 | GetSecurityInfo 23 | GetSidIdentifierAuthority 24 | GetSidSubAuthority 25 | GetSidSubAuthorityCount 26 | GetTokenInformation 27 | GetUserNameA 28 | ImpersonateLoggedOnUser 29 | InitializeAcl 30 | InitializeSecurityDescriptor 31 | InitializeSid 32 | LogonUserW 33 | LookupAccountNameW 34 | LookupAccountSidA 35 | LookupAccountSidW 36 | LsaClose 37 | LsaEnumerateAccountRights 38 | LsaFreeMemory 39 | LsaNtStatusToWinError 40 | LsaOpenPolicy 41 | LsaRetrievePrivateData 42 | LsaStorePrivateData 43 | MakeSelfRelativeSD 44 | OpenProcessToken 45 | PrivilegeCheck 46 | RegCloseKey 47 | RegCreateKeyExA 48 | RegDeleteKeyA 49 | RegDeleteValueA 50 | RegEnumKeyExA 51 | RegEnumValueA 52 | RegGetKeySecurity 53 | RegLoadKeyW 54 | RegOpenKeyExA 55 | RegOpenKeyExW 56 | RegQueryInfoKeyA 57 | RegQueryValueExA 58 | RegQueryValueExW 59 | RegSetValueExA 60 | RegSetValueExW 61 | RegisterEventSourceA 62 | ReportEventA 63 | RevertToSelf 64 | SetSecurityDescriptorDacl 65 | SetSecurityDescriptorGroup 66 | SetSecurityDescriptorOwner 67 | SetTokenInformation 68 | -------------------------------------------------------------------------------- /sgos2/apps/wadvapi/security.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | struct SECURITY_DESCRIPTOR 6 | { 7 | uchar Revision; 8 | uchar Sbz1; 9 | void* Control; // point to SECURITY_DESCRIPTOR_CONTROL typedef WORD SECURITY_DESCRIPTOR_CONTROL 10 | void* Owner; // point to PSID typedef PVOID PSID 11 | void* Group; // point to PSID typedef PVOID PSID 12 | void* Sacl; // point to ACL 13 | void* Dacl; // point to ACL 14 | }; 15 | 16 | int WINAPI InitializeSecurityDescriptor( struct SECURITY_DESCRIPTOR* p, uint revision ) 17 | { 18 | DBG("revision: %d", revision ); 19 | return 0; 20 | } 21 | 22 | int WINAPI SetSecurityDescriptorDacl( struct SECURITY_DESCRIPTOR* p, int bDaclPresent, int pDacl, int bDaclDefaulted ) 23 | { 24 | NOT_IMPLEMENTED(); 25 | return 0; //failed. 26 | } 27 | 28 | int WINAPI InitializeSid( void* Sid, void* pIdentifierAuthority, uchar nSubAuthorityCount ) 29 | { 30 | return 1; //success 31 | return 0; //failed. 32 | } 33 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/Makefile: -------------------------------------------------------------------------------- 1 | #sgos api 2 | 3 | CCFLAGS = -g -Werror -I../include -I../../include -fno-builtin -ffreestanding -fleading-underscore 4 | LDFLAGS = ../../api/api.dll --add-stdcall-alias 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = kernel32 9 | 10 | CC = gcc $(CCFLAGS) 11 | LDLIB = ld -r $(LDFLAGS) 12 | LD = ld --image-base 0x7FB00000 -shared $(LDFLAGS) 13 | OD = objdump $(ODFLAGS) 14 | WF = ../../tools/wf/wf 15 | IMAGE = ../../sgos2.img 16 | 17 | OBJECTS = $(patsubst %.c,%.o, $(wildcard *.c)) 18 | 19 | all: $(PROGRAM).dll 20 | 21 | $(PROGRAM).dll: $(OBJECTS) 22 | $(LD) $(OBJECTS) -o$(PROGRAM).dll 23 | $(OD) $(PROGRAM).dll > $(PROGRAM).dmp 24 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 25 | 26 | # Standard Procedures 27 | .c.o: 28 | $(CC) -c -o $@ $< 29 | 30 | clean: 31 | $(RM) $(OBJECTS) 32 | $(RM) $(PROGRAM).a 33 | $(RM) $(PROGRAM).dmp 34 | $(RM) $(PROGRAM).dll -------------------------------------------------------------------------------- /sgos2/apps/wkernel/backup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI BackupRead() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI BackupSeek() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI BackupWrite() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/badptr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI IsBadStringPtrA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI IsBadWritePtr() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/beep.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void WINAPI Beep(uint freq, uint duration) 5 | { 6 | Message msg; 7 | int ret; 8 | uint tid = SmGetServiceThreadByName("Speaker"); 9 | if( tid == 0 ){ 10 | printf("[wkernel] Speaker service not found.\n"); 11 | return; 12 | } 13 | msg.ThreadId = tid; 14 | msg.Command = 2; 15 | msg.Arguments[0] = freq; 16 | msg.Arguments[1] = duration; 17 | ret = Api_Send(&msg, 0); 18 | if( ret < 0 ){ 19 | printf("[wkernel] Failed to send command to service.\n"); 20 | return ; 21 | } 22 | Api_Receive(&msg, 3000); 23 | //Omit the result. 24 | } 25 | 26 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/char.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI MultiByteToWideChar() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI WideCharToMultiByte() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI GetOEMCP() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/comm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI SetCommBreak() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI SetCommMask() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI SetCommState() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI SetCommTimeouts() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | void WINAPI PurgeComm() 31 | { 32 | NOT_IMPLEMENTED(); 33 | } 34 | 35 | 36 | void WINAPI WaitCommEvent() 37 | { 38 | NOT_IMPLEMENTED(); 39 | } 40 | 41 | 42 | void WINAPI ClearCommBreak() 43 | { 44 | NOT_IMPLEMENTED(); 45 | } 46 | 47 | 48 | void WINAPI ClearCommError() 49 | { 50 | NOT_IMPLEMENTED(); 51 | } 52 | 53 | 54 | void WINAPI GetCommState() 55 | { 56 | NOT_IMPLEMENTED(); 57 | } 58 | 59 | 60 | void WINAPI TransmitCommChar() 61 | { 62 | NOT_IMPLEMENTED(); 63 | } 64 | 65 | 66 | void WINAPI EscapeCommFunction() 67 | { 68 | NOT_IMPLEMENTED(); 69 | } 70 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/console.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | int WINAPI SetConsoleCtrlHandler(void* routine, int add) 6 | { 7 | return 0; //failed. 8 | } 9 | 10 | 11 | void WINAPI SetConsoleCursorPosition() 12 | { 13 | NOT_IMPLEMENTED(); 14 | } 15 | 16 | 17 | void WINAPI SetConsoleMode() 18 | { 19 | NOT_IMPLEMENTED(); 20 | } 21 | 22 | 23 | void WINAPI SetConsoleTextAttribute() 24 | { 25 | NOT_IMPLEMENTED(); 26 | } 27 | 28 | 29 | void WINAPI SetConsoleTitleA() 30 | { 31 | NOT_IMPLEMENTED(); 32 | } 33 | 34 | 35 | void WINAPI ScrollConsoleScreenBufferA() 36 | { 37 | NOT_IMPLEMENTED(); 38 | } 39 | 40 | 41 | void WINAPI WriteConsoleOutputA() 42 | { 43 | NOT_IMPLEMENTED(); 44 | } 45 | 46 | 47 | void WINAPI ReadConsoleInputA() 48 | { 49 | NOT_IMPLEMENTED(); 50 | } 51 | 52 | 53 | void WINAPI ReadConsoleOutputA() 54 | { 55 | NOT_IMPLEMENTED(); 56 | } 57 | 58 | 59 | void WINAPI GetConsoleCP() 60 | { 61 | NOT_IMPLEMENTED(); 62 | } 63 | 64 | 65 | void WINAPI GetConsoleMode() 66 | { 67 | NOT_IMPLEMENTED(); 68 | } 69 | 70 | 71 | void WINAPI GetConsoleOutputCP() 72 | { 73 | NOT_IMPLEMENTED(); 74 | } 75 | 76 | 77 | void WINAPI GetConsoleScreenBufferInfo() 78 | { 79 | NOT_IMPLEMENTED(); 80 | } 81 | 82 | 83 | void WINAPI GetConsoleTitleA() 84 | { 85 | NOT_IMPLEMENTED(); 86 | } 87 | 88 | 89 | void WINAPI FillConsoleOutputAttribute() 90 | { 91 | NOT_IMPLEMENTED(); 92 | } 93 | 94 | 95 | void WINAPI FillConsoleOutputCharacterA() 96 | { 97 | NOT_IMPLEMENTED(); 98 | } 99 | 100 | 101 | void WINAPI FreeConsole() 102 | { 103 | NOT_IMPLEMENTED(); 104 | } 105 | 106 | 107 | void WINAPI FlushConsoleInputBuffer() 108 | { 109 | NOT_IMPLEMENTED(); 110 | } 111 | 112 | 113 | void WINAPI PeekConsoleInputA() 114 | { 115 | NOT_IMPLEMENTED(); 116 | } 117 | 118 | 119 | void WINAPI GetNumberOfConsoleInputEvents() 120 | { 121 | NOT_IMPLEMENTED(); 122 | } 123 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * debug.c 3 | * 4 | * SGOS debugger. 5 | * 6 | * Copyright (C) 2008 Huang Guan 7 | * 8 | * 2008-01-31 Created. 9 | * 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | void print_error(char* file, char* function, int line, const char *fmt, ...) 17 | { 18 | va_list args; 19 | char printbuf[512]; 20 | int i; 21 | va_start(args, fmt); 22 | i=vsprintf( printbuf, fmt, args ); 23 | printbuf[i] = 0; 24 | va_end(args); 25 | printf("[%s]%s(%d): %s\n", file, function, line, printbuf); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/debug.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEBUG_H 2 | #define _DEBUG_H 3 | 4 | #include 5 | #include 6 | 7 | //#define RELEASE 8 | 9 | #ifndef RELEASE 10 | #define DBG(args ...) \ 11 | print_error( __FILE__, (char*)__func__, __LINE__, ##args ) 12 | #else 13 | #define DBG(args ...) 14 | //#define DBG printf 15 | #endif 16 | #define MSG printf 17 | void print_error(char* file, char* function, int line, const char *fmt, ...); 18 | 19 | #define NOT_IMPLEMENTED() DBG("## Not implemented.") 20 | 21 | #endif //_DEBUG_H 22 | 23 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/dir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI CreateDirectoryA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI CreateDirectoryExA() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI RemoveDirectoryA() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/dll.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI FreeLibrary(int mid) 7 | { 8 | return PsFreeModule( mid ); 9 | } 10 | 11 | 12 | int WINAPI LoadLibraryA(const char* filename) 13 | { 14 | return PsLoadModule( filename ); 15 | } 16 | 17 | 18 | int WINAPI LoadLibraryExA(const char* filename, int fd, uint flag) 19 | { 20 | DBG("Warning: Omitting flag %x", flag ); 21 | return LoadLibraryA(filename); 22 | } 23 | 24 | 25 | size_t WINAPI GetModuleFileNameA( int mid, const char* filename, int siz ) 26 | { 27 | NOT_IMPLEMENTED(); 28 | return 0; 29 | } 30 | 31 | //返回一个加载的模块的句柄 32 | //if input NULL, return the module to the file used to create the calling process 33 | int WINAPI GetModuleHandleA( const char* mname){ 34 | DBG("mname:%s", mname ); 35 | if( !mname ){ 36 | ProcessInformation* pi = GetCurrentProcessInformation(); 37 | return pi->ModuleId; 38 | } 39 | int mid = PsGetModule( mname, NULL, 0 ); 40 | return mid; 41 | } 42 | 43 | size_t WINAPI GetProcAddress( int mid, const char* name ) 44 | { 45 | DBG("mid:%x name:%s", mid, name ); 46 | size_t addr = PsGetProcedure( mid, name ); 47 | return addr; 48 | } 49 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/emul_handle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | #include "kernel.h" 5 | 6 | #define MAX_HANDLES_PER_PROCESS 1024 7 | #define HANDLE_MAGIC 0xFBFC1314 8 | 9 | typedef struct HandleInformationSet{ 10 | uint magic; 11 | struct HandleInformationSet* prev, *next; 12 | int type; 13 | uint data; 14 | uint mask; 15 | uint flag; 16 | }HandleInformation; 17 | 18 | 19 | uint _CreateHandle( int type, uint data ) 20 | { 21 | ProcessInformation* pi = GetCurrentProcessInformation(); 22 | if( pi ){ 23 | HandleInformation* hi = (HandleInformation*)malloc( sizeof(HandleInformation) ); 24 | if( hi==NULL ) 25 | return 0; 26 | hi->type = type; 27 | hi->data = data; 28 | hi->magic = HANDLE_MAGIC; 29 | hi->mask = hi->flag = 0; 30 | //## Lock here 31 | hi->next = pi->HandleSet; 32 | hi->prev = 0; 33 | if( pi->HandleSet ) 34 | ((HandleInformation*)pi->HandleSet)->prev = hi; 35 | pi->HandleSet = hi; 36 | //## Unlock 37 | return (uint)hi; 38 | } 39 | return 0; 40 | } 41 | 42 | uint _GetHandleType(uint h ) 43 | { 44 | switch( h ){ 45 | case STD_INPUT_HANDLE: 46 | case STD_OUTPUT_HANDLE: 47 | case STD_ERROR_HANDLE: 48 | return TYPE_CHAR; 49 | } 50 | HandleInformation* hi = (HandleInformation*)h; 51 | if( !hi || hi->magic != HANDLE_MAGIC ) 52 | return TYPE_UNKNOWN; 53 | return hi->type; 54 | } 55 | 56 | void _CloseHandle( uint h ) 57 | { 58 | ProcessInformation* pi = GetCurrentProcessInformation(); 59 | HandleInformation* hi = (HandleInformation*)h; 60 | if( !hi || hi->magic != HANDLE_MAGIC ) 61 | return ; 62 | switch( hi->type ){ 63 | case TYPE_FILE: 64 | FsCloseFile( (void*)hi->data ); 65 | break; 66 | case TYPE_CHAR: 67 | break; 68 | default: 69 | DBG("Unknown handle type."); 70 | } 71 | //## Lock here 72 | if( hi->next ) 73 | hi->next->prev = hi->prev; 74 | if( hi->prev ) 75 | hi->prev->next = hi->next; 76 | else 77 | pi->HandleSet = hi->next; 78 | //## Unlock here 79 | } 80 | 81 | uint _GetHandleData( uint h ) 82 | { 83 | HandleInformation* hi = (HandleInformation*)h; 84 | if( !hi || hi->magic != HANDLE_MAGIC ) 85 | return ; 86 | return hi->data; 87 | } 88 | 89 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/env.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI SetEnvironmentVariableA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI GetEnvironmentStrings() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | size_t WINAPI GetEnvironmentVariableA(const char* name, char* buf, size_t buf_len) 19 | { 20 | DBG("name: %s buf_len:0x%x", name, buf_len ); 21 | return 0; 22 | } 23 | 24 | 25 | void WINAPI FreeEnvironmentStringsA() 26 | { 27 | NOT_IMPLEMENTED(); 28 | } 29 | 30 | 31 | void WINAPI GetCommandLineA() 32 | { 33 | NOT_IMPLEMENTED(); 34 | } 35 | 36 | 37 | void WINAPI ExpandEnvironmentStringsA() 38 | { 39 | NOT_IMPLEMENTED(); 40 | } 41 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/error.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI SetLastError(uint code) 7 | { 8 | ThreadInformation* ti = GetCurrentThreadInformation(); 9 | if( ti ) 10 | ti->ErrorCode = code; 11 | } 12 | 13 | 14 | uint WINAPI GetLastError() 15 | { 16 | ThreadInformation* ti = GetCurrentThreadInformation(); 17 | if( ti ) 18 | return ti->ErrorCode; 19 | return -ERR_UNKNOWN; 20 | } 21 | 22 | 23 | void WINAPI SetErrorMode() 24 | { 25 | NOT_IMPLEMENTED(); 26 | } 27 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/event.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI OpenEventA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI CreateEventA() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | int WINAPI SetEvent(uint h) 19 | { 20 | if( h==0 ) 21 | return 0; 22 | NOT_IMPLEMENTED(); 23 | } 24 | 25 | 26 | void WINAPI ResetEvent() 27 | { 28 | NOT_IMPLEMENTED(); 29 | } 30 | 31 | 32 | void WINAPI PulseEvent() 33 | { 34 | NOT_IMPLEMENTED(); 35 | } 36 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/filetime.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI CompareFileTime() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI SetFileTime() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI SystemTimeToFileTime() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/find.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI FindClose() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI FindFirstChangeNotificationA() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI FindFirstFileA() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI FindNextChangeNotification() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | void WINAPI FindNextFileA() 31 | { 32 | NOT_IMPLEMENTED(); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/handle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | #include "kernel.h" 5 | 6 | 7 | uint WINAPI GetStdHandle(uint type) 8 | { 9 | switch( type ){ 10 | case STD_INPUT_HANDLE: 11 | return type; 12 | case STD_OUTPUT_HANDLE: 13 | return type; 14 | case STD_ERROR_HANDLE: 15 | return type; 16 | default: 17 | DBG("Unknown handle type: %x", type ); 18 | } 19 | return 0; 20 | } 21 | 22 | 23 | void WINAPI SetHandleInformation() 24 | { 25 | NOT_IMPLEMENTED(); 26 | } 27 | 28 | 29 | void WINAPI SetStdHandle() 30 | { 31 | NOT_IMPLEMENTED(); 32 | } 33 | 34 | 35 | void WINAPI CloseHandle() 36 | { 37 | NOT_IMPLEMENTED(); 38 | } 39 | 40 | 41 | void WINAPI DuplicateHandle() 42 | { 43 | NOT_IMPLEMENTED(); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/kernel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/kernel.h: -------------------------------------------------------------------------------- 1 | #ifndef _HANDLE_ 2 | 3 | #define STD_INPUT_HANDLE -10 4 | #define STD_OUTPUT_HANDLE -11 5 | #define STD_ERROR_HANDLE -12 6 | 7 | 8 | typedef struct _STARTUPINFO { 9 | uint cb; 10 | char* lpReserved; 11 | char* lpDesktop; 12 | char* lpTitle; 13 | uint dwX; 14 | uint dwY; 15 | uint dwXSize; 16 | uint dwYSize; 17 | uint dwXCountChars; 18 | uint dwYCountChars; 19 | uint dwFillAttribute; 20 | uint dwFlags; 21 | ushort wShowWindow; 22 | ushort cbReserved2; 23 | uchar* lpReserved2; 24 | uint hStdInput; 25 | uint hStdOutput; 26 | uint hStdError; 27 | } STARTUPINFO, *LPSTARTUPINFO; 28 | 29 | enum HandleType{ 30 | TYPE_UNKNOWN = 0, 31 | TYPE_DISK = 0x0001, 32 | TYPE_CHAR = 0x0002, 33 | TYPE_PIPE = 0x0003, 34 | TYPE_MODULE = 0x0004, 35 | TYPE_FILE, 36 | TYPE_PROCESS, 37 | TYPE_THREAD, 38 | TYPE_EVENT 39 | }; 40 | 41 | uint _CreateHandle( int type, uint data ); 42 | void _CloseHandle( uint h ); 43 | uint _GetHandleData( uint h ); 44 | uint _GetHandleType(uint h ); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/mapping.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI CreateFileMappingA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI MapViewOfFile() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI MapViewOfFileEx() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI OpenFileMappingA() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | void WINAPI UnmapViewOfFile() 31 | { 32 | NOT_IMPLEMENTED(); 33 | } 34 | 35 | 36 | void WINAPI FlushViewOfFile() 37 | { 38 | NOT_IMPLEMENTED(); 39 | } 40 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/mem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI VirtualAlloc() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI VirtualFree() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI VirtualProtect() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI VirtualQuery() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | void WINAPI GlobalAlloc() 31 | { 32 | NOT_IMPLEMENTED(); 33 | } 34 | 35 | 36 | void WINAPI GlobalFree() 37 | { 38 | NOT_IMPLEMENTED(); 39 | } 40 | 41 | 42 | void WINAPI GlobalLock() 43 | { 44 | NOT_IMPLEMENTED(); 45 | } 46 | 47 | 48 | void WINAPI GlobalUnlock() 49 | { 50 | NOT_IMPLEMENTED(); 51 | } 52 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/mutex.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI OpenMutexA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI OpenSemaphoreA() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI LeaveCriticalSection() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI EnterCriticalSection() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | void WINAPI CreateMutexA() 31 | { 32 | NOT_IMPLEMENTED(); 33 | } 34 | 35 | 36 | void WINAPI CreateSemaphoreA() 37 | { 38 | NOT_IMPLEMENTED(); 39 | } 40 | 41 | 42 | void WINAPI DeleteCriticalSection() 43 | { 44 | NOT_IMPLEMENTED(); 45 | } 46 | 47 | 48 | void WINAPI ReleaseMutex() 49 | { 50 | NOT_IMPLEMENTED(); 51 | } 52 | 53 | 54 | void WINAPI ReleaseSemaphore() 55 | { 56 | NOT_IMPLEMENTED(); 57 | } 58 | 59 | 60 | void WINAPI InitializeCriticalSection() 61 | { 62 | NOT_IMPLEMENTED(); 63 | } 64 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/object.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI WaitForMultipleObjects() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI WaitForSingleObject() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/performance.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI QueryPerformanceCounter() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | int WINAPI QueryPerformanceFrequency(int* freq) 13 | { 14 | return 0; //failed. 15 | } 16 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI CreatePipe() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI PeekNamedPipe() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI SetNamedPipeHandleState() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/process.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | #include "kernel.h" 5 | 6 | void WINAPI CreateProcessA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI ExitProcess() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI TerminateProcess() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI OpenProcess() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | uint WINAPI GetCurrentProcessId() 31 | { 32 | ProcessInformation* pi = GetCurrentProcessInformation(); 33 | if( pi && pi->ProcessId ) 34 | return pi->ProcessId; 35 | return 0; 36 | } 37 | 38 | 39 | uint WINAPI GetCurrentProcess() 40 | { 41 | uint pid = GetCurrentProcessId(); 42 | if( pid ) 43 | return _CreateHandle( TYPE_PROCESS, pid ); 44 | return 0; 45 | } 46 | 47 | 48 | void WINAPI WriteProcessMemory() 49 | { 50 | NOT_IMPLEMENTED(); 51 | } 52 | 53 | 54 | void WINAPI GetProcessTimes() 55 | { 56 | NOT_IMPLEMENTED(); 57 | } 58 | 59 | 60 | void WINAPI GetExitCodeProcess() 61 | { 62 | NOT_IMPLEMENTED(); 63 | } 64 | 65 | 66 | void WINAPI ReadProcessMemory() 67 | { 68 | NOT_IMPLEMENTED(); 69 | } 70 | 71 | 72 | void WINAPI GetPriorityClass() 73 | { 74 | NOT_IMPLEMENTED(); 75 | } 76 | 77 | 78 | void WINAPI SetPriorityClass() 79 | { 80 | NOT_IMPLEMENTED(); 81 | } 82 | 83 | 84 | void WINAPI GetCurrentDirectoryA() 85 | { 86 | NOT_IMPLEMENTED(); 87 | } 88 | 89 | 90 | void WINAPI SetCurrentDirectoryA() 91 | { 92 | NOT_IMPLEMENTED(); 93 | } 94 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/sleep.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI Sleep() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/systeminfo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI GetSystemDirectoryA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI GetSystemInfo() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI GetSystemTimeAsFileTime() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI GetTimeZoneInformation() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | void WINAPI GetACP() 31 | { 32 | NOT_IMPLEMENTED(); 33 | } 34 | 35 | 36 | void WINAPI GetComputerNameA() 37 | { 38 | NOT_IMPLEMENTED(); 39 | } 40 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/tape.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI PrepareTape() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI SetTapeParameters() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI SetTapePosition() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI GetTapeParameters() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | void WINAPI GetTapePosition() 31 | { 32 | NOT_IMPLEMENTED(); 33 | } 34 | 35 | 36 | void WINAPI EraseTape() 37 | { 38 | NOT_IMPLEMENTED(); 39 | } 40 | 41 | 42 | void WINAPI WriteTapemark() 43 | { 44 | NOT_IMPLEMENTED(); 45 | } 46 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/thread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | #include "kernel.h" 5 | 6 | void WINAPI ExitThread() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI CreateThread() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI SetThreadContext() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI SetThreadPriority() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | 29 | 30 | void WINAPI TerminateThread() 31 | { 32 | NOT_IMPLEMENTED(); 33 | } 34 | 35 | 36 | void WINAPI ResumeThread() 37 | { 38 | NOT_IMPLEMENTED(); 39 | } 40 | 41 | 42 | void WINAPI SuspendThread() 43 | { 44 | NOT_IMPLEMENTED(); 45 | } 46 | 47 | 48 | void WINAPI GetThreadContext() 49 | { 50 | NOT_IMPLEMENTED(); 51 | } 52 | 53 | 54 | void WINAPI GetStartupInfoA(STARTUPINFO* startupinfo) 55 | { 56 | startupinfo->hStdInput = STD_INPUT_HANDLE; 57 | startupinfo->hStdOutput = STD_OUTPUT_HANDLE; 58 | startupinfo->hStdError = STD_ERROR_HANDLE; 59 | } 60 | 61 | 62 | uint WINAPI GetCurrentThreadId() 63 | { 64 | ThreadInformation* ti = GetCurrentThreadInformation(); 65 | if( ti && ti->ThreadId) 66 | return ti->ThreadId; 67 | return SysGetCurrentThreadId(); 68 | } 69 | 70 | uint WINAPI GetCurrentThread() 71 | { 72 | uint tid = GetCurrentThreadId(); 73 | if( tid ) 74 | return _CreateHandle( TYPE_THREAD, tid ); 75 | return 0; 76 | } 77 | 78 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/time.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI SetSystemTime() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | int WINAPI GetTickCount() 13 | { 14 | return 0; 15 | } 16 | 17 | 18 | void WINAPI GetSystemTime() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/tls.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "debug.h" 5 | 6 | #define LOCAL_STORAGE_SIZE 64 7 | typedef struct LocalStorageUnit{ 8 | uchar used; 9 | void* value; 10 | }LocalStorageUnit; 11 | 12 | uint WINAPI TlsAlloc() 13 | { 14 | int i; 15 | ThreadInformation* ti= GetCurrentThreadInformation(); 16 | if( ti->LocalStorage == NULL ){ 17 | ti->LocalStorage = malloc( sizeof(LocalStorageUnit) * LOCAL_STORAGE_SIZE ); 18 | memset( ti->LocalStorage, 0, sizeof(LocalStorageUnit) * LOCAL_STORAGE_SIZE ); 19 | if( ti->LocalStorage == NULL ) 20 | return 0xFFFFFFFF; 21 | } 22 | for( i=0; iLocalStorage)[i].used ){ 24 | ((LocalStorageUnit*)ti->LocalStorage)[i].used = 1; 25 | return i; 26 | } 27 | return 0xFFFFFFFF; 28 | } 29 | 30 | 31 | void WINAPI TlsFree(uint i) 32 | { 33 | ThreadInformation* ti= GetCurrentThreadInformation(); 34 | ((LocalStorageUnit*)ti->LocalStorage)[i].used = 0; 35 | } 36 | 37 | 38 | void* WINAPI TlsGetValue(uint i) 39 | { 40 | ThreadInformation* ti= GetCurrentThreadInformation(); 41 | return ((LocalStorageUnit*)ti->LocalStorage)[i].value; 42 | } 43 | 44 | 45 | void WINAPI TlsSetValue(uint i, void* v) 46 | { 47 | ThreadInformation* ti= GetCurrentThreadInformation(); 48 | ((LocalStorageUnit*)ti->LocalStorage)[i].value = v; 49 | } 50 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/util.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/apps/wkernel/util.c -------------------------------------------------------------------------------- /sgos2/apps/wkernel/version.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | typedef struct _OSVERSIONINFO { 6 | uint dwOSVersionInfoSize; 7 | uint dwMajorVersion; 8 | uint dwMinorVersion; 9 | uint dwBuildNumber; 10 | uint dwPlatformId; 11 | uchar szCSDVersion[128]; 12 | } OSVERSIONINFO; 13 | 14 | void WINAPI GetVersion() 15 | { 16 | NOT_IMPLEMENTED(); 17 | } 18 | 19 | 20 | //Simulate a Windows XP 21 | int WINAPI GetVersionExA( OSVERSIONINFO * info ) 22 | { 23 | if( info->dwOSVersionInfoSize < sizeof(OSVERSIONINFO) ){ 24 | DBG("info buffer too small."); 25 | return 0; 26 | } 27 | info->dwMajorVersion = 5; 28 | info->dwMinorVersion = 1; 29 | info->dwBuildNumber = 0; 30 | info->dwPlatformId = 2; 31 | strcpy( info->szCSDVersion, "Service Pack -1" ); 32 | return sizeof(OSVERSIONINFO); 33 | } 34 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/volume.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI GetVolumeInformationA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | 11 | 12 | void WINAPI GetDiskFreeSpaceA() 13 | { 14 | NOT_IMPLEMENTED(); 15 | } 16 | 17 | 18 | void WINAPI GetDriveTypeA() 19 | { 20 | NOT_IMPLEMENTED(); 21 | } 22 | 23 | 24 | void WINAPI GetLogicalDrives() 25 | { 26 | NOT_IMPLEMENTED(); 27 | } 28 | -------------------------------------------------------------------------------- /sgos2/apps/wkernel/windbg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "debug.h" 4 | 5 | 6 | void WINAPI OutputDebugStringA() 7 | { 8 | NOT_IMPLEMENTED(); 9 | } 10 | -------------------------------------------------------------------------------- /sgos2/apps/wntdll/Makefile: -------------------------------------------------------------------------------- 1 | #sgos api 2 | 3 | CCFLAGS = -g -Werror -I../include -I../../include -fno-builtin -ffreestanding -fleading-underscore 4 | LDFLAGS = ../../api/api.dll 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = ntdll 9 | 10 | CC = gcc $(CCFLAGS) 11 | LDLIB = ld -r $(LDFLAGS) 12 | LD = ld --image-base 0x7F900000 -shared $(LDFLAGS) 13 | OD = objdump $(ODFLAGS) 14 | WF = ../../tools/wf/wf 15 | IMAGE = ../../sgos2.img 16 | 17 | OBJECTS = $(patsubst %.c,%.o, $(wildcard *.c)) 18 | 19 | all: $(PROGRAM).dll 20 | 21 | $(PROGRAM).dll: $(OBJECTS) 22 | $(LD) $(OBJECTS) -o$(PROGRAM).dll 23 | $(OD) $(PROGRAM).dll > $(PROGRAM).dmp 24 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 25 | 26 | # Standard Procedures 27 | .c.o: 28 | $(CC) -c -o $@ $< 29 | 30 | clean: 31 | $(RM) $(OBJECTS) 32 | $(RM) $(PROGRAM).a 33 | $(RM) $(PROGRAM).dmp 34 | $(RM) $(PROGRAM).dll -------------------------------------------------------------------------------- /sgos2/apps/wntdll/debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * debug.c 3 | * 4 | * SGOS debugger. 5 | * 6 | * Copyright (C) 2008 Huang Guan 7 | * 8 | * 2008-01-31 Created. 9 | * 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | void print_error(char* file, char* function, int line, const char *fmt, ...) 17 | { 18 | va_list args; 19 | char printbuf[512]; 20 | int i; 21 | va_start(args, fmt); 22 | i=vsprintf( printbuf, fmt, args ); 23 | printbuf[i] = 0; 24 | va_end(args); 25 | printf("[%s]%s(%d): %s\n", file, function, line, printbuf); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /sgos2/apps/wntdll/debug.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEBUG_H 2 | #define _DEBUG_H 3 | 4 | #include 5 | #include 6 | 7 | //#define RELEASE 8 | 9 | #ifndef RELEASE 10 | #define DBG(args ...) \ 11 | print_error( __FILE__, (char*)__func__, __LINE__, ##args ) 12 | #else 13 | #define DBG(args ...) 14 | //#define DBG printf 15 | #endif 16 | #define MSG printf 17 | void print_error(char* file, char* function, int line, const char *fmt, ...); 18 | 19 | #define NOT_IMPLEMENTED() DBG("## Not implemented.") 20 | 21 | #endif //_DEBUG_H 22 | 23 | -------------------------------------------------------------------------------- /sgos2/apps/wntdll/fun.txt: -------------------------------------------------------------------------------- 1 | NtAdjustPrivilegesToken 2 | NtClose 3 | NtCreateDirectoryObject 4 | NtCreateEvent 5 | NtCreateFile 6 | NtCreateMailslotFile 7 | NtCreateMutant 8 | NtCreateSection 9 | NtCreateSemaphore 10 | NtCreateToken 11 | NtFsControlFile 12 | NtLockVirtualMemory 13 | NtMapViewOfSection 14 | NtNotifyChangeDirectoryFile 15 | NtOpenDirectoryObject 16 | NtOpenEvent 17 | NtOpenFile 18 | NtOpenMutant 19 | NtOpenSection 20 | NtOpenSemaphore 21 | NtQueryAttributesFile 22 | NtQueryDirectoryFile 23 | NtQueryDirectoryObject 24 | NtQueryEaFile 25 | NtQueryEvent 26 | NtQueryFullAttributesFile 27 | NtQueryInformationFile 28 | NtQueryInformationProcess 29 | NtQueryObject 30 | NtQuerySecurityObject 31 | NtQuerySystemInformation 32 | NtQueryVirtualMemory 33 | NtQueryVolumeInformationFile 34 | NtReadFile 35 | NtSetEaFile 36 | NtSetInformationFile 37 | NtSetSecurityObject 38 | NtUnlockVirtualMemory 39 | NtUnmapViewOfSection 40 | NtWriteFile 41 | RtlAcquirePebLock 42 | RtlAppendUnicodeStringToString 43 | RtlAppendUnicodeToString 44 | RtlCompareUnicodeString 45 | RtlConvertSidToUnicodeString 46 | RtlCopyUnicodeString 47 | RtlCreateUnicodeStringFromAsciiz 48 | RtlDowncaseUnicodeString 49 | RtlEqualUnicodeString 50 | RtlFreeUnicodeString 51 | RtlInitUnicodeString 52 | RtlIsDosDeviceName_U 53 | RtlNtStatusToDosError 54 | RtlPrefixUnicodeString 55 | RtlReleasePebLock 56 | RtlSecondsSince1970ToTime 57 | RtlUpcaseUnicodeChar 58 | RtlUpcaseUnicodeString 59 | -------------------------------------------------------------------------------- /sgos2/apps/wprocess/Makefile: -------------------------------------------------------------------------------- 1 | #sgos apps 2 | 3 | CXXFLAGS = -g -Werror -I../../include/c++ -I../../include -nostdlib -fleading-underscore --no-exceptions 4 | LDFLAGS = 5 | ODFLAGS = -S 6 | 7 | #here defines the program information 8 | PROGRAM = wprocess 9 | LDFLAGS += ../../crt/crt.a ../../api/api.a 10 | 11 | CXX = g++ $(CXXFLAGS) 12 | LD = ld $(LDFLAGS) 13 | OD = objdump $(ODFLAGS) 14 | LD2 = ../../tools/ld2/ld2 15 | WF = ../../tools/wf/wf 16 | IMAGE = ../../sgos2.img 17 | 18 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) 19 | 20 | all: $(PROGRAM).exe 21 | 22 | $(PROGRAM).exe: $(OBJECTS) 23 | $(LD) $(OBJECTS) -o$(PROGRAM).exe 24 | $(OD) $(PROGRAM).exe > $(PROGRAM).dmp 25 | $(WF) $(IMAGE) -src $@ -dest sgos/$@ 26 | 27 | # Standard Procedures 28 | .c.o: 29 | $(CXX) -c -o $@ $< 30 | 31 | clean: 32 | $(RM) $(OBJECTS) 33 | $(RM) $(PROGRAM).dmp 34 | $(RM) $(PROGRAM).exe 35 | -------------------------------------------------------------------------------- /sgos2/apps/wprocess/module.h: -------------------------------------------------------------------------------- 1 | #ifndef _MODULE_H 2 | #define _MODULE_H 3 | 4 | #include "pe.h" 5 | 6 | #define MAX_IMPORT_MODULES 64 7 | 8 | typedef struct ModuleInSpace{ 9 | struct ModuleInSpace* prev, *next; 10 | uint SpaceId; 11 | size_t VirtualAddress; //Load address 12 | }ModuleInSpace; 13 | 14 | typedef struct PeModule{ 15 | uint ModuleId; 16 | char Path[PATH_LEN]; //模块绝对路径 17 | ushort Reference; 18 | size_t LoadAddress; 19 | size_t ImageSize; 20 | size_t ImageBase; 21 | size_t EntryAddress; 22 | size_t StackLimit; 23 | IMAGE_OPTIONAL_HEADER OptionalHeader; 24 | PeModule** ImportModules; 25 | int ImportModuleCount; 26 | ModuleInSpace* LoadedInformation; 27 | }PeModule; 28 | 29 | PeModule* AllocateModule( const char* path ); 30 | void FreeModule( PeModule* mo, uint spaceId ); 31 | size_t GetProcedureAddress( PeModule* mo, ModuleInSpace* mi, const char* procedureName ); 32 | int LinkModuleToSpace( PeModule* mo, uint spaceId ); 33 | ModuleInSpace* GetLoadedInformation( PeModule* mo, uint space ); 34 | PeModule* GetModuleById( uint id ); 35 | PeModule* GetModuleByPath( const char* path ); 36 | PeModule* GetModuleByName( const char* name ); 37 | 38 | int PeLoadLibrary( uint spaceId, const char * path ); 39 | int PeUnloadLibrary( uint spaceId, uint mid ); 40 | size_t PeGetProcedureAddress( uint spaceId, uint mid, const char* name ); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /sgos2/apps/wprocess/parser.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/apps/wprocess/parser.cpp -------------------------------------------------------------------------------- /sgos2/apps/wprocess/wprocess.h: -------------------------------------------------------------------------------- 1 | #ifndef _WPROCESS_H 2 | #define _WPROCESS_H 3 | 4 | #include 5 | #include "module.h" 6 | 7 | class Process; 8 | class Thread{ 9 | private: 10 | uint threadId; 11 | uint spaceId; 12 | ThreadInformation* ti; 13 | uint exitCode; 14 | Thread* next, *prev; 15 | bool disposed; 16 | Process* process; 17 | public: 18 | Thread( Process * ps, size_t entry ); 19 | ~Thread(); 20 | void Dispose(); 21 | int Terminate( int code ); 22 | void Resume(); 23 | void Suspend(); 24 | uint ThreadId(){ 25 | return threadId; 26 | } 27 | Thread* NextThread(){ 28 | return next; 29 | } 30 | }; 31 | 32 | class Process{ 33 | private: 34 | uint spaceId; 35 | uint moduleId; 36 | bool disposed; 37 | char* commandLine; 38 | char* environment; 39 | PeModule* module; 40 | int exitCode; 41 | ProcessInformation* pi; 42 | int threadCount; 43 | char modulePath[PATH_LEN]; 44 | Process *prev, *next, *parent, *child; 45 | static Process* EnumProcessTree( Process* p, uint id ); 46 | static void KillProcessChildren( Process* p ); 47 | public: 48 | Thread* mainThread; 49 | static Process* GetProcessById( uint id ); 50 | Thread* GetThreadById( uint tid ); 51 | Process( uint pid, char* cmdline, char* env ); 52 | ~Process(); 53 | int Initialize( uint pid, char* cmdline, char* env ); 54 | void Terminate( int code ); 55 | void Resume(); 56 | void Suspend(); 57 | Thread* CreateThread( size_t entry ); 58 | void Dispose(); 59 | uint SpaceId(){ 60 | return spaceId; 61 | } 62 | uint ProcessId(){ 63 | return spaceId; 64 | } 65 | PeModule* GetModule(){ 66 | return module; 67 | } 68 | ProcessInformation* GetInformation(){ 69 | return pi; 70 | } 71 | }; 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /sgos2/crt/Makefile: -------------------------------------------------------------------------------- 1 | #sgos api 2 | 3 | CCFLAGS = -g -fno-builtin -nostdlib -fleading-underscore -I../include 4 | ODFLAGS = -S 5 | LDFLAGS = -r 6 | 7 | #here defines the program information 8 | PROGRAM = crt 9 | 10 | CC = g++ $(CCFLAGS) 11 | LD = ld $(LDFLAGS) 12 | 13 | OBJECTS = $(patsubst %.cpp,%.o, $(wildcard *.cpp)) \ 14 | $(patsubst %.S,%.o, $(wildcard *.S)) 15 | 16 | all: $(PROGRAM).a 17 | 18 | $(PROGRAM).a: $(OBJECTS) 19 | $(LD) -o $(PROGRAM).a $(OBJECTS) 20 | 21 | # Standard Procedures 22 | .cpp.o: 23 | $(CC) -c -o $@ $< 24 | 25 | clean: 26 | $(RM) $(OBJECTS) 27 | $(RM) $(PROGRAM).a 28 | -------------------------------------------------------------------------------- /sgos2/crt/alloca.S: -------------------------------------------------------------------------------- 1 | /* 2 | The better way to do this is to touch every page 3 | when we decrement the stack pointer. 4 | Note: gcc will store the size in eax. 5 | */ 6 | .global __alloca 7 | __alloca: 8 | pushl %edx 9 | movl %esp, %edx 10 | addl $0x8, %edx 11 | subl %eax, %edx 12 | movl %esp, %eax 13 | movl %edx, %esp 14 | movl (%eax), %edx 15 | movl 4(%eax), %eax 16 | jmp *%eax 17 | -------------------------------------------------------------------------------- /sgos2/crt/ctors.S: -------------------------------------------------------------------------------- 1 | .global _get_ctors 2 | .global _get_dtors 3 | _get_ctors: 4 | lea __CTOR_LIST__, %eax 5 | ret 6 | 7 | _get_dtors: 8 | lea __DTOR_LIST__, %eax 9 | ret 10 | -------------------------------------------------------------------------------- /sgos2/crt/exception.cpp: -------------------------------------------------------------------------------- 1 | #define PERSONALITY_FUNCTION __gxx_personality_sj0 2 | 3 | typedef char _Unwind_Exception_Class[8]; 4 | 5 | typedef enum 6 | { 7 | _URC_OK = 0, /* operation completed successfully */ 8 | _URC_FOREIGN_EXCEPTION_CAUGHT = 1, 9 | _URC_END_OF_STACK = 5, 10 | _URC_HANDLER_FOUND = 6, 11 | _URC_INSTALL_CONTEXT = 7, 12 | _URC_CONTINUE_UNWIND = 8, 13 | _URC_FAILURE = 9 /* unspecified failure of some kind */ 14 | }_Unwind_Reason_Code; 15 | 16 | typedef enum 17 | { 18 | _US_VIRTUAL_UNWIND_FRAME = 0, 19 | _US_UNWIND_FRAME_STARTING = 1, 20 | _US_UNWIND_FRAME_RESUME = 2, 21 | _US_ACTION_MASK = 3, 22 | _US_FORCE_UNWIND = 8, 23 | _US_END_OF_STACK = 16 24 | }_Unwind_State; 25 | 26 | struct _Unwind_Exception 27 | { 28 | unsigned exception_class __attribute__((__mode__(__DI__))); 29 | void * exception_cleanup; 30 | unsigned private_1 __attribute__((__mode__(__word__))); 31 | unsigned private_2 __attribute__((__mode__(__word__))); 32 | } __attribute__((__aligned__)); 33 | 34 | struct _Unwind_Context { 35 | unsigned entry_regs[16]; 36 | unsigned current_regs[16]; 37 | unsigned cfa; 38 | unsigned pc; 39 | unsigned ra; 40 | void *fde; 41 | void* pfn; 42 | unsigned func; 43 | void *lsda; 44 | unsigned range; 45 | }; 46 | 47 | /* Provided only for for compatibility with existing code. */ 48 | typedef int _Unwind_Action; 49 | #define _UA_SEARCH_PHASE 1 50 | #define _UA_CLEANUP_PHASE 2 51 | #define _UA_HANDLER_FRAME 4 52 | #define _UA_FORCE_UNWIND 8 53 | #define _UA_END_OF_STACK 16 54 | #define _URC_NO_REASON _URC_OK 55 | 56 | extern "C" int printf(...); 57 | 58 | extern "C" _Unwind_Reason_Code PERSONALITY_FUNCTION (int version, 59 | _Unwind_Action actions, 60 | _Unwind_Exception_Class exception_class, 61 | struct _Unwind_Exception *ue_header, 62 | struct _Unwind_Context *context) 63 | { 64 | printf("ok\n"); 65 | return _URC_OK; 66 | } 67 | 68 | extern "C" void _Unwind_SjLj_Register() 69 | { 70 | } 71 | 72 | extern "C" void _Unwind_SjLj_Resume() 73 | { 74 | } 75 | 76 | extern "C" void _Unwind_SjLj_Unregister() 77 | { 78 | } 79 | -------------------------------------------------------------------------------- /sgos2/crt/exit.cpp: -------------------------------------------------------------------------------- 1 | typedef struct FUNC_LIST{ 2 | struct FUNC_LIST* next; 3 | void* func; 4 | }FUNC_LIST; 5 | 6 | static FUNC_LIST* first; 7 | 8 | extern "C" void* malloc(unsigned int ); 9 | extern "C" void free(void*p); 10 | //Not support multithreading yet. 11 | extern "C" void atexit(void* p) 12 | { 13 | FUNC_LIST* f = (FUNC_LIST*)malloc( sizeof(FUNC_LIST) ); 14 | if( f==(void*)0 ) return; 15 | f->func = p; 16 | //Note: Multithreading is not supported here!! 17 | f->next = first; 18 | first = f; 19 | } 20 | 21 | extern "C" void __run_exit_func() 22 | { 23 | FUNC_LIST* f; 24 | for( f=first; f; f=f->next ) 25 | if(f->func) 26 | ((void(*)())f->func)(); 27 | } 28 | -------------------------------------------------------------------------------- /sgos2/crt/init.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //overload the operator "new" 3 | extern "C" unsigned * get_ctors(); 4 | extern "C" unsigned * get_dtors(); 5 | 6 | //for MINGW 7 | extern "C" void __main() 8 | { 9 | } 10 | extern "C" int main(); 11 | extern "C" void __run_exit_func(); 12 | extern "C" void __allocation_init(); 13 | extern "C" void SysExitThread(uint ); 14 | extern "C" ProcessInformation* GetCurrentProcessInformation(); 15 | extern "C" int mainCRTStartup() 16 | { 17 | int ret; 18 | unsigned *p; 19 | // init memory 20 | __allocation_init(); 21 | // call ctors 22 | p = get_ctors(); 23 | for( p=&p[1]; *p; p++ ) 24 | ((void(*)())(*p))(); 25 | ret = main(); 26 | // call atexit 27 | __run_exit_func(); 28 | // call dtors 29 | p = get_dtors(); 30 | for( p=&p[1]; *p; p++ ) 31 | ((void(*)())(*p))(); 32 | SysExitThread((uint)ret); 33 | // return 34 | return ret; 35 | } 36 | -------------------------------------------------------------------------------- /sgos2/crt/memory.cpp: -------------------------------------------------------------------------------- 1 | 2 | extern "C" void* malloc(unsigned int ); 3 | extern "C" void free(void*p); 4 | 5 | void * operator new (unsigned int size) 6 | { 7 | return (void*)malloc(size); 8 | } 9 | 10 | //overload the operator "new[]" 11 | void * operator new[] (unsigned int size) 12 | { 13 | return (void*)malloc(size); 14 | } 15 | 16 | //overload the operator "delete" 17 | void operator delete (void * p) 18 | { 19 | free(p); 20 | } 21 | 22 | //overload the operator "delete[]" 23 | void operator delete[] (void * p) 24 | { 25 | free(p); 26 | } 27 | -------------------------------------------------------------------------------- /sgos2/crt/virtual.cpp: -------------------------------------------------------------------------------- 1 | 2 | //dummy function, do nothing! 3 | extern "C" void __cxa_pure_virtual() 4 | { 5 | // print error message 6 | } 7 | -------------------------------------------------------------------------------- /sgos2/doc/BXML.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/doc/BXML.doc -------------------------------------------------------------------------------- /sgos2/doc/SGOS2内核代码命名约定.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/doc/SGOS2内核代码命名约定.doc -------------------------------------------------------------------------------- /sgos2/doc/SGOS2内核内存管理.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/doc/SGOS2内核内存管理.doc -------------------------------------------------------------------------------- /sgos2/doc/SGOS2可执行文件结构说明.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/doc/SGOS2可执行文件结构说明.doc -------------------------------------------------------------------------------- /sgos2/doc/SGOS2开发文档1.9.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/doc/SGOS2开发文档1.9.doc -------------------------------------------------------------------------------- /sgos2/doc/SGOS2程序创建与运行过程.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/doc/SGOS2程序创建与运行过程.doc -------------------------------------------------------------------------------- /sgos2/include/c++/basestring.h: -------------------------------------------------------------------------------- 1 | // system string 2 | 3 | // 临时如此山寨一下,以后有空再抽象出一个子类。 4 | // 按照ib所说:typedef std::basic_string string; 5 | class string 6 | { 7 | private: 8 | char *buf; 9 | int len; 10 | int buf_size; 11 | void init(); 12 | void mksize( int siz ); 13 | void copy( const char* str ); 14 | public: 15 | string(); 16 | string( const char *src ); 17 | //拷贝函数 18 | string( const string &src ); 19 | ~string(); 20 | //操作符重载 21 | char& operator [] ( int index ); 22 | string& operator = ( const char *src ); 23 | string& operator = ( const string& s ); 24 | bool operator == ( const char *src ); 25 | bool operator == ( const string& s ); 26 | string& operator + ( const string& add2 ); 27 | string& operator + ( const char* src ); 28 | string& operator += ( const char* src ); 29 | string& operator += ( const string& add2 ); 30 | operator char *(); 31 | operator int(); 32 | //成员函数 33 | const char *cstr(); 34 | char charAt( int index ); 35 | string& fromInteger( int number ); 36 | int length(); 37 | int find( string substr , int start=0 ); 38 | int rfind( string substr ); 39 | string trim( char part ); 40 | string left( int sublen ); 41 | string right( int sublen ); 42 | string mid( int start , int sublen ); 43 | string replace( string src , string dest ); 44 | }; 45 | -------------------------------------------------------------------------------- /sgos2/include/c++/messenger.h: -------------------------------------------------------------------------------- 1 | #ifndef _MESSENGER_H_ 2 | #define _MESSENGER_H_ 3 | 4 | #include 5 | #include 6 | // 临时对msg进行封装,以后可以改进,直接使用系统调用。 7 | class Messenger{ 8 | private: 9 | messenger_t msg; 10 | uint flag; //send/recv flag 11 | public: 12 | Messenger(); 13 | Messenger( const char* str ); 14 | ~Messenger(); 15 | //useful functions 16 | // 得到一个回复用的Messenger 17 | Messenger reply(); 18 | // 获取发送者信息 19 | uint getSenderThread(); 20 | uint getSenderProcess(); 21 | // 添加一个兄弟节点 22 | Messenger& append( const char* name ); 23 | // 解析一个消息文本 24 | int parse( const char* str ); 25 | // 创建一个路径,并且转到创建的路径 26 | int mkdir( const char* path ); 27 | // 重定位当前路径 28 | int redir( const char* path ); 29 | // 移动到下一项 30 | int moveNext(); 31 | // 提交一个同步请求 32 | int request(); 33 | // 发送消息,异步 34 | int send(); 35 | // 接收消息 36 | int receive(bool pending = true); 37 | // 读操作 38 | int read( const char* path, void* buf, uint buf_size ); 39 | // 读节点名 40 | const char* readName( const char* path ); 41 | // 写操作 42 | int write( const char* path, const void* buf, uint buf_size ); 43 | // 读取一个字符串 44 | char* getString( const char* path ); 45 | int getInt( const char* path ); 46 | uint getUInt( const char* path ); 47 | short getShort( const char* path ); 48 | ushort getUShort( const char* path ); 49 | char getChar( const char* path ); 50 | uchar getUChar( const char* path ); 51 | uchar getByte( const char* path ); 52 | // 写入一个字符串 53 | int putString( const char* path, const char* str ); 54 | int putInt( const char* path, int v ); 55 | int putUInt( const char* path, uint v ); 56 | int putShort( const char* path, short v ); 57 | int putUShort( const char* path, ushort v ); 58 | int putChar( const char* path, char v ); 59 | int putUChar( const char* path, uchar v ); 60 | }; 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /sgos2/include/c++/service.h: -------------------------------------------------------------------------------- 1 | #ifndef _SERVICE_H_ 2 | 3 | namespace Service{ 4 | //IO操作 5 | uchar inbyte( ushort port ); 6 | ushort inword( ushort port ); 7 | uint indword( uint port ); 8 | void outbyte( ushort port, uchar v ); 9 | void outword( ushort port, ushort v ); 10 | void outdword( ushort port, uint v ); 11 | int MapMemory( size_t vaddr, size_t paddr, size_t siz, uint flag ); 12 | int UnmapMemory( size_t vaddr, size_t siz ); 13 | int CallBIOS( int interrupt, void* tc ); 14 | int CallBIOS( int interrupt, int& eax, int& ebx, int& ecx, int& edx ); 15 | }; 16 | 17 | #endif 18 | 19 | -------------------------------------------------------------------------------- /sgos2/include/c++/system.h: -------------------------------------------------------------------------------- 1 | #ifndef _SYSTEM_ 2 | #define _SYSTEM_ 3 | 4 | #include 5 | 6 | //C++ definitions 7 | #ifndef false 8 | #define false 0 9 | #endif 10 | #ifndef true 11 | #define true (!false) 12 | #endif 13 | #ifndef null 14 | #define null ((void*)0) 15 | #endif 16 | #ifndef bool 17 | #define bool int 18 | #endif 19 | 20 | namespace System{ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /sgos2/include/c++/thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H_ 2 | #define _THREAD_H_ 3 | 4 | enum ThreadState{ 5 | 6 | }; 7 | 8 | class Thread{ 9 | private: 10 | //线程句柄 11 | uint handle; 12 | //结束码 13 | uint exitCode; 14 | //执行函数 15 | void* procedure; 16 | public: 17 | Thread(); 18 | Thread(void* proc); 19 | ~Thread(); 20 | /* 静态函数 */ 21 | //返回当前线程 22 | static Thread ThisThread(); 23 | //当前线程等待 24 | static void Sleep(uint ms); 25 | //结束当前线程 26 | static void Exit(uint code); 27 | /* 非静态函数 */ 28 | // 终止线程 29 | void abort(); 30 | // 开始线程 31 | void resume(); 32 | void start(); 33 | // 挂起线程 34 | void suspend(); 35 | // 等待线程结束 36 | void join(); 37 | // 获取句柄 38 | uint getHandle(); 39 | 40 | //命名系统 41 | int createName( const char* name ); 42 | void deleteName( const char* name ); 43 | static Thread find( const char* name ); 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /sgos2/include/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef _CTYPE_H 2 | #define _CTYPE_H 3 | 4 | #include 5 | 6 | #define _U 0x01 /* upper */ 7 | #define _L 0x02 /* lower */ 8 | #define _D 0x04 /* digit */ 9 | #define _C 0x08 /* cntrl */ 10 | #define _P 0x10 /* punct */ 11 | #define _S 0x20 /* white space (space/lf/tab) */ 12 | #define _X 0x40 /* hex digit */ 13 | #define _SP 0x80 /* hard space (0x20) */ 14 | 15 | EXTERN unsigned char _ctype[]; 16 | EXTERN char _ctmp; 17 | 18 | #define isalnum(c) ((_ctype+1)[c]&(_U|_L|_D)) 19 | #define isalpha(c) ((_ctype+1)[c]&(_U|_L)) 20 | #define iscntrl(c) ((_ctype+1)[c]&(_C)) 21 | #define isdigit(c) ((_ctype+1)[c]&(_D)) 22 | #define isgraph(c) ((_ctype+1)[c]&(_P|_U|_L|_D)) 23 | #define islower(c) ((_ctype+1)[c]&(_L)) 24 | #define isprint(c) ((_ctype+1)[c]&(_P|_U|_L|_D|_SP)) 25 | #define ispunct(c) ((_ctype+1)[c]&(_P)) 26 | #define isspace(c) ((_ctype+1)[c]&(_S)) 27 | #define isupper(c) ((_ctype+1)[c]&(_U)) 28 | #define isxdigit(c) ((_ctype+1)[c]&(_D|_X)) 29 | 30 | #define isascii(c) (((unsigned) c)<=0x7f) 31 | #define toascii(c) (((unsigned) c)&0x7f) 32 | 33 | #define tolower(c) (_ctmp=c,isupper(_ctmp)?_ctmp-('A'-'a'):_ctmp) 34 | #define toupper(c) (_ctmp=c,islower(_ctmp)?_ctmp-('a'-'A'):_ctmp) 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /sgos2/include/pthread.h: -------------------------------------------------------------------------------- 1 | #ifndef _PTHREAD_H_ 2 | #define _PTHREAD_H_ 3 | 4 | #include 5 | 6 | struct timespec { 7 | long tv_sec; 8 | long tv_nsec; 9 | }; 10 | 11 | typedef uint pthread_t; 12 | typedef uint pthread_attr_t; 13 | typedef uint pthread_mutexattr_t; 14 | 15 | typedef struct THREAD_LIST{ 16 | pthread_t thread; 17 | struct THREAD_LIST* next; 18 | }THREAD_LIST; 19 | 20 | typedef struct _mutex{ 21 | struct THREAD_LIST* list; 22 | uint value; 23 | }pthread_mutex_t; 24 | 25 | 26 | /* 27 | * PThread Functions 28 | */ 29 | int pthread_create (pthread_t * thread, const pthread_attr_t * attr, 30 | void *(*start) (void *), void *arg); 31 | int pthread_detach (pthread_t thread); 32 | int pthread_equal (pthread_t t1, pthread_t t2); 33 | void pthread_exit (void *value_ptr); 34 | int pthread_join (pthread_t thread, void **value_ptr); 35 | int pthread_kill(pthread_t thread, int sig); 36 | int pthread_cancel (pthread_t thread); 37 | /* 38 | * Mutex Functions 39 | */ 40 | int pthread_mutex_init (pthread_mutex_t * mutex, 41 | const pthread_mutexattr_t * attr); 42 | int pthread_mutex_destroy (pthread_mutex_t * mutex); 43 | int pthread_mutex_lock (pthread_mutex_t * mutex); 44 | int pthread_mutex_timedlock(pthread_mutex_t *mutex, 45 | const struct timespec *abstime); 46 | int pthread_mutex_trylock (pthread_mutex_t * mutex); 47 | int pthread_mutex_unlock (pthread_mutex_t * mutex); 48 | #endif 49 | -------------------------------------------------------------------------------- /sgos2/include/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef _QUEUE_H 2 | #define _QUEUE_H 3 | 4 | 5 | #define QUEUE_NAME_LEN 16 6 | 7 | typedef int (*queue_search_func)(const void *, const void *); 8 | typedef void (*queue_delete_func)(const void *); 9 | 10 | typedef struct QUEUE_NODE{ 11 | struct QUEUE_NODE *pre, *next; 12 | void* v; 13 | }QUEUE_NODE, qnode_t; 14 | 15 | typedef struct _QUEUE{ 16 | void* semaphore; //这里用spin_lock好不? 17 | int cur_num; //当前链表元素个数 18 | int max_num; //最大允许元素个数 19 | QUEUE_NODE *front; //头节点 20 | QUEUE_NODE *back; //尾节点 21 | char name[QUEUE_NAME_LEN]; //used for debugging. 22 | queue_delete_func del_func; 23 | char use_sem; 24 | }queue_t; 25 | 26 | #define QUEUE_UNINTERRUPTABLE 1 27 | 28 | EXTERN int queue_create( queue_t* l, int size, queue_delete_func del, const char* name, int use_sem ); 29 | EXTERN void* queue_pop_back( queue_t* l ); 30 | EXTERN void* queue_pop_front( queue_t* l ); 31 | EXTERN int queue_push_front( queue_t* l, void* data ); 32 | EXTERN int queue_push_back( queue_t* l, void* data ); 33 | EXTERN void* queue_search( queue_t* l, void*, queue_search_func search, qnode_t** ret_nod ); 34 | EXTERN void* queue_quick_search( queue_t* l, void*, qnode_t** ret_nod ); 35 | EXTERN void queue_cleanup( queue_t* l ); 36 | EXTERN int queue_is_empty( queue_t* l ); 37 | EXTERN void queue_remove( queue_t* l, QUEUE_NODE* data ); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /sgos2/include/sema.h: -------------------------------------------------------------------------------- 1 | #ifndef _SEMA_H_ 2 | #define _SEMA_H_ 3 | 4 | #include 5 | #include 6 | 7 | //sema.c 8 | #define down(a) sem_down(a) 9 | #define up(a) sem_up(a) 10 | 11 | typedef struct SEMAPHORE{ 12 | struct THREAD_LIST* list; 13 | uint value; 14 | }sem_t; 15 | 16 | int sem_trywait( sem_t *sem ); 17 | void sem_wait( sem_t *sem ); 18 | void sem_init( sem_t *sem, int pshared, uint value ); 19 | void sem_post( sem_t *sem ); 20 | void sem_destroy( sem_t *sem ); 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /sgos2/include/sgos_version.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #define OS_MAJOR_VERSION 2 4 | #define OS_MINOR_VERSION 0 5 | #define OS_PATCH_VERSION 0 6 | 7 | -------------------------------------------------------------------------------- /sgos2/include/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef _STDARG_H 2 | #define _STDARG_H 3 | 4 | typedef char *va_list; 5 | 6 | /* Amount of space required in an argument list for an arg of type TYPE. 7 | TYPE may alternatively be an expression whose type is used. */ 8 | 9 | #define __va_rounded_size(TYPE) \ 10 | (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int)) 11 | 12 | #ifndef __sparc__ 13 | #define va_start(AP, LASTARG) \ 14 | (AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG))) 15 | #else 16 | #define va_start(AP, LASTARG) \ 17 | (__builtin_saveregs (), \ 18 | AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG))) 19 | #endif 20 | 21 | void va_end (va_list); /* Defined in gnulib */ 22 | #define va_end(AP) 23 | 24 | #define va_arg(AP, TYPE) \ 25 | (AP += __va_rounded_size (TYPE), \ 26 | *((TYPE *) (AP - __va_rounded_size (TYPE)))) 27 | 28 | #endif /* _STDARG_H */ 29 | -------------------------------------------------------------------------------- /sgos2/include/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef _STDLIB_H_ 2 | #define _STDLIB_H_ 3 | 4 | #include 5 | #include 6 | 7 | 8 | EXTERN void* malloc(size_t); 9 | EXTERN void free(void*); 10 | EXTERN void* calloc(size_t, size_t); 11 | EXTERN void* realloc(void*, size_t); 12 | EXTERN int rand(void); 13 | EXTERN void srand(int seed); 14 | 15 | EXTERN int sprintf(char * buf, const char *fmt, ...); 16 | EXTERN int vsprintf(char *buf, const char *fmt, va_list args); 17 | 18 | EXTERN void sleep(unsigned int ms); 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /sgos2/include/string.h: -------------------------------------------------------------------------------- 1 | #ifndef _STRING_H_ 2 | #define _STRING_H_ 3 | 4 | #ifndef NULL 5 | #define NULL ((void *) 0) 6 | #endif 7 | 8 | 9 | 10 | 11 | #ifdef __cplusplus 12 | extern "C" char * strerror(int errno); 13 | extern "C" char * ___strtok; 14 | extern "C" { 15 | #else 16 | extern char * strerror(int errno); 17 | extern char * ___strtok; 18 | #endif 19 | 20 | char * strcpy(char * dest,const char *src); 21 | char * strncpy(char * dest,const char *src,int count); 22 | char * strcat(char * dest,const char * src); 23 | char * strncat(char * dest,const char * src,int count); 24 | int strcmp(const char * cs,const char * ct); 25 | int strncmp(const char * cs,const char * ct,int count); 26 | char * strchr(const char * s,int c); 27 | char * strrchr(const char * s,int c); 28 | int strspn(const char * cs, const char * ct); 29 | int strcspn(const char * cs, const char * ct); 30 | char * strpbrk(const char * cs,const char * ct); 31 | char * strstr(const char * cs,const char * ct); 32 | char * strlwr(char * str); 33 | int strlen(const char * s); 34 | 35 | char * strtok(char * s,const char * ct); 36 | void * memcpy(void * dest,const void * src, int n); 37 | void * memcpyw(void * dest,const void * src, int n); 38 | void * memcpyd(void * dest,const void * src, int n); 39 | char * memmove(char * dest,const char * src, int n); 40 | int memcmp(const void * cs,const void * ct,int count); 41 | void * memchr(const void * cs,char c,int count); 42 | void * memset(void * s,int c,int count); 43 | void * memsetw(void * s,short n,int count); 44 | void * memsetd(void * s,int n,int count); 45 | 46 | int strnicmp(char * s1, char * s2, int len); 47 | int stricmp(char * s1, char * s2); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /sgos2/include/time.h: -------------------------------------------------------------------------------- 1 | // time structures 2 | #ifndef _TIME_H__ 3 | #define _TIME_H__ 4 | 5 | #include 6 | 7 | struct tms { 8 | clock_t tms_utime; /* user time */ 9 | clock_t tms_stime; /* system time */ 10 | clock_t tms_cutime; /* user time of children */ 11 | clock_t tms_cstime; /* system time of children */ 12 | }; 13 | 14 | struct timeval{ 15 | long tv_sec; /*秒*/ 16 | long tv_usec; /*微秒*/ 17 | }; 18 | 19 | struct timezone{ 20 | int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/ 21 | int tz_dsttime; /*日光节约时间的状态*/ 22 | }; 23 | 24 | //! the old time struct 25 | #ifndef _TM_DEFINED 26 | typedef struct tm 27 | { 28 | int tm_sec; // 29 | int tm_min; // 30 | int tm_hour; // 31 | int tm_mday; //day of the month 32 | int tm_mon; //month of the year 33 | int tm_year; // 34 | int tm_wday; //day of the week? 0~6 35 | int tm_yday; //day of the year 36 | int tm_isdst; //is leap year? 37 | }TIME; 38 | #define _TM_DEFINED 39 | #endif //_TM_DEFINED 40 | 41 | //! ... 42 | EXTERN time_t time(time_t* ); 43 | //! Get a format string by a time, 44 | //! The function returns the length of the string, 0 means failed, others > 0 means success 45 | //! Note: the str buffer must be enough big, normally 48 bytes 46 | EXTERN int strtime( time_t*, char* str ); 47 | //! From struct tm to time_t 48 | EXTERN time_t mktime( const struct tm* ); 49 | //! From time_t to struct tm 50 | //! If failed, it will return 0, others > 0 means success 51 | EXTERN int gettime( const time_t*, struct tm* ); 52 | 53 | #endif 54 | 55 | -------------------------------------------------------------------------------- /sgos2/include/types.h: -------------------------------------------------------------------------------- 1 | #ifndef _TYPES_H_ 2 | #define _TYPES_H_ 3 | 4 | //common types 5 | typedef unsigned char uchar; 6 | typedef unsigned short ushort; 7 | typedef unsigned int uint; 8 | typedef unsigned long ulong; 9 | typedef unsigned char byte; 10 | 11 | typedef unsigned int size_t; 12 | typedef unsigned int time_t; 13 | typedef unsigned int clock_t; 14 | 15 | typedef unsigned char t_8; 16 | typedef unsigned short t_16; 17 | typedef unsigned t_32; 18 | typedef unsigned long long t_64 19 | ; 20 | typedef signed char int8_t; 21 | typedef unsigned char uint8_t; 22 | typedef short int16_t; 23 | typedef unsigned short uint16_t; 24 | typedef int int32_t; 25 | typedef unsigned uint32_t; 26 | typedef long long int64_t; 27 | typedef unsigned long long uint64_t; 28 | 29 | #ifndef NULL 30 | #define NULL ((void *) 0) 31 | #endif 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /sgos2/kernel/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for SGOS2:kernel 2 | 3 | # Target machine information 4 | ARCH = i386 5 | 6 | ASFLAGS = -g 7 | CCFLAGS = -g -Werror -Wall -Wno-char-subscripts -I../include -Iinclude -fno-builtin -ffreestanding -fleading-underscore 8 | LDFLAGS = 9 | OCFLAGS = -Obinary 10 | ODFLAGS = -S 11 | 12 | ifeq ($(OSTYPE), Linux) 13 | # linux env 14 | CCFLAGS += -fno-stack-protector 15 | LDFLAGS += -T$(LDSCRIPT) 16 | else 17 | # windows env 18 | LDFLAGS += -Ttext=0xC0100000 -e_start 19 | endif 20 | 21 | AS = as $(ASFLAGS) 22 | CC = gcc $(CCFLAGS) 23 | LD = ld $(LDFLAGS) 24 | OC = objcopy $(OCFLAGS) 25 | OD = objdump $(ODFLAGS) 26 | LD2 = ld2 27 | NM = nm 28 | WF = ../tools/wf/wf 29 | 30 | IMAGE = ../sgos2.img 31 | 32 | # Kernel dir 33 | DIR_ARCH = arch 34 | DIR_IPC = ipc 35 | DIR_MM = mm 36 | DIR_KD = kd 37 | DIR_START = start 38 | DIR_LIB = lib 39 | DIR_TM = tm 40 | 41 | # All objects, here can be set the first link obj for your kernel 42 | OBJECTS := 43 | 44 | # Platform objects 45 | include arch/$(ARCH)/Makefile 46 | 47 | # Kernel objects 48 | OBJECTS += $(patsubst %.c,%.o, $(wildcard $(DIR_ARCH)/*.c)) \ 49 | $(patsubst %.c,%.o, $(wildcard $(DIR_IPC)/*.c)) \ 50 | $(patsubst %.c,%.o, $(wildcard $(DIR_MM)/*.c)) \ 51 | $(patsubst %.c,%.o, $(wildcard $(DIR_KD)/*.c)) \ 52 | $(patsubst %.c,%.o, $(wildcard $(DIR_START)/*.c)) \ 53 | $(patsubst %.c,%.o, $(wildcard $(DIR_LIB)/*.c)) \ 54 | $(patsubst %.c,%.o, $(wildcard $(DIR_TM)/*.c)) 55 | 56 | # Target 57 | all: kernel 58 | 59 | kernel: $(OBJECTS) 60 | $(LD) $(OBJECTS) -o$@ 61 | $(NM) $@ > $@.sym 62 | $(OD) $@ > $@.dmp 63 | $(OC) $@ $@.bin 64 | $(WF) $(IMAGE) -src $@.bin -dest sgos/$@.bin 65 | 66 | clean: 67 | $(RM) $(OBJECTS) \ 68 | kernel.dmp \ 69 | kernel \ 70 | kernel.sym \ 71 | kernel.bin 72 | 73 | # Standard Procedures 74 | .s.o: 75 | $(AS) -o $@ $< 76 | 77 | .c.o: 78 | $(CC) -c -o $@ $< 79 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/Makefile: -------------------------------------------------------------------------------- 1 | # I386 architecture 2 | 3 | CCFLAGS += -Iinclude/arch_i386 4 | ASFLAGS += -Iinclude/arch_i386 5 | 6 | 7 | LDSCRIPT = arch/i386/kernel.ld 8 | 9 | # Kernel Objects 10 | OBJECTS += arch/i386/init/multiboot.o \ 11 | arch/i386/init/init.o \ 12 | arch/i386/io/port.o \ 13 | arch/i386/io/terminal.o \ 14 | arch/i386/mmu/gdt.o \ 15 | arch/i386/mmu/page.o \ 16 | arch/i386/mmu/map.o \ 17 | arch/i386/mmu/fault.o \ 18 | arch/i386/clock/rtc.o \ 19 | arch/i386/debug/dbgx86.o \ 20 | arch/i386/cpu/threading.o \ 21 | arch/i386/cpu/sysenter.o \ 22 | arch/i386/cpu/switch.o \ 23 | arch/i386/cpu/lock.o \ 24 | arch/i386/cpu/fastcall.o \ 25 | arch/i386/cpu/vm86.o \ 26 | arch/i386/math/fpu.o \ 27 | arch/i386/irq/irq.o \ 28 | arch/i386/irq/isr.o \ 29 | arch/i386/irq/interrupt.o \ 30 | arch/i386/misc/mmop.o 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/clock/rtc.c: -------------------------------------------------------------------------------- 1 | //real time clock 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define BCD_TO_HEX(bcd) (bcd&0xf)+((bcd>>4)&0xf)*10 10 | 11 | //global variables, they may be accessed by another file 12 | unsigned rtc_time; //unit: clock 13 | unsigned rtc_millisecond; //unit: ms 14 | unsigned rtc_second; //unit: s 15 | 16 | //设置时钟频率 17 | //Set PIT Interrupt Frequency 18 | void ArSetRealTimeClockFrequency(unsigned freq) 19 | { 20 | unsigned div = 1193180/freq; 21 | ArOutByte(0x43, 0x36); 22 | ArOutByte(0x40, div & 0xFF); 23 | ArOutByte(0x40, div>>8); 24 | } 25 | 26 | unsigned ArGetMilliSecond() 27 | { 28 | return rtc_millisecond; 29 | } 30 | 31 | //时钟中断 32 | static void rtc_interrupt(const I386_REGISTERS* r) 33 | { 34 | rtc_time ++; 35 | rtc_millisecond += 1000/RTC_FREQUENCY ; 36 | //屏蔽时钟中断 37 | ArSetIrqMask( RTC_INTERRUPT, 0 ); 38 | //开启中断 39 | ArLocalEnableIrq(); 40 | //更新时间 41 | if( (rtc_time % RTC_FREQUENCY)==0 ) 42 | rtc_second ++; 43 | //调度时钟 44 | TmIncreaseTime(); 45 | //关中断 46 | ArLocalDisableIrq(); 47 | //允许时钟中断 48 | ArSetIrqMask( RTC_INTERRUPT, 1 ); 49 | } 50 | 51 | //实时钟初始化 52 | void ArStartRealTimeClock() 53 | { 54 | struct tm t; 55 | char timestr[48]; 56 | 57 | /* initialize RTC */ 58 | ArSetRealTimeClockFrequency( RTC_FREQUENCY ); 59 | ArInstallIrq( RTC_INTERRUPT, rtc_interrupt); 60 | ArSetIrqMask( RTC_INTERRUPT, 1 ); 61 | 62 | /* initialize system time */ 63 | ArOutByte(0x70, 0); /* seconds */ 64 | t.tm_sec = BCD_TO_HEX(ArInByte(0x71)); 65 | ArOutByte(0x70, 2); /* minutes */ 66 | t.tm_min = BCD_TO_HEX(ArInByte(0x71)); 67 | ArOutByte(0x70, 4); /* hours */ 68 | t.tm_hour = BCD_TO_HEX(ArInByte(0x71)); 69 | ArOutByte(0x70, 7); /* date of month */ 70 | t.tm_mday = BCD_TO_HEX(ArInByte(0x71)); 71 | ArOutByte(0x70, 8); /* month */ 72 | t.tm_mon = BCD_TO_HEX(ArInByte(0x71)); 73 | ArOutByte(0x70, 9); /* year */ 74 | t.tm_year = BCD_TO_HEX(ArInByte(0x71)); 75 | t.tm_mon --; 76 | /* calculate to time calculated in ms from 1970-01-01 */ 77 | rtc_second = mktime( &t ); 78 | rtc_time = 0; 79 | rtc_millisecond = rtc_second * 1000; 80 | // Show time 81 | strtime( &rtc_second, timestr ); 82 | KdPrintf("System start time: %s\n", timestr ); 83 | } 84 | 85 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/cpu/fastcall.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //使用sysenter需要gdt按照一定的要求设计。 6 | //sgos2的gdt符合这个要求。 7 | //初始化fastcall要使用到的寄存器 8 | extern void ArAsmSysenterHandler(); 9 | void ArInitializeFastcall() 10 | { 11 | wrmsr(MSR_IA32_SYSENTER_CS, GD_KERNEL_CODE, 0); 12 | wrmsr(MSR_IA32_SYSENTER_EIP, (size_t)&ArAsmSysenterHandler, 0); 13 | 14 | } 15 | 16 | //更新fastcall要使用的线程内核堆栈 17 | void ArUpdateFastcallEsp(size_t kesp) 18 | { 19 | wrmsr(MSR_IA32_SYSENTER_ESP, kesp, 0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/cpu/lock.c: -------------------------------------------------------------------------------- 1 | //lock 2 | 3 | #include 4 | #include 5 | 6 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/cpu/switch.S: -------------------------------------------------------------------------------- 1 | .global _ArAsmEnterThreadingMode 2 | ;// 主要是变更内核堆栈 3 | _ArAsmEnterThreadingMode: 4 | pushl %ebp 5 | movl %esp, %ebp 6 | movl 0x08(%ebp), %esp 7 | pushl 0x04(%ebp) ;//returen address 8 | pushfl ;//flag寄存器 9 | pushl $0x08 ;//代码段 10 | pushl $return 11 | movw $0x10, %ax 12 | movw %ax, %es 13 | movw %ax, %ds 14 | iret 15 | return: 16 | ret 17 | 18 | .global _ArAsmSwapContext 19 | _ArAsmSwapContext: ;// 线程切换 20 | pushl %ebp 21 | movl %esp, %ebp 22 | pushfl 23 | push %cs 24 | push $Return 25 | subl $8, %esp 26 | pushal 27 | pushl %ds 28 | push %es 29 | push %fs 30 | push %gs 31 | movl 0x0C(%ebp), %edi 32 | movl %esp, (%edi) ;// cur->stack_pointer = esp 33 | movl 0x10(%ebp), %esp 34 | movl (%esp), %esp ;// esp = p->stack_pointer 35 | pop %gs 36 | pop %fs 37 | pop %es 38 | pop %ds 39 | popal 40 | addl $8, %esp 41 | iret 42 | 43 | Return: 44 | popl %ebp 45 | ret 46 | 47 | 48 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/cpu/sysenter.S: -------------------------------------------------------------------------------- 1 | ;// perform a faster system call 2 | ;// 090818 implemeted. 3 | 4 | .global _ArAsmSysenterHandler 5 | ret_val: 6 | .long 0 7 | _ArAsmSysenterHandler: 8 | pushal 9 | pushl %ds 10 | pushl %es 11 | pushl %eax 12 | movw $0x10, %ax 13 | movw %ax, %ds 14 | movw %ax, %es 15 | popl %eax 16 | pushl %edx 17 | pushl %ecx 18 | pushl %ebx 19 | sti 20 | call *_SystemCallTable(,%eax,4) 21 | addl $12, %esp 22 | movl %eax, ret_val 23 | popl %es 24 | popl %ds 25 | popal 26 | movl ret_val, %eax 27 | movl %ebp, %ecx 28 | movl %esi, %edx 29 | sysexit 30 | 31 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/debug/dbgx86.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | //调试器中断句柄 7 | static int dbg_handler( int err_code, I386_REGISTERS* r ) 8 | { 9 | //dbg can not work now 10 | //continue the program. 11 | PERROR("##debug at 0x%X is not implemented.", r->eip ); 12 | return 1; 13 | } 14 | 15 | //初始化调试器 16 | void ArInitializeDebugger() 17 | { 18 | ArInstallIsr( DEBUG_INTERRUPT, (void*)dbg_handler ); 19 | } 20 | 21 | //打印寄存器的内容 22 | void ArDumpRegisters( const I386_REGISTERS *r ) 23 | { 24 | KdPrintf("pid:%d dump cpu:\ncs: 0x%X\teip: 0x%X\nss: 0x%X\tesp: 0x%X\tkesp: 0x%X\n" 25 | "ds: 0x%X\tesi: 0x%X\nes: 0x%X\tedi: 0x%X\nfs: 0x%X\ngs: 0x%X\neax: 0x%X\tecx: 0x%X\tebx: 0x%X\t" 26 | "edx: 0x%X\neflags: 0x%X\n", 0,r->cs, r->eip, r->ss, r->esp, r->kesp, 27 | r->ds, r->esi, r->es, r->edi, r->fs, r->gs, r->eax, r->ecx, 28 | r->ebx, r->edx, r->eflags); 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/init/init.c: -------------------------------------------------------------------------------- 1 | //Xiaoxia 2 | //090803 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //multiboot.S结束时调用这个函数 10 | void ArSaveMultibootInformation( size_t info_addr ) 11 | { 12 | // 13 | ArLocalDisableIrq(); 14 | //call KeStartOs, never return 15 | KeStartOs( info_addr ); 16 | } 17 | 18 | //进一步设置x86保护模式 19 | #define SET_SYSTEM_GATE( vector, handle ) ArSetGate( vector, DA_386IGate | DA_DPL3, handle ) 20 | extern int* SystemCallService; 21 | int ArInitializeSystem() 22 | { 23 | //init i386 24 | //重新设置gdt 25 | ArInitializeGdt(); 26 | //设置isr,捕获各种机器异常 27 | ArInitializeIsr(); 28 | //设置irq,捕获硬、软件中断 29 | ArInitializeIrq(); 30 | //调试器 31 | ArInitializeDebugger(); 32 | //i387数学协处理器 33 | ArInitializeMathProcessor(); 34 | //vm86 35 | ArInitializeVm86(); 36 | //设置软中断 37 | SET_SYSTEM_GATE( SYSTEM_INTERRUPT, &SystemCallService ); 38 | //初始化fastcall 39 | ArInitializeFastcall(); 40 | //Real time clock 41 | ArStartRealTimeClock(); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/io/port.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | t_16 ArInWord( t_16 port ) 5 | { 6 | register t_16 ret; 7 | __asm__ __volatile__ ( "in %%dx , %%ax" : "=a"( ret ) : "d"( port ) ) ; 8 | return ret; 9 | } 10 | 11 | t_32 ArInWord32( t_16 port ) 12 | { 13 | register t_16 ret; 14 | __asm__ __volatile__ ( "in %%dx , %%eax" : "=a"( ret ) : "d"( port ) ) ; 15 | return ret; 16 | } 17 | 18 | t_8 ArInByte( t_16 port ) 19 | { 20 | register t_8 ret; 21 | __asm__ __volatile__ ( "in %%dx , %%al" : "=a"( ret ) : "d"( port ) ) ; 22 | return ret; 23 | } 24 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Link script for SGOS2 */ 2 | 3 | ENTRY(_start) 4 | 5 | K_VIR_ADDR = 0xC0100000; 6 | K_PHY_ADDR = 0x100000; 7 | PAGE_SIZE = 0x1000; 8 | /* 8KB stack size */ 9 | K_STACK_SIZE = 0x1000; 10 | 11 | SECTIONS 12 | { 13 | .text K_VIR_ADDR : AT(K_PHY_ADDR) 14 | { 15 | INIT_BEG = . ; 16 | *(.init) 17 | . = ALIGN(PAGE_SIZE); 18 | INIT_END = . ; 19 | *(.text) 20 | *(.rodata.*) 21 | . = ALIGN(PAGE_SIZE); 22 | } 23 | .data : AT(K_PHY_ADDR + (DATA_BEG - INIT_BEG) ) 24 | { 25 | DATA_BEG = . ; 26 | *(.data) 27 | *(.rodata) 28 | . = ALIGN(PAGE_SIZE); 29 | } 30 | .bss : AT(K_PHY_ADDR + (BSS_BEG - INIT_BEG)) 31 | { 32 | BSS_BEG = . ; 33 | *(.bss) 34 | . = ALIGN(PAGE_SIZE); 35 | } 36 | .stack : AT(K_PHY_ADDR +(_stack_end - INIT_BEG)) 37 | { 38 | _stack_end = . ; 39 | . += K_STACK_SIZE; 40 | _stack_start = . ; 41 | K_STACK_ADDR = . ; 42 | . = ALIGN( PAGE_SIZE ); 43 | } 44 | KERNEL_END = . ; 45 | . = ALIGN(PAGE_SIZE); 46 | } 47 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/math/fpu.c: -------------------------------------------------------------------------------- 1 | // Hope it works!! 2 | // 090828 by Xiaoxia 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //为线程分配fpu 10 | void ArPrepareFpuForThread( KThread* thr ) 11 | { 12 | ArchThread* arch; 13 | arch = &thr->ArchitectureInformation; 14 | if( !arch->fsave ){ 15 | arch->fsave = (I387_REGISTERS*)MmAllocateKernelMemory( sizeof(I387_REGISTERS) ); 16 | RtlZeroMemory32( arch->fsave, sizeof(I387_REGISTERS)>>2 ); 17 | } 18 | } 19 | 20 | //保存fpu寄存器 21 | inline void fpu_save( KThread* thr ) 22 | { 23 | __asm__("fnsave %0; fwait"::"m"(*thr->ArchitectureInformation.fsave)); 24 | } 25 | 26 | //恢复fpu寄存器 27 | inline void fpu_restore( KThread* thr ) 28 | { 29 | __asm__("frstor %0"::"m"(*thr->ArchitectureInformation.fsave)); 30 | } 31 | 32 | //在线程切换时,检查fpu是否有必要保存 33 | void ArCheckAndSaveFpu( KThread* thr ) 34 | { 35 | if(thr->ArchitectureInformation.used_fpu){ 36 | fpu_save(thr); 37 | thr->ArchitectureInformation.used_fpu = 0; 38 | //捕捉浮点处理异常 39 | stts(); 40 | } 41 | } 42 | 43 | // 注意,在处理过程中可能会发生硬件中断。 44 | static int fpu_handler(int err_code, I386_REGISTERS* r) 45 | { 46 | KThread* thr; 47 | uint flags; 48 | thr = TmGetCurrentThread(); 49 | //是否数学协处理器已经有内容 50 | if(thr->UsedMathProcessor){ 51 | //禁止切换线程 52 | ArLocalSaveIrq(flags); 53 | //清ts,返回后可以使用fpu 54 | clts(); 55 | //恢复 56 | fpu_restore(thr); 57 | thr->ArchitectureInformation.used_fpu = 1; 58 | ArLocalRestoreIrq(flags); 59 | }else{ 60 | ArPrepareFpuForThread(thr); 61 | //禁止切换线程 62 | ArLocalSaveIrq(flags); 63 | //then it would save fpu when TmSchedule 64 | thr->ArchitectureInformation.used_fpu = 1; 65 | //标记已初始化数学协处理器的内容 66 | thr->UsedMathProcessor = 1; 67 | clts(); 68 | //init fpu and return 69 | __asm__ __volatile__("fninit"); 70 | ArLocalRestoreIrq(flags); 71 | } 72 | return 1; 73 | } 74 | 75 | //初始化fpu 76 | void ArInitializeMathProcessor() 77 | { 78 | //安装浮点异常处理函数 79 | ArInstallIsr( FPU_INTERRUPT, (void*)fpu_handler ); 80 | //设置ts位,使用浮点时触发异常。 81 | stts(); 82 | } 83 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/misc/mmop.c: -------------------------------------------------------------------------------- 1 | // mmop.c 2 | // bits 3 | 4 | #include 5 | 6 | void * memcpy8(void * dest,const void * src, int n) 7 | { 8 | __asm__("cld\n\t" 9 | "rep\n\t" 10 | "movsb" 11 | ::"c" (n),"S" (src),"D" (dest)); 12 | return dest; 13 | } 14 | 15 | void * memcpy16(void * dest,const void * src, int n) 16 | { 17 | __asm__("cld\n\t" 18 | "rep\n\t" 19 | "movsw" 20 | ::"c" (n),"S" (src),"D" (dest)); 21 | return dest; 22 | } 23 | 24 | void * memcpy32(void * dest,const void * src, int n) 25 | { 26 | __asm__("cld\n\t" 27 | "rep\n\t" 28 | "movsl" 29 | ::"c" (n),"S" (src),"D" (dest)); 30 | return dest; 31 | } 32 | 33 | 34 | void * memset8(void * s,unsigned char c,int count) 35 | { 36 | __asm__("cld\n\t" 37 | "rep\n\t" 38 | "stosb" 39 | ::"a" (c),"D" (s),"c" (count)); 40 | return s; 41 | } 42 | 43 | void * memset16(void * s,unsigned short n,int count) 44 | { 45 | __asm__("cld\n\t" 46 | "rep\n\t" 47 | "stosw" 48 | ::"a" (n),"D" (s),"c" (count)); 49 | return s; 50 | } 51 | 52 | void * memset32(void * s,unsigned int n,int count) 53 | { 54 | __asm__("cld\n\t" 55 | "rep\n\t" 56 | "stosl" 57 | ::"a" (n),"D" (s),"c" (count)); 58 | return s; 59 | } 60 | -------------------------------------------------------------------------------- /sgos2/kernel/arch/i386/mmu/fault.c: -------------------------------------------------------------------------------- 1 | //pagefault 2 | //sgos2 3 | //Xiaoxia 090806 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | //if in MmAllocateKernelMemory, we can not allow irqs 12 | extern int in_allocator; 13 | //pagefault 14 | //页面缺页、权限等异常处理 15 | int pagefault_handler( int err_code, I386_REGISTERS* r ) 16 | { 17 | uint addr; 18 | KSpace* space; 19 | //被访问或修改的内存地址 20 | __asm__ __volatile__("movl %%cr2, %0" : "=r"( addr ) ); 21 | space = MmGetCurrentSpace(); 22 | 23 | /* 24 | If TmGetCurrentThread()==NULL, it means the kernel is sitll 25 | initializing. If in MmAllocateKernelMemory, we should disable IRQ. 26 | */ 27 | if( TmGetCurrentThread() && !in_allocator ) 28 | ArLocalEnableIrq(); 29 | 30 | if( !(err_code&PAGE_ATTR_PRESENT ) ) 31 | return MmHandleNonPresentPageFault( space, addr ); 32 | else if(err_code&PAGE_ATTR_WRITE) 33 | return MmHandleReadOnlyPageFault( space, addr ); 34 | else if(err_code&PAGE_ATTR_USER) 35 | PERROR("##user try to access kernel memory at 0x%X.", addr); 36 | //未能处理,则BSOD 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /sgos2/kernel/include/allocator.h: -------------------------------------------------------------------------------- 1 | #ifndef _ALLOC_H_ 2 | #define _ALLOC_H_ 3 | 4 | //类型定义 5 | //typedef unsigned long size_t; 6 | #include 7 | 8 | #define FREE_NODE 0x00000001 9 | 10 | #define MAX_HASH_ENTRY 15 11 | 12 | typedef struct node{ 13 | //邻接链表 14 | struct node* prev; 15 | struct node* next; 16 | //Hash table 17 | struct node* hash_prev; 18 | struct node* hash_next; 19 | //分配描述 20 | size_t attribute; 21 | size_t size; //不包括分配描述符大小 22 | }node_t; 23 | 24 | typedef struct allocator{ 25 | struct node* first_node; //used for debugging. 26 | struct node* free_table[MAX_HASH_ENTRY]; 27 | }allocator_t; 28 | 29 | #define IS_FREE_NODE(nod) (nod->attribute&FREE_NODE ) 30 | #define MAKE_FREE(nod) (nod->attribute |= FREE_NODE) 31 | #define MAKE_OCCUPIED(nod) (nod->attribute &= ~FREE_NODE ) 32 | 33 | //导出函数 34 | void* mm_alloc(allocator_t*, size_t); 35 | void* mm_alloc_ex(allocator_t* who, size_t addr, size_t siz ); 36 | size_t mm_free(allocator_t*, void*); 37 | void* mm_calloc(allocator_t*, size_t, size_t); 38 | void* mm_realloc(allocator_t*, void*, size_t); 39 | void mm_init_block(allocator_t* who, size_t addr, size_t size); 40 | void mm_print_block(allocator_t* who); 41 | void mm_free_all(allocator_t * who); 42 | int mm_check_allocated(allocator_t* who, size_t addr ); 43 | 44 | #endif //_ALLOC_H_ 45 | -------------------------------------------------------------------------------- /sgos2/kernel/include/apidef.h: -------------------------------------------------------------------------------- 1 | #ifndef _APIDEF_H_ 2 | #define _APIDEF_H_ 3 | 4 | #include 5 | #include 6 | 7 | //system call for SGOS2 8 | //调试 9 | SYSCALL0( 0, uint, Test ); 10 | SYSCALL2( 1, int, Print, const char*, buf, int, nbytes ); 11 | //消息处理 12 | SYSCALL2( 2, int, Send, Message*, msg, time_t, timeout ); 13 | SYSCALL2( 3, int, Receive, Message*, msg, time_t, timeout ); 14 | SYSCALL3( 4, int, InvokeBiosService, int, int_no, void*, context, size_t, context_size ); 15 | 16 | #endif //_APIDEF_H_ 17 | -------------------------------------------------------------------------------- /sgos2/kernel/include/arch_i386/gdt_const.h: -------------------------------------------------------------------------------- 1 | #ifndef _GDT_CONST_H_ 2 | #define _GDT_CONST_H_ 3 | 4 | /* 描述符类型值说明 */ 5 | #define DA_32 0x4000 /* 32 位段 */ 6 | #define DA_LIMIT_4K 0x8000 /* 段界限粒度为 4K 字节 */ 7 | #define DA_DPL0 0x00 /* DPL = 0 */ 8 | #define DA_DPL1 0x20 /* DPL = 1 */ 9 | #define DA_DPL2 0x40 /* DPL = 2 */ 10 | #define DA_DPL3 0x60 /* DPL = 3 */ 11 | /* 存储段描述符类型值说明 */ 12 | #define DA_DR 0x90 /* 存在的只读数据段类型值 */ 13 | #define DA_DRW 0x92 /* 存在的可读写数据段属性值 */ 14 | #define DA_DRWA 0x93 /* 存在的已访问可读写数据段类型值 */ 15 | #define DA_C 0x98 /* 存在的只执行代码段属性值 */ 16 | #define DA_CR 0x9A /* 存在的可执行可读代码段属性值 */ 17 | #define DA_CCO 0x9C /* 存在的只执行一致代码段属性值 */ 18 | #define DA_CCOR 0x9E /* 存在的可执行可读一致代码段属性值 */ 19 | /* 系统段描述符类型值说明 */ 20 | #define DA_LDT 0x82 /* 局部描述符表段类型值 */ 21 | #define DA_TaskGate 0x85 /* 任务门类型值 */ 22 | #define DA_386TSS 0x89 /* 可用 386 任务状态段类型值 */ 23 | #define DA_386CGate 0x8C /* 386 调用门类型值 */ 24 | #define DA_386IGate 0x8E /* 386 中断门类型值 */ 25 | #define DA_386TGate 0x8F /* 386 陷阱门类型值 */ 26 | /* 选择子类型值说明 */ 27 | /* 其中, SA_ : Selector Attribute */ 28 | #define SA_RPL_MASK 0xFFFC 29 | #define SA_RPL0 0 30 | #define SA_RPL1 1 31 | #define SA_RPL2 2 32 | #define SA_RPL3 3 33 | 34 | #define SA_TI_MASK 0xFFFB 35 | #define SA_TIG 0 36 | #define SA_TIL 4 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /sgos2/kernel/include/arch_i386/io.h: -------------------------------------------------------------------------------- 1 | #ifndef _IO_H_ 2 | #define _IO_H_ 3 | 4 | // (端口, 数据) 5 | #define ArOutByteWait(port,data) \ 6 | __asm__ __volatile__ ( "out %%al , %%dx\n\tjmp 1f\n1:\tjmp 1f\n1:" : : "a"( data ) , "d"( port ) ); 7 | #define ArOutWordWait(port,data) \ 8 | __asm__ __volatile__ ( "out %%ax , %%dx\n\tjmp 1f\n1:\tjmp 1f\n1:" : : "a"( data ) , "d"( port ) ); 9 | #define ArOutByte(port,data) \ 10 | __asm__ __volatile__ ( "out %%al , %%dx" : : "a"( data ) , "d"( port ) ); 11 | #define ArOutWord(port,data) \ 12 | __asm__ __volatile__ ( "out %%ax , %%dx" : : "a"( data ) , "d"( port ) ); 13 | #define ArOutWord32(port,data) \ 14 | __asm__ __volatile__ ( "out %%eax , %%dx" : : "a"( data ) , "d"( port ) ); 15 | 16 | 17 | 18 | // port.c 19 | t_16 ArInWord( t_16 port ); 20 | t_32 ArInWord32( t_16 port ); 21 | t_8 ArInByte( t_16 port ); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /sgos2/kernel/include/arch_i386/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOCK_H_ 2 | #define _LOCK_H_ 3 | 4 | #define ArLocalDisableIrq() __asm__ __volatile__("cli") 5 | 6 | #define ArLocalEnableIrq() __asm__ __volatile__("sti") 7 | 8 | #define ArLocalSaveIrq( x ) __asm__ __volatile__ \ 9 | ("pushfl ; popl %0 ; cli":"=g" (x): :"memory") 10 | 11 | #define ArLocalRestoreIrq(x) __asm__ __volatile__ \ 12 | ("pushl %0 ; popfl" : :"g" (x):"memory") 13 | 14 | 15 | 16 | #endif 17 | 18 | -------------------------------------------------------------------------------- /sgos2/kernel/include/arch_i386/mmop.h: -------------------------------------------------------------------------------- 1 | // bits 2 | #ifndef _MMOP_H_ 3 | #define _MMOP_H_ 4 | 5 | void * memcpy8(void * dest, const void * src, int n); 6 | void * memcpy16(void * dest, const void * src, int n); 7 | void * memcpy32(void * dest, const void * src, int n); 8 | void * memset8(void * s, unsigned char c, int count); 9 | void * memset16(void * s, unsigned short n, int count); 10 | void * memset32(void * s, unsigned int n, int count); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /sgos2/kernel/include/arch_i386/msr.h: -------------------------------------------------------------------------------- 1 | #ifndef _MSR_H_ 2 | #define _MSR_H_ 3 | 4 | #define rdmsr(msr,val1,val2) \ 5 | __asm__ __volatile__("rdmsr" \ 6 | : "=a" (val1), "=d" (val2) : "c" (msr)) 7 | 8 | #define wrmsr(msr,val1,val2) \ 9 | __asm__ __volatile__("wrmsr" : : "c" (msr), "a" (val1), "d" (val2)) 10 | 11 | #define MSR_IA32_SYSENTER_CS 0x174 //CS Selector of the target segment 12 | #define MSR_IA32_SYSENTER_ESP 0x175 //Target ESP 13 | #define MSR_IA32_SYSENTER_EIP 0x176 //Target EIP 14 | 15 | #endif //_MSR_H_ 16 | -------------------------------------------------------------------------------- /sgos2/kernel/include/arch_i386/multiboot.h: -------------------------------------------------------------------------------- 1 | #ifndef _MULTIBOOT_H_ 2 | #define _MULTIBOOT_H_ 3 | 4 | #define MULTIBOOT_HEADER_MAGIC 0x1BADB002 5 | #define MULTIBOOT_HEADER_FLAGS 0x00010003 6 | #define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 7 | 8 | typedef struct multiboot_header 9 | { 10 | t_32 Magic; 11 | t_32 flags; 12 | t_32 checksum; 13 | t_32 header_addr; 14 | t_32 load_addr; 15 | t_32 load_end_addr; 16 | t_32 bss_end_addr; 17 | t_32 entry_addr; 18 | t_32 video_mode; 19 | t_32 width; 20 | t_32 height; 21 | t_32 depth; 22 | } multiboot_header_t; 23 | 24 | /* The symbol table for a.out. */ 25 | typedef struct aout_symbol_table 26 | { 27 | t_32 tabsize; 28 | t_32 strsize; 29 | t_32 addr; 30 | t_32 reserved; 31 | } aout_symbol_table_t; 32 | 33 | /* The section header table for ELF. */ 34 | typedef struct elf_section_header_table 35 | { 36 | t_32 num; 37 | t_32 size; 38 | t_32 addr; 39 | t_32 shndx; 40 | } elf_section_header_table_t; 41 | 42 | /* The Multiboot information. */ 43 | typedef struct multiboot_info 44 | { 45 | t_32 flags; 46 | t_32 mem_lower; 47 | t_32 mem_upper; 48 | t_32 boot_device; 49 | t_32 cmdline; 50 | t_32 mods_count; 51 | t_32 mods_addr; 52 | union 53 | { 54 | aout_symbol_table_t aout_sym; 55 | elf_section_header_table_t elf_sec; 56 | } u; 57 | t_32 mmap_length; 58 | t_32 mmap_addr; 59 | } multiboot_info_t; 60 | 61 | /* The module structure. */ 62 | typedef struct module 63 | { 64 | t_32 mod_start; 65 | t_32 mod_end; 66 | t_32 string; 67 | t_32 reserved; 68 | } module_t; 69 | 70 | /* The memory map. Be careful that the offset 0 is base_addr_low 71 | but no size. */ 72 | typedef struct memory_map 73 | { 74 | t_32 size; 75 | t_32 base_addr_low; 76 | t_32 base_addr_high; 77 | t_32 length_low; 78 | t_32 length_high; 79 | t_32 type; 80 | } memory_map_t; 81 | 82 | #define CHECK_FLAG(flags,bit) ((flags) & (1 << (bit))) 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /sgos2/kernel/include/arch_i386/terminal.h: -------------------------------------------------------------------------------- 1 | #ifndef _TERMINAL_H_ 2 | #define _TERMINAL_H_ 3 | 4 | void ArClearScreen(); 5 | void ArPrintChar(char ch); 6 | 7 | 8 | #endif 9 | 10 | -------------------------------------------------------------------------------- /sgos2/kernel/include/arch_i386/types.h: -------------------------------------------------------------------------------- 1 | #ifndef _TYPES_H_ 2 | #define _TYPES_H_ 3 | 4 | //common types 5 | typedef unsigned char uchar; 6 | typedef unsigned short ushort; 7 | typedef unsigned int uint; 8 | typedef unsigned char byte; 9 | 10 | typedef unsigned int size_t; 11 | typedef unsigned int time_t; 12 | 13 | typedef unsigned char t_8; 14 | typedef unsigned short t_16; 15 | typedef unsigned t_32; 16 | typedef unsigned long long t_64 17 | ; 18 | typedef signed char int8_t; 19 | typedef unsigned char uint8_t; 20 | typedef short int16_t; 21 | typedef unsigned short uint16_t; 22 | typedef int int32_t; 23 | typedef unsigned uint32_t; 24 | typedef long long int64_t; 25 | typedef unsigned long long uint64_t; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /sgos2/kernel/include/bigblock.h: -------------------------------------------------------------------------------- 1 | #ifndef _BIGBLOCK_H_ 2 | #define _BIGBLOCK_H_ 3 | 4 | //类型定义 5 | //typedef unsigned long size_t; 6 | #include 7 | #include 8 | 9 | #define FREE_NODE 0x00000001 10 | 11 | #define MAX_HASH_ENTRY 15 12 | 13 | typedef struct bnode{ 14 | //邻接链表 15 | struct bnode* prev; 16 | struct bnode* next; 17 | //Hash table 18 | struct bnode* hash_prev; 19 | struct bnode* hash_next; 20 | //分配描述 21 | size_t attribute; 22 | size_t size; //不包括分配描述符大小 23 | size_t addr; //内存地址 24 | }bnode_t; 25 | 26 | typedef struct bigblock{ 27 | struct bnode* first_node; //used for debugging. 28 | struct bnode* free_table[MAX_HASH_ENTRY]; 29 | KSemaphore semaphore; 30 | }bigblock_t; 31 | 32 | 33 | //导出函数 34 | void* bb_alloc(bigblock_t*, size_t); 35 | void* bb_alloc_ex(bigblock_t* who, size_t addr, size_t siz ); 36 | size_t bb_free(bigblock_t*, void*); 37 | void* bb_calloc(bigblock_t*, size_t, size_t); 38 | void* bb_realloc(bigblock_t*, void*, size_t); 39 | void bb_init_block(bigblock_t* who, size_t addr, size_t size); 40 | void bb_print_block(bigblock_t* who); 41 | void bb_free_all(bigblock_t * who); 42 | int bb_check_allocated(bigblock_t* who, size_t addr ); 43 | 44 | #endif //_BIGBLOCK_H_ 45 | -------------------------------------------------------------------------------- /sgos2/kernel/include/ctype.h: -------------------------------------------------------------------------------- 1 | #ifndef _CTYPE_H 2 | #define _CTYPE_H 3 | 4 | #define _U 0x01 /* upper */ 5 | #define _L 0x02 /* lower */ 6 | #define _D 0x04 /* digit */ 7 | #define _C 0x08 /* cntrl */ 8 | #define _P 0x10 /* punct */ 9 | #define _S 0x20 /* white space (space/lf/tab) */ 10 | #define _X 0x40 /* hex digit */ 11 | #define _SP 0x80 /* hard space (0x20) */ 12 | 13 | extern unsigned char _ctype[]; 14 | extern char _ctmp; 15 | 16 | #define isalnum(c) ((_ctype+1)[c]&(_U|_L|_D)) 17 | #define isalpha(c) ((_ctype+1)[c]&(_U|_L)) 18 | #define iscntrl(c) ((_ctype+1)[c]&(_C)) 19 | #define isdigit(c) ((_ctype+1)[c]&(_D)) 20 | #define isgraph(c) ((_ctype+1)[c]&(_P|_U|_L|_D)) 21 | #define islower(c) ((_ctype+1)[c]&(_L)) 22 | #define isprint(c) ((_ctype+1)[c]&(_P|_U|_L|_D|_SP)) 23 | #define ispunct(c) ((_ctype+1)[c]&(_P)) 24 | #define isspace(c) ((_ctype+1)[c]&(_S)) 25 | #define isupper(c) ((_ctype+1)[c]&(_U)) 26 | #define isxdigit(c) ((_ctype+1)[c]&(_D|_X)) 27 | 28 | #define isascii(c) (((unsigned) c)<=0x7f) 29 | #define toascii(c) (((unsigned) c)&0x7f) 30 | 31 | #define tolower(c) (_ctmp=c,isupper(_ctmp)?_ctmp-('A'-'a'):_ctmp) 32 | #define toupper(c) (_ctmp=c,islower(_ctmp)?_ctmp-('a'-'A'):_ctmp) 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /sgos2/kernel/include/ipc.h: -------------------------------------------------------------------------------- 1 | #ifndef _IPC_H_ 2 | #define _IPC_H_ 3 | 4 | #include 5 | #include 6 | 7 | #define down(a) IpcLockSemaphore(a) 8 | #define up(a) IpcUnlockSemaphore(a) 9 | 10 | struct KThread; 11 | 12 | typedef struct KSemaphore{ 13 | KQueue wait_queue; 14 | uint value; 15 | }KSemaphore; 16 | 17 | void IpcRemoveSleepingThread( KSemaphore* mut, struct KThread* thr ); 18 | int IpcTryLockSemaphore( KSemaphore *mut ); 19 | void IpcLockSemaphore( KSemaphore *mut ); 20 | void IpcInitializeSemaphore( KSemaphore *mut ); 21 | void IpcInitializeSemaphoreValue( KSemaphore *mut, int value ); 22 | void IpcUnlockSemaphore( KSemaphore *mut ); 23 | void IpcDestroySemaphore( KSemaphore *mut ); 24 | 25 | 26 | //Give it a proper value!! 27 | #define MAX_MESSAGES_IN_QUEUE 100 28 | 29 | //内核消息描述符 30 | //include the message structure in user mode 31 | typedef struct KMessage{ 32 | Message UserMessage; 33 | struct KThread * Destination; 34 | struct KThread * Source; 35 | }KMessage; 36 | 37 | //发送消息 38 | //发送后返回 39 | int IpcSend( 40 | Message* usermsg, //消息正文 41 | uint flag //发送参数 42 | ); 43 | //Quick way of IpcSend 44 | int IpcQuickSend( uint tid, uint cmd, uint arg1, uint arg2 ); 45 | 46 | //发送后,等待对方处理完毕,超时返回负值 47 | int IpcCall( 48 | Message* usermsg, //消息正文 49 | uint flag, //发送参数 50 | int timeout //超时值 51 | ); 52 | 53 | //接收消息 54 | //timeout为0,表示不等待,立刻返回。 55 | //timeout为-1,表示一直等到。 56 | //timeout非0和-1,则等待一定的毫秒。 57 | int IpcReceive( 58 | Message* usermsg, //消息正文缓冲区 59 | uint flag, //接收参数 60 | int timeout //超时值 61 | ); 62 | 63 | //回应IpcCall 64 | int IpcReply( 65 | Message* usermsg, // 66 | uint flag // 67 | ); 68 | 69 | // 线程消息初始化 70 | void IpcInitializeThreadMessageQueue( struct KThread* thr ); 71 | // 释放消息队列占用的空间 72 | void IpcDestroyThreadMessageQueue( struct KThread* thr ); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /sgos2/kernel/include/kd.h: -------------------------------------------------------------------------------- 1 | //090804 Ported from SGOS1 2 | 3 | #ifndef _KD_H_ 4 | #define _KD_H_ 5 | 6 | #include 7 | 8 | #define ASSERT(condition) assert_err( (char*)__FILE__, (char*)__FUNCTION__, __LINE__, condition ) 9 | #define PERROR(args ...) print_err( (char*)__FILE__, (char*)__FUNCTION__, __LINE__, ##args ) 10 | #define KERROR(args ...) kernel_err( (char*)__FILE__, (char*)__FUNCTION__, __LINE__, ##args ) 11 | 12 | void print_err(char* file, char* function, int line, const char *fmt, ...); 13 | void kernel_err(char* file, char* function, int line, const char *fmt, ...); 14 | void assert_err(char* file, char* function, int line, int b); 15 | 16 | void KeBugCheck( const char *s ); //内核死亡 17 | void KdPrintf(const char *fmt, ...); //打印字符信息 18 | int KdPrint( char *buf ); //同上,只是不能格式化字符串 19 | void KdInitializeDebugger(); //初始化字符调试工具 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /sgos2/kernel/include/ke.h: -------------------------------------------------------------------------------- 1 | // Micro Kernel 2 | #ifndef _KE_H 3 | #define _KE_H 4 | 5 | #include 6 | 7 | int KeStartSystemThread(); 8 | int KeCreateBaseService(); 9 | int KeLoadPeExecutable(); 10 | int KeCreateRemoteThread(); 11 | 12 | void KeStartOs( size_t boot_info ); 13 | void KeBugCheck(const char *s ); 14 | void KeResumeStart(); 15 | 16 | void KeInitializeSystemInformation(); 17 | SystemInformation* KeGetSystemInforamtion(); 18 | 19 | int KeHardwareInterruptMessage( int no ); 20 | void KeDeleteHardwareInterruptHandler( int no, uint threadId ); 21 | int KeAddHardwareInterruptHandler( int no, uint threadId ); 22 | void KeEnableHardwareInterrupt( int no, int b ); 23 | void KeInitializeHardwareIntterupt(); 24 | 25 | #endif 26 | 27 | -------------------------------------------------------------------------------- /sgos2/kernel/include/loader.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOADER_H_ 2 | #define _LOADER_H_ 3 | 4 | #include 5 | 6 | struct PROCESS; 7 | // 从文件中加载可执行文件。 8 | int loader_load( struct PROCESS* proc, char* filename, uchar share, MODULE** mod ); 9 | int loader_process( struct PROCESS* proc, char* file, uchar* data, uchar share, MODULE** ret_mod ); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /sgos2/kernel/include/rtl.h: -------------------------------------------------------------------------------- 1 | #ifndef _RTL_H 2 | #define _RTL_H 3 | 4 | // Bits Functioins 5 | void * RtlCopyMemory(void * dest, const void * src, int n); 6 | void * RtlCopyMemory16(void * dest, const void * src, int n); 7 | void * RtlCopyMemory32(void * dest, const void * src, int n); 8 | void * RtlZeroMemory(void * s, int count); 9 | void * RtlZeroMemory16(void * s, int count); 10 | void * RtlZeroMemory32(void * s, int count); 11 | 12 | #define QUEUE_NAME_LEN 16 13 | typedef int (*KQueueSearcher)(const void *, const void *); 14 | typedef void (*KQueueEraser)(const void *); 15 | typedef struct KQueueNode{ 16 | struct KQueueNode *prev, *next; 17 | void* v; 18 | }KQueueNode; 19 | typedef struct KQueue{ 20 | void* semaphore; //这里用spin_lock好不? 21 | int cur_num; //当前链表元素个数 22 | int max_num; //最大允许元素个数 23 | KQueueNode *front; //头节点 24 | KQueueNode *back; //尾节点 25 | char name[QUEUE_NAME_LEN]; //used for debugging. 26 | KQueueEraser del_func; 27 | char use_sem; 28 | }KQueue; 29 | 30 | #define QUEUE_UNINTERRUPTABLE 1 31 | 32 | int RtlCreateQueue( KQueue* l, int size, KQueueEraser del, const char* name, int use_sem ); 33 | void* RtlPopBackQueue( KQueue* l ); 34 | void* RtlPopFrontQueue( KQueue* l ); 35 | int RtlPushFrontQueue( KQueue* l, void* data ); 36 | int RtlPushBackQueue( KQueue* l, void* data ); 37 | void* RtlSearchQueue( KQueue* l, void*, KQueueSearcher search, KQueueNode** ret_nod ); 38 | void* RtlQuickSearchQueue( KQueue* l, void*, KQueueNode** ret_nod ); 39 | void RtlDestroyQueue( KQueue* l ); 40 | int RtlIsEmptyQueue( KQueue* l ); 41 | void RtlRemoveQueueElement( KQueue* l, KQueueNode* data ); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /sgos2/kernel/include/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef _STDARG_H 2 | #define _STDARG_H 3 | 4 | typedef char *va_list; 5 | 6 | /* Amount of space required in an argument list for an arg of type TYPE. 7 | TYPE may alternatively be an expression whose type is used. */ 8 | 9 | #define __va_rounded_size(TYPE) \ 10 | (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int)) 11 | 12 | #ifndef __sparc__ 13 | #define va_start(AP, LASTARG) \ 14 | (AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG))) 15 | #else 16 | #define va_start(AP, LASTARG) \ 17 | (__builtin_saveregs (), \ 18 | AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG))) 19 | #endif 20 | 21 | void va_end (va_list); /* Defined in gnulib */ 22 | #define va_end(AP) 23 | 24 | #define va_arg(AP, TYPE) \ 25 | (AP += __va_rounded_size (TYPE), \ 26 | *((TYPE *) (AP - __va_rounded_size (TYPE)))) 27 | 28 | #endif /* _STDARG_H */ 29 | -------------------------------------------------------------------------------- /sgos2/kernel/include/time.h: -------------------------------------------------------------------------------- 1 | // time structures 2 | #ifndef __TIME_H__ 3 | #define __TIME_H__ 4 | 5 | #include 6 | 7 | //! the old time struct 8 | #ifndef _TM_DEFINED 9 | struct tm 10 | { 11 | int tm_sec; // 12 | int tm_min; // 13 | int tm_hour; // 14 | int tm_mday; //day of the month 15 | int tm_mon; //month of the year 16 | int tm_year; // 17 | int tm_wday; //day of the week? 0~6 18 | int tm_yday; //day of the year 19 | int tm_isdst; //is leap year? 20 | }; 21 | #define _TM_DEFINED 22 | #endif //_TM_DEFINED 23 | 24 | //! ... 25 | time_t time(time_t* ); 26 | //! Get a format string by a time, 27 | //! The function returns the length of the string, 0 means failed, others > 0 means success 28 | //! Note: the str buffer must be enough big, normally 48 bytes 29 | int strtime( time_t*, char* str ); 30 | //! From struct tm to time_t 31 | time_t mktime( const struct tm* ); 32 | //! From time_t to struct tm 33 | //! If failed, it will return 0, others > 0 means success 34 | int gettime( const time_t*, struct tm* ); 35 | 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /sgos2/kernel/ipc/semaphore.c: -------------------------------------------------------------------------------- 1 | //semaphore 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | //尝试P 12 | int IpcTryLockSemaphore( KSemaphore *sem ) 13 | { 14 | uint eflags; 15 | //we don't want to be interrupt 16 | ArLocalSaveIrq( eflags ); 17 | while( --sem->value ){ 18 | //don't have it.. 19 | ArLocalRestoreIrq(eflags); 20 | sem->value ++; 21 | return 0; 22 | } 23 | //have it 24 | ArLocalRestoreIrq(eflags); 25 | return 1; 26 | } 27 | 28 | //P 29 | void IpcLockSemaphore( KSemaphore *sem ) 30 | { 31 | uint eflags; 32 | //we don't want to be interrupt 33 | ArLocalSaveIrq( eflags ); 34 | if( --sem->value ){ 35 | //dont have it, then sleep 36 | //跟队末尾 37 | RtlPushFrontQueue( &sem->wait_queue, TmGetCurrentThread() ); 38 | TmSleepThread( TmGetCurrentThread(), INFINITE ); 39 | //now, we have it!! 40 | } 41 | ArLocalRestoreIrq(eflags); 42 | } 43 | 44 | //初始化 45 | void IpcInitializeSemaphore( KSemaphore *sem ) 46 | { 47 | IpcInitializeSemaphoreValue( sem, 1 ); 48 | } 49 | 50 | //初始化 51 | void IpcInitializeSemaphoreValue( KSemaphore *sem, int v ) 52 | { 53 | RtlCreateQueue( &sem->wait_queue, 0, NULL, "sema_queue", 0 ); 54 | sem->value = v; 55 | } 56 | 57 | //V 58 | void IpcUnlockSemaphore( KSemaphore *sem ) 59 | { 60 | uint eflags; 61 | KThread* thr = NULL; 62 | ArLocalSaveIrq( eflags ); 63 | thr = RtlPopBackQueue( &sem->wait_queue ); 64 | ArLocalRestoreIrq( eflags ); 65 | sem->value ++; 66 | if( thr ){ 67 | TmWakeupThread( thr ); 68 | } 69 | } 70 | 71 | //释放一个sema 72 | void IpcDestroySemaphore( KSemaphore *sem ) 73 | { 74 | KThread* thr; 75 | uint eflags; 76 | ArLocalSaveIrq( eflags ); 77 | while( (thr=RtlPopBackQueue(&sem->wait_queue)) ){ 78 | TmWakeupThread( thr ); 79 | } 80 | sem->value = 0; 81 | ArLocalRestoreIrq( eflags ); 82 | } 83 | 84 | //删除一个链表中的项 85 | void IpcRemoveSleepingThread( KSemaphore* sem, KThread* thr ) 86 | { 87 | uint eflags; 88 | KQueueNode *nod; 89 | ArLocalSaveIrq(eflags); 90 | thr = RtlQuickSearchQueue( &sem->wait_queue, thr, &nod ); 91 | if( thr ) 92 | RtlRemoveQueueElement( &sem->wait_queue, nod ); 93 | else 94 | PERROR("##thread not found."); 95 | ArLocalRestoreIrq(eflags); 96 | } 97 | -------------------------------------------------------------------------------- /sgos2/kernel/kd/debug.c: -------------------------------------------------------------------------------- 1 | // Ported from SGOS1 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | //#define KEEP_INTEGRAL_INFO 15 | 16 | static int PrintString(const char* str); 17 | static int (*Printer)(const char*); 18 | 19 | // this should be called as early as possible.. 20 | void KdInitializeDebugger() 21 | { 22 | Printer = PrintString; 23 | ArClearScreen(); 24 | Printer("System Debugger( 2009-08-04 ) for SGOS2.\n"); 25 | } 26 | 27 | // generally use PERROR instead of KdPrintf 28 | void KdPrintf(const char *fmt, ...) 29 | { 30 | char printbuf[256]; 31 | va_list args; 32 | int i; 33 | #ifdef KEEP_INTEGRAL_INFO 34 | uint flags; 35 | va_start(args, fmt); 36 | i=vsprintf( printbuf, fmt, args ); 37 | ArLocalSaveIrq(flags); 38 | KdPrint( printbuf ); 39 | ArLocalRestoreIrq(flags); 40 | #else 41 | va_start(args, fmt); 42 | i=vsprintf( printbuf, fmt, args ); 43 | KdPrint( printbuf ); 44 | #endif 45 | va_end(args); 46 | } 47 | 48 | //打印调试信息 49 | int KdPrint( char *buf ) 50 | { 51 | return Printer(buf); 52 | } 53 | 54 | //默认终端输出 55 | static int PrintString( const char* buf ) 56 | { 57 | int i=0; 58 | while(buf[i]) 59 | ArPrintChar(buf[i++]); 60 | return i; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /sgos2/kernel/kd/format.c: -------------------------------------------------------------------------------- 1 | //ported from SGOS1 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //发出错误警告 10 | void print_err(char* file, char* function, int line, const char *fmt, ...) 11 | { 12 | va_list args; 13 | char printbuf[256]; 14 | int i; 15 | va_start(args, fmt); 16 | i=vsprintf( printbuf, fmt, args ); 17 | va_end(args); 18 | KdPrintf("[%s]%s(%d): %s\n", file, function, line, printbuf ); 19 | } 20 | 21 | //Fatal Error 严重错误 22 | void kernel_err(char* file, char* function, int line, const char *fmt, ...) 23 | { 24 | va_list args; 25 | char printbuf[256]; 26 | int i; 27 | KThread* thr; 28 | va_start(args, fmt); 29 | i=vsprintf( printbuf, fmt, args ); 30 | va_end(args); 31 | KdPrintf("[%s]%s(%d): %s\n", file, function, line, printbuf ); 32 | thr = TmGetCurrentThread(); 33 | if( thr ){ 34 | TmTerminateThread( thr, -1 ); 35 | }else 36 | KeBugCheck("System halted."); 37 | } 38 | 39 | //诊断错误 40 | void assert_err(char* file, char* function, int line, int b ) 41 | { 42 | KThread* thr; 43 | if( b ) 44 | return; 45 | KdPrintf("[%s]%s(%d): Assertion failed.\n", file, function, line ); 46 | thr = TmGetCurrentThread(); 47 | if( thr ){ 48 | KdPrintf("Terminated. tid:%x SpaceId:%x\n", thr->ThreadId, thr->Space->SpaceId ); 49 | TmTerminateThread( thr, -1 ); 50 | }else 51 | KeBugCheck("System halted."); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /sgos2/kernel/lib/bits.c: -------------------------------------------------------------------------------- 1 | // bits.c 2 | 3 | #include 4 | #include 5 | 6 | void * RtlCopyMemory(void * dest, const void * src, int n) 7 | { 8 | return memcpy8( dest, src, n ); 9 | } 10 | 11 | void * RtlCopyMemory16(void * dest, const void * src, int n) 12 | { 13 | return memcpy16( dest, src, n ); 14 | } 15 | 16 | void * RtlCopyMemory32(void * dest, const void * src, int n) 17 | { 18 | return memcpy32( dest, src, n ); 19 | } 20 | 21 | void * RtlZeroMemory(void * s, int count) 22 | { 23 | return memset8( s, 0, count ); 24 | } 25 | 26 | void * RtlZeroMemory16(void * s, int count) 27 | { 28 | return memset16( s, 0, count ); 29 | } 30 | 31 | void * RtlZeroMemory32(void * s, int count) 32 | { 33 | return memset32( s, 0, count ); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /sgos2/kernel/lib/ctype.c: -------------------------------------------------------------------------------- 1 | //ported from SGOS1 2 | 3 | #include 4 | 5 | char _ctmp; 6 | unsigned char _ctype[] = {0x00, /* EOF */ 7 | _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ 8 | _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ 9 | _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ 10 | _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */ 11 | _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */ 12 | _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */ 13 | _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */ 14 | _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */ 15 | _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */ 16 | _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */ 17 | _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */ 18 | _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */ 19 | _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */ 20 | _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */ 21 | _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */ 22 | _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */ 23 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */ 24 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */ 25 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 160-175 */ 26 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 176-191 */ 27 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 192-207 */ 28 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 208-223 */ 29 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 224-239 */ 30 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* 240-255 */ 31 | 32 | -------------------------------------------------------------------------------- /sgos2/kernel/mm/globalmemory.c: -------------------------------------------------------------------------------- 1 | //global memory management 2 | //provide global memory for storing global informations 3 | //use 0xE0000000 - 0xE0400000 4MB VIRTUAL_MEMORY 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | //Global Memory Structure 10 | KVirtualMemory GlobalVirtualMemory; 11 | 12 | // 全局空间内存初始化 13 | void MmInitializeGlobalMemoryPool() 14 | { 15 | MmInitializeVirtualMemory( &GlobalVirtualMemory, 16 | GLOBAL_MEMORY_BEG, //address 17 | GLOBAL_MEMORY_END //size 18 | ); 19 | } 20 | 21 | // 分配全局空间内存 22 | void* MmAllocateGlobalMemory( size_t siz, uint pattr, uint flag) 23 | { 24 | void *ptr; 25 | int ret; 26 | ASSERT( siz%PAGE_SIZE==0 ); 27 | pattr |= PAGE_ATTR_USER|PAGE_ATTR_PRESENT; 28 | ptr = MmAllocateVirtualMemory( &GlobalVirtualMemory, 0, siz, pattr, flag|ALLOC_RANDOM ); 29 | if( ptr==NULL ) 30 | return NULL; 31 | if( !(flag&ALLOC_VIRTUAL) && !(flag&ALLOC_LAZY) ){ //是否立刻分配物理地址 32 | ret = MmAcquireMultiplePhysicalPages( MmGetCurrentSpace(), (size_t)ptr, 33 | siz, pattr, MAP_ATTRIBUTE ); 34 | if( ret<0 ){ 35 | MmFreeGlobalMemory(ptr); 36 | return NULL; 37 | } 38 | } 39 | return ptr; 40 | } 41 | 42 | // 释放全局空间的内存 43 | void MmFreeGlobalMemory(void* p) 44 | { 45 | size_t siz; 46 | if(!p) 47 | return; 48 | siz = MmFreeVirtualMemory( &GlobalVirtualMemory, p ); 49 | //释放物理地址 50 | MmReleaseMultiplePhysicalPages( MmGetCurrentSpace(), (size_t)p, siz ); 51 | } 52 | -------------------------------------------------------------------------------- /sgos2/kernel/mm/usermemory.c: -------------------------------------------------------------------------------- 1 | //user space memory bigblock allocator 2 | //provide a lot of memory for running SGOS2 user process 3 | //use 0x00000000 - 0x80000000 2GB VIRTUAL_MEMORY 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | // 分配指定地址的用户态空间内存 12 | void* MmAllocateUserMemoryAddress(KSpace* space, size_t addr, size_t siz, uint pattr, uint flag) 13 | { 14 | KMemoryInformation* info; 15 | void *ptr; 16 | int ret; 17 | if( !space ) 18 | return NULL; 19 | ASSERT( addr%PAGE_SIZE==0 && siz%PAGE_SIZE==0 ); 20 | //change some mem info 21 | info = &space->MemoryInformation; 22 | info->UserMemoryAllocated += siz; 23 | if( info->UserMemoryAllocated > info->UserMemoryCapacity ){ 24 | info->UserMemoryAllocated -= siz; 25 | PERROR("##user memory used out!"); 26 | return NULL; 27 | } 28 | pattr |= PAGE_ATTR_USER|PAGE_ATTR_PRESENT; 29 | ptr = MmAllocateVirtualMemory( &space->VirtualMemory, addr, siz, pattr, flag ); 30 | if( ptr==NULL ){ 31 | info->UserMemoryAllocated -= siz; 32 | return NULL; 33 | } 34 | if( !(flag&ALLOC_VIRTUAL) && !(flag&ALLOC_LAZY) ){ //是否立刻分配物理地址 35 | uint mapFlag = MAP_ATTRIBUTE; 36 | if( flag&ALLOC_ZERO ) 37 | mapFlag |= MAP_ZERO; 38 | ret = MmAcquireMultiplePhysicalPages( space, (size_t)ptr, siz, pattr, mapFlag ); 39 | if( ret<0 ){ 40 | MmFreeUserMemory(space, ptr); 41 | return NULL; 42 | } 43 | } 44 | return ptr; 45 | } 46 | 47 | // 分配用户态空间的内存 48 | void* MmAllocateUserMemory(KSpace* space, size_t siz, uint attr, uint flag) 49 | { 50 | return MmAllocateUserMemoryAddress( space, 0, siz, attr, flag|ALLOC_RANDOM ); 51 | } 52 | 53 | // 释放用户空间的内存 54 | void MmFreeUserMemory(KSpace* space, void* p) 55 | { 56 | KMemoryInformation* info; 57 | size_t siz; 58 | uint state; 59 | if( space ){ 60 | info = &space->MemoryInformation; 61 | //此时避免其他线程使用内存分配。 62 | TmSaveScheduleState(state); 63 | siz = MmFreeVirtualMemory( &space->VirtualMemory, p ); 64 | MmReleaseMultiplePhysicalPages( space, (size_t)p, siz ); 65 | TmRestoreScheduleState(state); 66 | info->UserMemoryAllocated -= siz; 67 | if( info->UserMemoryAllocated < 0 ) 68 | info->UserMemoryAllocated = 0; 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /sgos2/kernel/start/elfloader.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/kernel/start/elfloader.c -------------------------------------------------------------------------------- /sgos2/kernel/start/hwintr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define MAX_INTERRUPT 32 9 | #define MAX_HANDLER_PER_INTERRUPT 4 10 | 11 | static uint handlers[MAX_INTERRUPT][MAX_HANDLER_PER_INTERRUPT]; 12 | 13 | void KeInitializeHardwareIntterupt() 14 | { 15 | RtlZeroMemory( handlers, sizeof(handlers) ); 16 | } 17 | 18 | void KeEnableHardwareInterrupt( int no, int b ) 19 | { 20 | ArSetIrqMask( no, b ); 21 | } 22 | 23 | int KeAddHardwareInterruptHandler( int no, uint threadId ) 24 | { 25 | int i; 26 | for( i=0; i 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // 内核下创建一个服务 12 | int KeCreateBaseService(const char* srvName, size_t addr, size_t siz ) 13 | { 14 | const char* ext; 15 | int ret; 16 | //check file extension 17 | for( ext=srvName; *ext && *ext!='.'; ext++ ) 18 | ; 19 | if( *ext ){ 20 | if( ext[1]=='e' && ext[2]=='x' && ext[3]=='e' ){ //exe 21 | //it's a PE Executable 22 | KSpace* space; 23 | size_t entry; 24 | //创建地址空间 25 | KdPrintf("Found Pe Executable: %s\n", srvName ); 26 | space = MmCreateSpace( MmGetCurrentSpace() ); 27 | if( (ret=KeLoadPeExecutable( space, addr, siz, &entry )) != 0 ){ 28 | PERROR("Load failed: %s, ret=%d\n", srvName, ret ); 29 | return ret; 30 | } 31 | KThread* srvThread; 32 | srvThread = TmCreateThread( space, entry, USER_THREAD ); 33 | TmResumeThread( srvThread ); 34 | }else if( ext[1]=='c' && ext[2]=='o' && ext[3]=='m' ){//16位文件,BIOSCALL所需 35 | //映射前1MB 36 | KdPrintf("Installed bioscall service program at 0x%X\n", 0x10100 ); 37 | ArMapMultiplePages( &MmGetCurrentSpace()->PageDirectory, 0, 38 | 0, MB(1), PAGE_ATTR_PRESENT|PAGE_ATTR_WRITE, MAP_ADDRESS|MAP_ATTRIBUTE ); 39 | RtlCopyMemory( (void*)0x10100, (void*)addr, siz ); 40 | } 41 | } 42 | return 0; 43 | } 44 | 45 | //加载基础服务 46 | extern multiboot_info_t* mbi; 47 | void KeLoadBaseServices() 48 | { 49 | KdPrintf("KeLoadBaseServices.\n"); 50 | //check module 内核需要加载的基本服务信息 51 | if (CHECK_FLAG (mbi->flags, 3)) 52 | { 53 | module_t *mod; 54 | int i; 55 | mbi->mods_addr += KERNEL_BASE; //修正地址 56 | for (i = 0, mod = (module_t *) mbi->mods_addr; 57 | i < mbi->mods_count; i++, mod ++) { 58 | //修正地址 59 | mod->mod_start += KERNEL_BASE; 60 | mod->mod_end += KERNEL_BASE; 61 | mod->string += KERNEL_BASE; 62 | KeCreateBaseService( (char*)mod->string, mod->mod_start, 63 | mod->mod_end-mod->mod_start ); 64 | //wait for the service to initialize... 65 | TmSleepThread(TmGetCurrentThread(), 100); 66 | } 67 | } 68 | //加载结束。 69 | TmTerminateThread( TmGetCurrentThread(), 0 ); 70 | } 71 | -------------------------------------------------------------------------------- /sgos2/kernel/tm/sys.c: -------------------------------------------------------------------------------- 1 | //System Calls 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | #define SYSCALL0(id, type, name) static type Api_##name() 13 | #define SYSCALL1(id, type, name, atype, a) static type Api_##name( atype a ) 14 | #define SYSCALL2(id, type, name, atype, a, btype, b) static type Api_##name( atype a, btype b ) 15 | #define SYSCALL3(id, type, name, atype, a, btype, b, ctype, c) static type Api_##name( atype a, btype b, ctype c ) 16 | #define SYSCALL4(id, type, name, atype, a, btype, b, ctype, c, dtype, d) static type Api_##name( atype a, btype b, ctype c, dtype d ) 17 | 18 | #include 19 | 20 | void* SystemCallTable[] = { 21 | //0-4 22 | Api_Test, 23 | Api_Print, 24 | Api_Send, 25 | Api_Receive, 26 | Api_InvokeBiosService, 27 | }; 28 | 29 | //返回计数 30 | uint Api_Test() 31 | { 32 | static unsigned counter=0; 33 | return counter++; 34 | } 35 | 36 | //输出调试信息 37 | int Api_Print( const char* buf, int nbytes ) 38 | { 39 | int i; 40 | for( i=0; i= KERNEL_BASE ) 49 | return -ERR_WRONGARG; 50 | return IpcCall( msg, 0, timeout); 51 | } 52 | 53 | //Receive a message 54 | int Api_Receive( Message* msg, time_t timeout ) 55 | { 56 | if( (size_t)msg + sizeof(Message) >= KERNEL_BASE ) 57 | return -ERR_WRONGARG; 58 | return IpcReceive( msg, 0, timeout ); 59 | } 60 | 61 | int Api_InvokeBiosService( int int_no, void* context, size_t context_size ) 62 | { 63 | KVirtualMemoryAllocation* vma; 64 | vma = MmGetVmaByAddress( &MmGetCurrentSpace()->VirtualMemory, (size_t)context ); 65 | if( !vma || vma->VirtualAddress+vma->VirtualSize < (size_t)context + context_size ) 66 | return -ERR_WRONGARG; 67 | return ArInvokeBiosService( int_no, context, context_size ); 68 | } 69 | 70 | -------------------------------------------------------------------------------- /sgos2/qemu/bios.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/qemu/bios.bin -------------------------------------------------------------------------------- /sgos2/qemu/vgabios-cirrus.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/qemu/vgabios-cirrus.bin -------------------------------------------------------------------------------- /sgos2/qemu/vgabios.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/qemu/vgabios.bin -------------------------------------------------------------------------------- /sgos2/scripts/bochsrc.bxrc: -------------------------------------------------------------------------------- 1 | ############################################################### 2 | # bochsrc.txt file for DLX Linux disk image. 3 | ############################################################### 4 | 5 | # how much memory the emulated machine will have 6 | megs: 128 7 | 8 | # filename of ROM images 9 | romimage: file=qemu/bios.bin #BIOS-bochs-latest 10 | vgaromimage: file=qemu/vgabios.bin #VGABIOS-lgpl-latest 11 | 12 | # what disk images will be used 13 | floppya: 1_44=floppya.img, status=inserted 14 | floppyb: 1_44=floppyb.img, status=inserted 15 | 16 | # hard disk 17 | ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 18 | ata0-master: type=disk, path="sgos2.img", cylinders=81, heads=16, spt=63 19 | 20 | # choose the boot disk. 21 | boot: c 22 | 23 | # default config interface is textconfig. 24 | #config_interface: textconfig 25 | #config_interface: wx 26 | 27 | #display_library: x 28 | # other choices: win32 sdl wx carbon amigaos beos macintosh nogui rfb term svga 29 | 30 | # where do we send log messages? 31 | #log: bochsout.txt 32 | 33 | sb16: midimode=1, midi=/dev/midi00, wavemode=1, wave=/dev/dsp, loglevel=2, dmatimer=600000 34 | 35 | # disable the mouse, since DLX is text only 36 | mouse: enabled=0 37 | 38 | # enable key mapping, using US layout as default. 39 | # 40 | # NOTE: In Bochs 1.4, keyboard mapping is only 100% implemented on X windows. 41 | # However, the key mapping tables are used in the paste function, so 42 | # in the DLX Linux example I'm enabling keyboard_mapping so that paste 43 | # will work. Cut&Paste is currently implemented on win32 and X windows only. 44 | 45 | #keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-us.map 46 | #keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-fr.map 47 | #keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-de.map 48 | #keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-es.map 49 | -------------------------------------------------------------------------------- /sgos2/scripts/gdb: -------------------------------------------------------------------------------- 1 | target remote localhost:1234 2 | -------------------------------------------------------------------------------- /sgos2/scripts/menu.lst: -------------------------------------------------------------------------------- 1 | 2 | # default - boot the first entry 3 | default 0 4 | 5 | # if have problem boot the second entry 6 | fallback 1 7 | 8 | # after 30 secs boot default 9 | timeout 1 10 | 11 | # Set the color ;) 12 | color light-green/black blink-red/black 13 | 14 | # SGOS2 15 | title SGOS2 16 | root (hd0,0) 17 | kernel /sgos/kernel.bin root=c: 18 | module /sgos/vm86.com 19 | module /sgos/servicemanager.exe 20 | module /sgos/filesystem.exe 21 | module /sgos/harddisk.exe 22 | module /sgos/wprocess.exe 23 | 24 | # Halt the machine 25 | title Turn off power! 26 | halt 27 | 28 | # Reboot the computer 29 | title Reboot 30 | reboot 31 | 32 | 33 | 34 | ### following command can list the mode! 35 | # > grub 36 | # > vbeprobe 37 | 38 | # 39 | # 0x100: Packed pixel, 640x400x8 40 | # 0x101: Packed pixel, 640x480x8 41 | # 0x103: Packed pixel, 800x600x8 42 | # 0x105: Packed pixel, 1024x768x8 43 | 44 | # 0x110: Direct Color, 640x480x15 45 | # 0x113: Direct Color, 800x600x15 46 | # 0x116: Direct Color, 1024x768x15 47 | 48 | # 0x111: Direct Color, 640x480x16 49 | # 0x114: Direct Color, 800x600x16 50 | # 0x117: Direct Color, 1024x768x16 51 | 52 | # 0x112: Direct Color, 640x480x24 53 | # 0x115: Direct Color, 800x600x24 54 | # 0x118: Direct Color, 1024x768x24 55 | 56 | # 0x142: Direct Color, 640x480x32 57 | # 0x143: Direct Color, 800x600x32 58 | # 0x144: Direct Color, 1024x768x32 59 | 60 | # 0x146: Direct Color, 320x200x8 61 | -------------------------------------------------------------------------------- /sgos2/scripts/startup.txt: -------------------------------------------------------------------------------- 1 | /diskc/sgos/acpi.exe 2 | /diskc/sgos/time.exe 3 | /diskc/sgos/speaker.exe 4 | /diskc/sgos/keyboard.exe 5 | /diskc/sgos/vesa.exe 6 | /diskc/sgos/hello.exe 7 | /diskc/sgos/cat.exe /diskc/sgos/startup.txt 8 | # 9 | 每行表示一个要启动的程序。 10 | 遇到#结束。 11 | -------------------------------------------------------------------------------- /sgos2/setenv.bat: -------------------------------------------------------------------------------- 1 | path Z:\osdev\bin -------------------------------------------------------------------------------- /sgos2/sgos2.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/sgos2.7z -------------------------------------------------------------------------------- /sgos2/tools/ld2/bxml.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/tools/ld2/bxml.c -------------------------------------------------------------------------------- /sgos2/tools/ld2/elf.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/tools/ld2/elf.h -------------------------------------------------------------------------------- /sgos2/tools/ld2/ld2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/tools/ld2/ld2.exe -------------------------------------------------------------------------------- /sgos2/tools/vm86/Makefile: -------------------------------------------------------------------------------- 1 | 2 | .PHONY : all clean 3 | all: 4 | nasm vm86.asm -o vm86.com 5 | 6 | clean: 7 | $(DEL) vm86.com 8 | -------------------------------------------------------------------------------- /sgos2/tools/vm86/vm86.asm: -------------------------------------------------------------------------------- 1 | [bits 16] 2 | [section .text] 3 | [org 0x100] 4 | ; popping stack 5 | pop ax 6 | mov byte [cs:int_no], al 7 | pop es 8 | pop ds 9 | popa 10 | db 0xCD 11 | int_no 12 | db 0xFB 13 | ; save 14 | pusha 15 | push ds 16 | push es 17 | ; exit 18 | int 0xFB 19 | 20 | -------------------------------------------------------------------------------- /sgos2/tools/vm86/vm86.com: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/78/sgos/a7bf14f9decf369963e7dfb97ad77e34bb3a8955/sgos2/tools/vm86/vm86.com -------------------------------------------------------------------------------- /sgos2/tools/wf/Makefile: -------------------------------------------------------------------------------- 1 | 2 | .PHONY : all clean 3 | all: 4 | gcc wfat.c buffer.c fat32.c unicode.c -owf 5 | 6 | clean: 7 | $(DEL) wf 8 | -------------------------------------------------------------------------------- /sgos2/tools/wf/buffer.h: -------------------------------------------------------------------------------- 1 | #ifndef _BUFFER_H_ 2 | #define _BUFFER_H_ 3 | 4 | #define BLOCK_SIZE 1024 5 | typedef unsigned int t_32; 6 | typedef unsigned short t_16; 7 | typedef unsigned char t_8; 8 | 9 | int buffer_init( const char* fname ); 10 | void* buffer_read( unsigned short dev, unsigned int block ); 11 | int buffer_write( unsigned short dev, unsigned int block, void* datas ); 12 | void buffer_release( void* ); 13 | void buffer_cleanup(); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /sgos2/tools/wf/unicode.c: -------------------------------------------------------------------------------- 1 | //Huang Guan 2 | //Unicode 3 | //2007-8-16 22:44 Created 4 | //gdxxhg@gmail.com 5 | 6 | #include 7 | #include 8 | #include "unicode.h" 9 | 10 | #define UNICODE_DATABASE "unicode.db" 11 | static char* unicodeDB; 12 | 13 | static int LoadUnicodeDatabase(const char* fileName) 14 | { 15 | int len; 16 | FILE* fp; 17 | fp = fopen( fileName, "rb" ); 18 | if( fp==NULL ) 19 | return -1; 20 | fseek( fp, 0, SEEK_END ); 21 | len = ftell( fp ); 22 | fseek( fp, 0, SEEK_SET ); 23 | unicodeDB = (char*)malloc( len ); 24 | if( fread( unicodeDB, len, 1, fp ) <=0 ) 25 | { 26 | fclose( fp ); 27 | return -1; 28 | } 29 | fclose( fp ); 30 | return 0; 31 | } 32 | 33 | int unicode_init() 34 | { 35 | if( !unicodeDB ) 36 | LoadUnicodeDatabase( UNICODE_DATABASE ); 37 | return 0; 38 | } 39 | 40 | int unicode_decode( wchar_t* input, int inputLen, char* output, int outputLen ) 41 | { 42 | int i, j; 43 | for( i=0, j=0; i+1