From 14b216f2e8b391046f284249177a5cffad13b57a Mon Sep 17 00:00:00 2001 From: turbocat Date: Thu, 29 Apr 2021 18:49:57 +0000 Subject: [PATCH] - Added the simplest examples of drivers (fasm and kos32-gcc). - Added a program for testing them. git-svn-id: svn://kolibrios.org@8703 a494cfbc-eb01-0410-851d-a64ba20cac60 --- drivers/examples/asm_drv/Makefile | 9 ++++ drivers/examples/asm_drv/asm_drv.asm | 61 +++++++++++++++++++++++++ drivers/examples/c_drv/Makefile | 42 ++++++++++++++++++ drivers/examples/c_drv/c_drv.c | 21 +++++++++ drivers/examples/c_drv/drv.lds | 57 ++++++++++++++++++++++++ drivers/examples/test/Makefile | 12 +++++ drivers/examples/test/drv_test.c | 66 ++++++++++++++++++++++++++++ 7 files changed, 268 insertions(+) create mode 100644 drivers/examples/asm_drv/Makefile create mode 100644 drivers/examples/asm_drv/asm_drv.asm create mode 100644 drivers/examples/c_drv/Makefile create mode 100644 drivers/examples/c_drv/c_drv.c create mode 100644 drivers/examples/c_drv/drv.lds create mode 100644 drivers/examples/test/Makefile create mode 100644 drivers/examples/test/drv_test.c diff --git a/drivers/examples/asm_drv/Makefile b/drivers/examples/asm_drv/Makefile new file mode 100644 index 0000000000..a8cf648ad3 --- /dev/null +++ b/drivers/examples/asm_drv/Makefile @@ -0,0 +1,9 @@ +FASM = fasm +KPACK = kpack + +all: + $(FASM) asm_drv.asm + $(KPACK) asm_drv.sys + +clean: + rm *.sys diff --git a/drivers/examples/asm_drv/asm_drv.asm b/drivers/examples/asm_drv/asm_drv.asm new file mode 100644 index 0000000000..1a05b49ab8 --- /dev/null +++ b/drivers/examples/asm_drv/asm_drv.asm @@ -0,0 +1,61 @@ +format PE DLL native +entry START + +__DEBUG__ = 1 +__DEBUG_LEVEL__ = 2 + +section '.flat' readable writable executable + +include '../../proc32.inc' +include '../../struct.inc' +include '../../macros.inc' +include '../../fdo.inc' + +proc START c, reason:dword + + DEBUGF 2,"Loading 'asm_drv' driver\n" + cmp [reason], DRV_ENTRY + jne .fail + + invoke RegService, my_service, service_proc + ret + + .fail: + xor eax, eax + ret + +endp + +proc service_proc stdcall, ioctl:dword + + mov ebx, [ioctl] + mov eax, [ebx + IOCTL.io_code] + cmp eax, 0 ; FUNCTION ADD(a+b); + jne .fail + + mov eax, [ebx + IOCTL.input] + mov ecx, eax + add ecx, 4 + + mov eax, [eax] + add eax, [ecx] + + mov ecx, [ebx + IOCTL.output] + mov [ecx], eax + + xor eax, eax + ret + + .fail: + or eax, -1 + ret +endp + +data fixups +end data + +include '../../peimport.inc' + +my_service db 'asm_drv',0 ; max 16 chars include zero + +include_debug_strings diff --git a/drivers/examples/c_drv/Makefile b/drivers/examples/c_drv/Makefile new file mode 100644 index 0000000000..ac8997901b --- /dev/null +++ b/drivers/examples/c_drv/Makefile @@ -0,0 +1,42 @@ +CC = kos32-gcc +LD = kos32-ld +KPACK = kpack + + +DDK_TOPDIR = ../../ddk +DRV_INCLUDES = ../../include + +INCLUDES = -I$(DRV_INCLUDES) \ + -I$(DRV_INCLUDES)/asm \ + -I$(DRV_INCLUDES)/uapi \ + -I$(DRV_INCLUDES)/drm +NAME=c_drv + +DEFINES = -DKOLIBRI -D__KERNEL__ -DCONFIG_X86_32 -DCONFIG_DMI -DCONFIG_TINY_RCU +DEFINES+= -DCONFIG_X86_L1_CACHE_SHIFT=6 -DCONFIG_ARCH_HAS_CACHE_LINE_SIZE +DEFINES+= -DCONFIG_PRINTK + +CFLAGS= -c -O2 $(DEFINES) $(INCLUDES) -march=i686 -fno-ident -msse2 -fomit-frame-pointer -fno-builtin-printf +CFLAGS+= -mno-stack-arg-probe -mpreferred-stack-boundary=2 -mincoming-stack-boundary=2 -mno-ms-bitfields + +LIBPATH = -L $(DDK_TOPDIR) +LIBPATH+= -L ../../../contrib/sdk/lib + +LIBS:= -lddk -lcore -lgcc + +PE_FLAGS = --major-os-version 0 --minor-os-version 7 --major-subsystem-version 0 \ + --minor-subsystem-version 5 --subsystem native + +LDFLAGS = -nostdlib -shared -s $(PE_FLAGS) --image-base 0\ + --file-alignment 512 --section-alignment 4096 + +all: $(NAME).dll + +$(NAME).dll: + $(CC) $(CFLAGS) c_drv.c + $(LD) $(LIBPATH) $(LDFLAGS) -T drv.lds c_drv.o -o $@ $(NAME_OBJS) $(LIBS) + $(KPACK) $(NAME).dll + +clean: + rm -rf *.o *.dll + diff --git a/drivers/examples/c_drv/c_drv.c b/drivers/examples/c_drv/c_drv.c new file mode 100644 index 0000000000..3d4ccaaa22 --- /dev/null +++ b/drivers/examples/c_drv/c_drv.c @@ -0,0 +1,21 @@ +#include "linux/kernel.h" +#include +#include + +static int __stdcall service_proc(ioctl_t *my_ctl){ + int c = 0; + int a = *(int*)(my_ctl->input); + int b = *(int*)(my_ctl->input+4); + if(my_ctl->io_code==0){ + c = a + b; + }else{ + return -1; + } + *(int*)(my_ctl->output) = c; + return 0; +} + +unsigned drvEntry(int action, char *cmdline){ + printk("Driver c_drv.dll loaded!\n"); + return RegService("c_drv", service_proc); +} diff --git a/drivers/examples/c_drv/drv.lds b/drivers/examples/c_drv/drv.lds new file mode 100644 index 0000000000..35ed3679ea --- /dev/null +++ b/drivers/examples/c_drv/drv.lds @@ -0,0 +1,57 @@ + + +OUTPUT_FORMAT(pei-i386) + +ENTRY("_drvEntry") + +SECTIONS +{ + . = SIZEOF_HEADERS; + . = ALIGN(__section_alignment__); + + .text __image_base__ + ( __section_alignment__ < 0x1000 ? . : __section_alignment__ ) : + + { + *(.text) *(.rdata) + } + + .data ALIGN(__section_alignment__) : + { + *(.data) + } + + .bss ALIGN(__section_alignment__): + { + *(.bss) + *(COMMON) + } + + /DISCARD/ : + { + *(.debug$S) + *(.debug$T) + *(.debug$F) + *(.drectve) + *(.edata) + *(.eh_frame) + } + + .idata ALIGN(__section_alignment__): + { + SORT(*)(.idata$2) + SORT(*)(.idata$3) + /* These zeroes mark the end of the import list. */ + LONG (0); LONG (0); LONG (0); LONG (0); LONG (0); + SORT(*)(.idata$4) + SORT(*)(.idata$5) + SORT(*)(.idata$6) + SORT(*)(.idata$7) + } + + .reloc ALIGN(__section_alignment__) : + { + *(.reloc) + } + +} + diff --git a/drivers/examples/test/Makefile b/drivers/examples/test/Makefile new file mode 100644 index 0000000000..c6bc293e77 --- /dev/null +++ b/drivers/examples/test/Makefile @@ -0,0 +1,12 @@ + +KTCC=../../../programs/develop/ktcc/trunk/bin/kos32-tcc +KPACK = kpack +KLIBC = ../../../programs/develop/libraries/kolibri-libc +CFLAGS = -I $(KLIBC)/include +LDFLAGS = -nostdlib -L$(KLIBC)/lib $(KLIBC)/lib/crt0.o + +all: + $(KTCC) $(CFLAGS) $(LDFLAGS) drv_test.c -o drv_test -lc.obj + $(KPACK) --nologo drv_test +clean: + rm *.kex diff --git a/drivers/examples/test/drv_test.c b/drivers/examples/test/drv_test.c new file mode 100644 index 0000000000..1618c3f590 --- /dev/null +++ b/drivers/examples/test/drv_test.c @@ -0,0 +1,66 @@ +#include +#include +#include + +char* drv_name = NULL; + +struct{ + int a; + int b; +}add_struct; + +int sum=0; +ksys_drv_hand_t drv = 0; + +#define DRV_ADD_FUNC 0 + +int main(int argc, char** argv){ + puts("Which driver?"); + puts("1 - asm_drv.sys\n2 - c_drv.dll"); + int num_drv = (char)getchar()-'0'; + switch (num_drv) { + case 1 : + drv_name = "asm_drv"; + drv = _ksys_load_driver(drv_name); + break; + case 2 : + drv_name = "/rd/1/drivers/c_drv.dll"; + drv = _ksys_load_pe_driver(drv_name, NULL); + break; + default: + printf("No driver selected!\n"); + exit(0); + } + + if(!drv){ + printf("'%s' driver not load!\n", drv_name); + exit(0); + }else{ + printf("'%s' driver is load!\n", drv_name); + } + + + add_struct.a = 43; + add_struct.b = 532; + + ksys_drv_ctl_t ioctl; + ioctl.func_num = DRV_ADD_FUNC; + ioctl.handler = drv; + ioctl.in_data_ptr = &add_struct; + ioctl.in_data_size = sizeof(add_struct); + ioctl.out_data_ptr = ∑ + ioctl.out_data_size = sizeof(sum); + + unsigned status =_ksys_work_driver(&ioctl); + if(status==-1){ + puts("Error!"); + }else { + printf("%d + %d = %d\n", add_struct.a, add_struct.b, sum); + if(sum == add_struct.a + add_struct.b){ + puts("True!"); + }else{ + puts("False!"); + } + } + exit(0); +}