From da448a7a840203dedf023abe143b4c2293609512 Mon Sep 17 00:00:00 2001 From: "Sergey Semyonov (Serge)" Date: Thu, 30 Oct 2008 19:23:42 +0000 Subject: [PATCH] PE loader. Link kernel.mnt git-svn-id: svn://kolibrios.org@890 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/branches/kolibri_pe/bus/pci/pci32.inc | 2 +- kernel/branches/kolibri_pe/const.inc | 1 + kernel/branches/kolibri_pe/core/dll.c | 688 +++++++++++++++++++ kernel/branches/kolibri_pe/core/dll.inc | 10 + kernel/branches/kolibri_pe/core/export.asm | 43 ++ kernel/branches/kolibri_pe/core/exports.inc | 1 - kernel/branches/kolibri_pe/core/heap.inc | 8 +- kernel/branches/kolibri_pe/core/malloc.inc | 4 + kernel/branches/kolibri_pe/core/memory.inc | 16 +- kernel/branches/kolibri_pe/data32.inc | 49 +- kernel/branches/kolibri_pe/include/core.h | 5 +- kernel/branches/kolibri_pe/include/mm.h | 10 +- kernel/branches/kolibri_pe/include/types.h | 4 +- kernel/branches/kolibri_pe/kernel.asm | 49 +- kernel/branches/kolibri_pe/kernel32.inc | 1 - kernel/branches/kolibri_pe/ld.x | 11 +- kernel/branches/kolibri_pe/makefile | 15 +- 17 files changed, 862 insertions(+), 55 deletions(-) create mode 100644 kernel/branches/kolibri_pe/core/dll.c create mode 100644 kernel/branches/kolibri_pe/core/export.asm diff --git a/kernel/branches/kolibri_pe/bus/pci/pci32.inc b/kernel/branches/kolibri_pe/bus/pci/pci32.inc index 113c38782d..037cc5119e 100644 --- a/kernel/branches/kolibri_pe/bus/pci/pci32.inc +++ b/kernel/branches/kolibri_pe/bus/pci/pci32.inc @@ -32,7 +32,7 @@ $Revision$ ;*************************************************************************** align 4 - +_PciApi: pci_api: cmp [pci_access_enabled],1 diff --git a/kernel/branches/kolibri_pe/const.inc b/kernel/branches/kolibri_pe/const.inc index 0f82379560..0970d41738 100644 --- a/kernel/branches/kolibri_pe/const.inc +++ b/kernel/branches/kolibri_pe/const.inc @@ -202,6 +202,7 @@ master_tab equ (page_tabs+ (page_tabs shr 10)) ;0xFDFF70000 _16BIT_BASE equ 0x00010000 LOAD_BASE equ 0x00100000 OS_BASE equ 0xE0000000 +IMAGE_BASE equ (OS_BASE+LOAD_BASE) window_data equ OS_BASE diff --git a/kernel/branches/kolibri_pe/core/dll.c b/kernel/branches/kolibri_pe/core/dll.c new file mode 100644 index 0000000000..56235f245b --- /dev/null +++ b/kernel/branches/kolibri_pe/core/dll.c @@ -0,0 +1,688 @@ + +#include +#include +#include +#include +#include +#include + +typedef unsigned short WORD; +typedef unsigned int DWORD; +typedef unsigned int LONG; +typedef unsigned char BYTE; + +#define IMAGE_DOS_SIGNATURE 0x5A4D +#define IMAGE_NT_SIGNATURE 0x00004550 +#define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b + +#pragma pack(push,2) +typedef struct _IMAGE_DOS_HEADER +{ + WORD e_magic; + WORD e_cblp; + WORD e_cp; + WORD e_crlc; + WORD e_cparhdr; + WORD e_minalloc; + WORD e_maxalloc; + WORD e_ss; + WORD e_sp; + WORD e_csum; + WORD e_ip; + WORD e_cs; + WORD e_lfarlc; + WORD e_ovno; + WORD e_res[4]; + WORD e_oemid; + WORD e_oeminfo; + WORD e_res2[10]; + LONG e_lfanew; +} IMAGE_DOS_HEADER,*PIMAGE_DOS_HEADER; +#pragma pack(pop) + + +#pragma pack(push,4) +typedef struct _IMAGE_FILE_HEADER +{ + WORD Machine; + WORD NumberOfSections; + DWORD TimeDateStamp; + DWORD PointerToSymbolTable; + DWORD NumberOfSymbols; + WORD SizeOfOptionalHeader; + WORD Characteristics; +} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER; + +typedef struct _IMAGE_DATA_DIRECTORY { + DWORD VirtualAddress; + DWORD Size; +} IMAGE_DATA_DIRECTORY,*PIMAGE_DATA_DIRECTORY; + +#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16 + +typedef struct _IMAGE_OPTIONAL_HEADER { + WORD Magic; + BYTE MajorLinkerVersion; + BYTE MinorLinkerVersion; + DWORD SizeOfCode; + DWORD SizeOfInitializedData; + DWORD SizeOfUninitializedData; + DWORD AddressOfEntryPoint; + DWORD BaseOfCode; + DWORD BaseOfData; + DWORD ImageBase; + DWORD SectionAlignment; + DWORD FileAlignment; + WORD MajorOperatingSystemVersion; + WORD MinorOperatingSystemVersion; + WORD MajorImageVersion; + WORD MinorImageVersion; + WORD MajorSubsystemVersion; + WORD MinorSubsystemVersion; + DWORD Win32VersionValue; + DWORD SizeOfImage; + DWORD SizeOfHeaders; + DWORD CheckSum; + WORD Subsystem; + WORD DllCharacteristics; + DWORD SizeOfStackReserve; + DWORD SizeOfStackCommit; + DWORD SizeOfHeapReserve; + DWORD SizeOfHeapCommit; + DWORD LoaderFlags; + DWORD NumberOfRvaAndSizes; + IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; +} IMAGE_OPTIONAL_HEADER,*PIMAGE_OPTIONAL_HEADER; + +#pragma pack(pop) + + +#pragma pack(push,4) +typedef struct _IMAGE_NT_HEADERS +{ + DWORD Signature; + IMAGE_FILE_HEADER FileHeader; + IMAGE_OPTIONAL_HEADER OptionalHeader; +} IMAGE_NT_HEADERS32,*PIMAGE_NT_HEADERS32; + +#define IMAGE_SIZEOF_SHORT_NAME 8 + +typedef struct _IMAGE_SECTION_HEADER +{ + BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; + union + { + DWORD PhysicalAddress; + DWORD VirtualSize; + } Misc; + DWORD VirtualAddress; + DWORD SizeOfRawData; + DWORD PointerToRawData; + DWORD PointerToRelocations; + DWORD PointerToLinenumbers; + WORD NumberOfRelocations; + WORD NumberOfLinenumbers; + DWORD Characteristics; +} IMAGE_SECTION_HEADER,*PIMAGE_SECTION_HEADER; +#pragma pack(pop) + +#pragma pack(push,4) +typedef struct _IMAGE_BASE_RELOCATION { + DWORD VirtualAddress; + DWORD SizeOfBlock; +} IMAGE_BASE_RELOCATION,*PIMAGE_BASE_RELOCATION; +#pragma pack(pop) + +typedef struct _IMAGE_IMPORT_DESCRIPTOR +{ + union + { + DWORD Characteristics; + DWORD OriginalFirstThunk; + }; + DWORD TimeDateStamp; + DWORD ForwarderChain; + DWORD Name; + DWORD FirstThunk; +} IMAGE_IMPORT_DESCRIPTOR,*PIMAGE_IMPORT_DESCRIPTOR; + +typedef struct _IMAGE_THUNK_DATA32 +{ + union + { + DWORD ForwarderString; + DWORD Function; + DWORD Ordinal; + DWORD AddressOfData; + } u1; +} IMAGE_THUNK_DATA32,*PIMAGE_THUNK_DATA32; + +typedef struct _IMAGE_IMPORT_BY_NAME +{ + WORD Hint; + BYTE Name[1]; +} IMAGE_IMPORT_BY_NAME,*PIMAGE_IMPORT_BY_NAME; + +#define IMAGE_ORDINAL_FLAG 0x80000000 + +typedef struct _IMAGE_EXPORT_DIRECTORY { + DWORD Characteristics; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + DWORD Name; + DWORD Base; + DWORD NumberOfFunctions; + DWORD NumberOfNames; + DWORD AddressOfFunctions; + DWORD AddressOfNames; + DWORD AddressOfNameOrdinals; +} IMAGE_EXPORT_DIRECTORY,*PIMAGE_EXPORT_DIRECTORY; + +//extern IMAGE_EXPORT_DIRECTORY kernel_exports; + +#define MakePtr( cast, ptr, addValue ) (cast)( (addr_t)(ptr) + (addValue) ) + +typedef struct +{ + addr_t base; + addr_t frame; + md_t *md; + + IMAGE_OPTIONAL_HEADER *opthdr; + +}dll_t; + +static inline bool IsPowerOf2(u32_t val) +{ + if(val == 0) + return false; + return (val & (val - 1)) == 0; +} + + +static inline void sec_copy(void *dst, const void *src, size_t len) +{ + u32_t tmp; + __asm__ __volatile__ ( + "shrl $2, %%ecx \n\t" + "rep movsl" + :"=c"(tmp),"=S"(tmp),"=D"(tmp) + :"c"(len),"S"(src),"D"(dst) + :"cc"); +}; + +static inline void sec_clear(void *dst, size_t len) +{ + u32_t tmp; + __asm__ __volatile__ ( + "xorl %%eax, %%eax \n\t" + "rep stosb" + :"=c"(tmp),"=D"(tmp) + :"c"(len),"D"(dst) + :"eax","cc"); +}; + +int __stdcall strncmp(const char *s1, const char *s2, size_t n); + + +void __export create_image(void *img_base, void *image) asm ("CreateImage"); + +md_t* __fastcall load_image(const char *path); + + +void* __fastcall load_pe(const char *path) +{ + md_t *md; + + md = load_image(path); + + if( md ) + return (void*)md->base; + + return NULL; +}; + +typedef struct +{ + char srv_name[16]; // ASCIIZ string + u32_t magic; // +0x10 'SRV ' + size_t size; // +0x14 size of structure SRV + void *fd; // +0x18 next SRV descriptor + void *bk; // +0x1C prev SRV descriptor + addr_t base; // +0x20 service base address + addr_t entry; // +0x24 service START function + void *srv_proc; // +0x28 main service handler +}srv_t; + +typedef srv_t* __stdcall drv_entry_t(int); + +srv_t* __fastcall load_pe_driver(const char *path) +{ + PIMAGE_DOS_HEADER dos; + PIMAGE_NT_HEADERS32 nt; + + drv_entry_t *drv_entry; + md_t *md; + srv_t *srv; + + md = load_image(path); + + if( ! md ) + return 0; + + dos = (PIMAGE_DOS_HEADER)md->base; + nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew); + + drv_entry = MakePtr(drv_entry_t*, md->base, + nt->OptionalHeader.AddressOfEntryPoint); + + srv = drv_entry(1); + + if(srv != NULL) + srv->entry = nt->OptionalHeader.AddressOfEntryPoint + md->base; + + return srv; +} + +md_t* __fastcall load_image(const char *path) +{ + PIMAGE_DOS_HEADER dos; + PIMAGE_NT_HEADERS32 nt; + + md_t *img_md; + + size_t img_size; + void *img_base; + count_t img_pages; + + size_t raw_size = 0; + void *raw; + +// void *image; + + DBG("load file %s\n", path); + + raw = load_file(path, &raw_size); + + DBG("raw = %x\n\n", raw); + + dos = (PIMAGE_DOS_HEADER)raw; + + if( !raw || raw_size < sizeof(IMAGE_DOS_HEADER) ) + return NULL; + + if( dos->e_magic != IMAGE_DOS_SIGNATURE || dos->e_lfanew <= 0) + return NULL; + + nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew); + + if( (addr_t)nt < (addr_t)raw) + return NULL; + + if(nt->Signature != IMAGE_NT_SIGNATURE) + return NULL; + + if(nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) + return NULL; + + if(nt->OptionalHeader.SectionAlignment < PAGE_SIZE) + { + if(nt->OptionalHeader.FileAlignment != nt->OptionalHeader.SectionAlignment) + return NULL; + } + else if(nt->OptionalHeader.SectionAlignment < nt->OptionalHeader.FileAlignment) + return NULL; + + if(!IsPowerOf2(nt->OptionalHeader.SectionAlignment) || + !IsPowerOf2(nt->OptionalHeader.FileAlignment)) + return NULL; + + if(nt->FileHeader.NumberOfSections > 96) + return NULL; + + img_size = nt->OptionalHeader.SizeOfImage; +// img_pages = img_size / PAGE_SIZE; + + img_md = md_alloc(img_size, PG_SW); + + + if( !img_md) + { + mem_free(raw); + return NULL; + }; + + img_base = (void*)img_md->base; + + create_image(img_base, raw); + + mem_free(raw); + +// dos = (PIMAGE_DOS_HEADER)img_base; +// nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew); + + return img_md; +}; + + +/* +addr_t get_proc_addr(addr_t module, char *name) +{ + PIMAGE_DOS_HEADER expdos; + PIMAGE_NT_HEADERS32 expnt; + PIMAGE_EXPORT_DIRECTORY exp; + u32_t *functions; + char **funcname; + int ind; + + expdos = (PIMAGE_DOS_HEADER)module; + expnt = MakePtr( PIMAGE_NT_HEADERS32, expdos, expdos->e_lfanew); + + exp = MakePtr(PIMAGE_EXPORT_DIRECTORY,module, + expnt->OptionalHeader.DataDirectory[0].VirtualAddress); + + functions = MakePtr(DWORD*,exp->AddressOfFunctions,module); + funcname = MakePtr(char**,exp->AddressOfNames,module); + + for(ind=0; *funcname;funcname++,ind++) + { + if(!strcmp(name,MakePtr(char*,*funcname,module))) + return functions[ind] + module; + }; + return -1; +}; +*/ + + +void create_image(void *img_base, void *image) +{ + PIMAGE_DOS_HEADER dos; + PIMAGE_NT_HEADERS32 nt; + PIMAGE_SECTION_HEADER img_sec; + + u32_t sec_align; + int i; + + +/* assumed that image is valid */ + + dos = (PIMAGE_DOS_HEADER)image; + nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew); + + sec_copy(img_base,image,nt->OptionalHeader.SizeOfHeaders); + + img_sec = MakePtr(PIMAGE_SECTION_HEADER,nt,sizeof(IMAGE_NT_HEADERS32)); + + sec_align = nt->OptionalHeader.SectionAlignment; + + for(i=0; i< nt->FileHeader.NumberOfSections; i++) + { + char *src_ptr; + char *dest_ptr; + size_t sec_size; + + src_ptr = MakePtr(char*, image, img_sec->PointerToRawData); + dest_ptr = MakePtr(char*,img_base, img_sec->VirtualAddress); + + if(img_sec->SizeOfRawData) + sec_copy(dest_ptr, src_ptr, img_sec->SizeOfRawData); + + sec_size = (img_sec->Misc.VirtualSize + sec_align -1) & -sec_align; + + if(sec_size > img_sec->SizeOfRawData) + sec_clear(dest_ptr + img_sec->SizeOfRawData, + sec_size - img_sec->SizeOfRawData); + img_sec++; + } + + if(nt->OptionalHeader.DataDirectory[5].Size) + { + PIMAGE_BASE_RELOCATION reloc; + +/* FIXME addr_t */ + + u32_t delta = (u32_t)img_base - nt->OptionalHeader.ImageBase; + + reloc = MakePtr(PIMAGE_BASE_RELOCATION, img_base, + nt->OptionalHeader.DataDirectory[5].VirtualAddress); + + while ( reloc->SizeOfBlock != 0 ) + { + u32_t cnt; + u16_t *entry; + u16_t reltype; + u32_t offs; + + cnt = (reloc->SizeOfBlock - sizeof(*reloc))/sizeof(u16_t); + entry = MakePtr( u16_t*, reloc, sizeof(*reloc) ); + + for ( i=0; i < cnt; i++ ) + { + u16_t *p16; + u32_t *p32; + + reltype = (*entry & 0xF000) >> 12; + offs = (*entry & 0x0FFF) + reloc->VirtualAddress; + switch(reltype) + { + case 1: + p16 = MakePtr(u16_t*, img_base, offs); + *p16+= (u16_t)(delta>>16); + break; + case 2: + p16 = MakePtr(u16_t*, img_base, offs); + *p16+= (u16_t)delta; + break; + case 3: + p32 = MakePtr(u32_t*, img_base, offs); + *p32+= delta; + } + entry++; + } + reloc = MakePtr(PIMAGE_BASE_RELOCATION, reloc,reloc->SizeOfBlock); + } + }; + + if(nt->OptionalHeader.DataDirectory[1].Size) + { + PIMAGE_IMPORT_DESCRIPTOR imp; + + int warn = 0; + + imp = MakePtr(PIMAGE_IMPORT_DESCRIPTOR, img_base, + nt->OptionalHeader.DataDirectory[1].VirtualAddress); + + + + while ( 1 ) + { + PIMAGE_THUNK_DATA32 thunk; + + PIMAGE_DOS_HEADER expdos; + PIMAGE_NT_HEADERS32 expnt; + PIMAGE_EXPORT_DIRECTORY exp; + + u32_t *iat; + char *libname; + addr_t *functions; + u16_t *ordinals; + char **funcname; + + + if ( (imp->TimeDateStamp==0 ) && (imp->Name==0) ) + break; + + libname=MakePtr(char*,imp->Name, img_base); + + DBG("import from %s\n",libname); + + expdos = (PIMAGE_DOS_HEADER)IMAGE_BASE; + expnt = MakePtr( PIMAGE_NT_HEADERS32, expdos, expdos->e_lfanew); + + exp = MakePtr(PIMAGE_EXPORT_DIRECTORY,LOAD_BASE, + expnt->OptionalHeader.DataDirectory[0].VirtualAddress); + + functions = MakePtr(DWORD*,exp->AddressOfFunctions,LOAD_BASE); + ordinals = MakePtr(WORD*, exp->AddressOfNameOrdinals,LOAD_BASE); + funcname = MakePtr(char**, exp->AddressOfNames,LOAD_BASE); + + thunk = MakePtr(PIMAGE_THUNK_DATA32, + imp->Characteristics, img_base); + iat= MakePtr(DWORD*,imp->FirstThunk, img_base); + + while ( 1 ) // Loop forever (or until we break out) + { + PIMAGE_IMPORT_BY_NAME ord; + addr_t addr; + + if ( thunk->u1.AddressOfData == 0 ) + break; + + if ( thunk->u1.Ordinal & IMAGE_ORDINAL_FLAG ) + { + // printf(" %4u\n", thunk->u1.Ordinal & 0xFFFF); + break; + } + else + { + ord = MakePtr(PIMAGE_IMPORT_BY_NAME, + thunk->u1.AddressOfData, img_base); + *iat=0; + + DBG("import %s", ord->Name); + + if(strncmp(ord->Name, + MakePtr(char*,funcname[ord->Hint],LOAD_BASE),32)) + { + int ind; + char **names=funcname; + + for(names = funcname,ind = 0; + ind < exp->NumberOfNames; names++,ind++) + { + if(!strncmp(ord->Name,MakePtr(char*,*names,LOAD_BASE),32)) + { + DBG(" \tat %x\n", functions[ind] + LOAD_BASE); + *iat = functions[ind] + LOAD_BASE; + break; + }; + }; + if(ind == exp->NumberOfNames) + { + DBG(" unresolved import %s\n",ord->Name); + warn=1; + }; + } + else + { + DBG(" \tat %x\n", functions[ord->Hint] + LOAD_BASE); + *iat = functions[ord->Hint] + LOAD_BASE; + }; + }; + thunk++; // Advance to next thunk + iat++; + } + imp++; // advance to next IMAGE_IMPORT_DESCRIPTOR + }; + }; + + DBG("\ncreate pe base %x, size %x, %d sections\n\n",img_base, + nt->OptionalHeader.SizeOfImage, nt->FileHeader.NumberOfSections); +}; + + + + + +/* + +u32 map_PE(u32 base, void *image) +{ + PIMAGE_DOS_HEADER dos; + PIMAGE_NT_HEADERS32 nt; + PIMAGE_SECTION_HEADER sec; + + int i; + int pages; + + dos = (PIMAGE_DOS_HEADER)image; + nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew); + + + img_size = nt->OptionalHeader.SizeOfImage; + img_pages = img_size / PAGE_SIZE; + + img_md = md_alloc(img_size, PG_SW); + + if( !img_md) + return NULL; + + + + scopy(base,(u32)image,nt->OptionalHeader.SizeOfHeaders); + + sec = MakePtr(PIMAGE_SECTION_HEADER,nt,sizeof(IMAGE_NT_HEADERS32)); + + + if(nt->OptionalHeader.DataDirectory[1].Size) + { + PIMAGE_IMPORT_DESCRIPTOR imp; + + imp = MakePtr(PIMAGE_IMPORT_DESCRIPTOR,base, + nt->OptionalHeader.DataDirectory[1].VirtualAddress); + while ( 1 ) + { + PIMAGE_THUNK_DATA32 thunk; + u32 *iat; + char *libname; + + if ( (imp->TimeDateStamp==0 ) && (imp->Name==0) ) + break; + + + thunk = MakePtr(PIMAGE_THUNK_DATA32, + imp->Characteristics, base); + iat= MakePtr(DWORD*,imp->FirstThunk, base); + + while ( 1 ) // Loop forever (or until we break out) + { + PIMAGE_IMPORT_BY_NAME ord; + + u32 addr; + + if ( thunk->u1.AddressOfData == 0 ) + break; + + if ( thunk->u1.Ordinal & IMAGE_ORDINAL_FLAG ) + { + // printf(" %4u\n", thunk->u1.Ordinal & 0xFFFF); + break; + } + else + { + PKERNEL_EXPORT exp; + exp = kernel_export; + + ord = MakePtr(PIMAGE_IMPORT_BY_NAME, + thunk->u1.AddressOfData,base); + *iat=-1; + + do + { + if(!strncmp(ord->Name,exp->name,16)) + { + *iat = exp->address; + break; + } + exp++; + } while(exp->name != 0); + }; + thunk++; // Advance to next thunk + iat++; + } + imp++; // advance to next IMAGE_IMPORT_DESCRIPTOR + } + }; + +*/ diff --git a/kernel/branches/kolibri_pe/core/dll.inc b/kernel/branches/kolibri_pe/core/dll.inc index 0c41f4892c..530fbcb315 100644 --- a/kernel/branches/kolibri_pe/core/dll.inc +++ b/kernel/branches/kolibri_pe/core/dll.inc @@ -208,6 +208,7 @@ proc get_notify stdcall, p_ev:dword endp align 4 +_PciRead32: proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword push ebx xor eax, eax @@ -222,6 +223,7 @@ proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword endp align 4 +_PciRead16: proc pci_read16 stdcall, bus:dword, devfn:dword, reg:dword push ebx xor eax, eax @@ -236,6 +238,7 @@ proc pci_read16 stdcall, bus:dword, devfn:dword, reg:dword endp align 4 +_PciRead8: proc pci_read8 stdcall, bus:dword, devfn:dword, reg:dword push ebx xor eax, eax @@ -250,6 +253,7 @@ proc pci_read8 stdcall, bus:dword, devfn:dword, reg:dword endp align 4 +_PciWrite8: proc pci_write8 stdcall, bus:dword, devfn:dword, reg:dword, val:dword push ebx xor eax, eax @@ -265,6 +269,7 @@ proc pci_write8 stdcall, bus:dword, devfn:dword, reg:dword, val:dword endp align 4 +_PciWrite16: proc pci_write16 stdcall, bus:dword, devfn:dword, reg:dword, val:dword push ebx xor eax, eax @@ -280,6 +285,7 @@ proc pci_write16 stdcall, bus:dword, devfn:dword, reg:dword, val:dword endp align 4 +_PciWrite32: proc pci_write32 stdcall, bus:dword, devfn:dword, reg:dword, val:dword push ebx xor eax, eax @@ -387,6 +393,7 @@ proc get_service stdcall, sz_name:dword endp align 4 +_RegService: proc reg_service stdcall, name:dword, handler:dword push ebx @@ -562,6 +569,7 @@ endp ; loaded by the load_file() function align 4 +_LoadFile: _load_file@4: proc load_file stdcall, file_name:dword locals @@ -1110,6 +1118,7 @@ endp ; ebx= pid align 4 +_CreateObject: create_kernel_object: push ebx @@ -1138,6 +1147,7 @@ create_kernel_object: ; eax= object align 4 +_DestroyObject: destroy_kernel_object: pushfd diff --git a/kernel/branches/kolibri_pe/core/export.asm b/kernel/branches/kolibri_pe/core/export.asm new file mode 100644 index 0000000000..69e096d2cb --- /dev/null +++ b/kernel/branches/kolibri_pe/core/export.asm @@ -0,0 +1,43 @@ + .file "export.asm" + .intel_syntax + + + .section .drectve + .ascii " -export:CreateImage" + .ascii " -export:LoadFile" + + .ascii " -export:Kmalloc" # + .ascii " -export:Kfree" # + + .ascii " -export:UserAlloc" # stdcall + .ascii " -export:UserFree" # stdcall + + .ascii " -export:MapIoMem" # stdcall + .ascii " -export:GetPgAddr" # eax + .ascii " -export:CreateObject" # + .ascii " -export:DestroyObject" # + .ascii " -export:CreateRingBuffer" # stdcall + .ascii " -export:CommitPages" # eax, ebx, ecx + + .ascii " -export:RegService" # stdcall + .ascii " -export:UnmapPages" # eax, ecx + .ascii " -export:SysMsgBoardStr" # + .ascii " -export:SetScreen" # + + + .ascii " -export:PciApi" # + .ascii " -export:PciRead8" # stdcall + .ascii " -export:PciRead16" # stdcall + .ascii " -export:PciRead32" # stdcall + .ascii " -export:PciWrite8" # stdcall + .ascii " -export:PciWrite16" # stdcall + .ascii " -export:PciWrite32" # stdcall + + .ascii " -export:SelectHwCursor" # stdcall + .ascii " -export:SetHwCursor" # stdcall + .ascii " -export:HwCursorRestore" # + .ascii " -export:HwCursorCreate" # + + + + diff --git a/kernel/branches/kolibri_pe/core/exports.inc b/kernel/branches/kolibri_pe/core/exports.inc index c88bb9fed8..e343becc35 100644 --- a/kernel/branches/kolibri_pe/core/exports.inc +++ b/kernel/branches/kolibri_pe/core/exports.inc @@ -110,7 +110,6 @@ kernel_export: dd szCommitPages , commit_pages ;not implemented dd szReleasePages , release_pages - dd szFreeKernelSpace , free_kernel_space ;stdcall dd szMemAlloc , @mem_alloc@8 ;fastcall dd szMemFree , @mem_free@4 ;fastcall dd szUserAlloc , user_alloc ;stdcall diff --git a/kernel/branches/kolibri_pe/core/heap.inc b/kernel/branches/kolibri_pe/core/heap.inc index 1a12b4ad6a..2c08edceac 100644 --- a/kernel/branches/kolibri_pe/core/heap.inc +++ b/kernel/branches/kolibri_pe/core/heap.inc @@ -15,12 +15,6 @@ USED_BLOCK equ 8 DONT_FREE_BLOCK equ 10h -align 4 -proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword - - ret -endp - ;;;;;;;;;;;;;; USER ;;;;;;;;;;;;;;;;; @@ -55,6 +49,7 @@ proc init_heap endp align 4 +_UserAlloc: proc user_alloc stdcall, alloc_size:dword push ebx @@ -131,6 +126,7 @@ m_exit: endp align 4 +_UserFree: proc user_free stdcall, base:dword push esi diff --git a/kernel/branches/kolibri_pe/core/malloc.inc b/kernel/branches/kolibri_pe/core/malloc.inc index 640b88f569..83e784e064 100644 --- a/kernel/branches/kolibri_pe/core/malloc.inc +++ b/kernel/branches/kolibri_pe/core/malloc.inc @@ -20,6 +20,9 @@ $Revision$ ; esi= nb ; ebx= idx ; + +align 4 +_Kmalloc: malloc: push esi @@ -197,6 +200,7 @@ malloc: ; eax= mem align 4 +_Kfree: free: push edi mov edi, eax diff --git a/kernel/branches/kolibri_pe/core/memory.inc b/kernel/branches/kolibri_pe/core/memory.inc index 20b30e7f39..a8f9813730 100644 --- a/kernel/branches/kolibri_pe/core/memory.inc +++ b/kernel/branches/kolibri_pe/core/memory.inc @@ -29,6 +29,8 @@ map_space: ;not implemented ret +align 4 +_MapIoMem: proc map_io_mem stdcall, base:dword, size:dword, flags:dword push edi @@ -71,6 +73,7 @@ endp ; ecx= count align 4 +_CommitPages: commit_pages: push edi test ecx, ecx @@ -136,6 +139,7 @@ release_pages: ; ecx= count align 4 +_UnmapPages: unmap_pages: push edi @@ -362,6 +366,7 @@ update_mem_size: ; eax= phisical page address align 4 +_GetPgAddr: get_pg_addr: shr eax, 12 mov eax, [page_tabs+eax*4] @@ -860,10 +865,11 @@ proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword .buffer_overflow: push 3 .ret: - mov eax, [used_buf] - cmp eax, [ipc_tmp] + mov ecx, [used_buf] + cmp ecx, [ipc_tmp] jz @f - stdcall free_kernel_space,eax + + call @mem_free@4 @@: pop eax popf @@ -1105,6 +1111,7 @@ proc stall stdcall, delay:dword endp align 4 +_CreateRingBuffer: proc create_ring_buffer stdcall, size:dword, flags:dword locals buf_ptr dd ? @@ -1151,7 +1158,8 @@ proc create_ring_buffer stdcall, size:dword, flags:dword pop ebx ret .mm_fail: - ;stdcall free_kernel_space, [buf_ptr] + mov ecx, [buf_ptr] + call @mem_free@4 pop ebx xor eax, eax .fail: diff --git a/kernel/branches/kolibri_pe/data32.inc b/kernel/branches/kolibri_pe/data32.inc index 0124138aec..1bf3ce1f96 100644 --- a/kernel/branches/kolibri_pe/data32.inc +++ b/kernel/branches/kolibri_pe/data32.inc @@ -345,37 +345,44 @@ free_blocks rd 1 mst MEM_STATE -page_start rd 1 -page_end rd 1 -events rd 1 -event_start rd 1 -event_end rd 1 -event_uid rd 1 -sys_page_map rd 1 +page_start rd 1 +page_end rd 1 +events rd 1 +event_start rd 1 +event_end rd 1 +event_uid rd 1 +sys_page_map rd 1 -srv.fd rd 1 -srv.bk rd 1 +srv.fd rd 1 +srv.bk rd 1 -scr_width rd 1 -scr_height rd 1 +scr_width rd 1 +scr_height rd 1 +_HwCursorCreate: create_cursor rd 1 -select_hw_cursor rd 1 -set_hw_cursor rd 1 -hw_restore rd 1 -def_cursor rd 1 +_SelectHwCursor: +select_hw_cursor rd 1 + +_SetHwCursor: +set_hw_cursor rd 1 + +_HwCursorRestore: +hw_restore rd 1 + +def_cursor rd 1 current_cursor rd 1 -hw_cursor rd 1 +hw_cursor rd 1 cur_def_interl rd 1 cur_saved_base rd 1 cur_saved_interl rd 1 -cur_saved_w rd 1 -cur_saved_h rd 1 +cur_saved_w rd 1 +cur_saved_h rd 1 -ipc_tmp rd 1 -ipc_pdir rd 1 -ipc_ptab rd 1 +ipc_tmp rd 1 +ipc_pdir rd 1 +ipc_ptab rd 1 proc_mem_map rd 1 proc_mem_pdir rd 1 diff --git a/kernel/branches/kolibri_pe/include/core.h b/kernel/branches/kolibri_pe/include/core.h index 977af087e6..bb9fac9713 100644 --- a/kernel/branches/kolibri_pe/include/core.h +++ b/kernel/branches/kolibri_pe/include/core.h @@ -1,6 +1,7 @@ -#define OS_BASE 0xE0000000 - +#define OS_BASE 0xE0000000 +#define IMAGE_BASE 0xE0100000 +#define LOAD_BASE 0x00100000 void printf (const char *format, ...); diff --git a/kernel/branches/kolibri_pe/include/mm.h b/kernel/branches/kolibri_pe/include/mm.h index 3327d5752e..ab5d424d0e 100644 --- a/kernel/branches/kolibri_pe/include/mm.h +++ b/kernel/branches/kolibri_pe/include/mm.h @@ -83,7 +83,11 @@ void __fastcall core_free(addr_t frame); pfn_t alloc_page() __attribute__ ((deprecated)); -md_t* __fastcall md_alloc(size_t size, u32_t flags); -void* __fastcall mem_alloc(size_t size, u32_t flags); -void __fastcall mem_free(void *mem); +#define __export __attribute__ ((dllexport)) + + +md_t* __fastcall md_alloc(size_t size, u32_t flags) ; + +void* __fastcall __export mem_alloc(size_t size, u32_t flags) asm ("MemAlloc"); +void __fastcall __export mem_free(void *mem) asm ("MemFree"); diff --git a/kernel/branches/kolibri_pe/include/types.h b/kernel/branches/kolibri_pe/include/types.h index 51b01ed4f0..c4e4154ff0 100644 --- a/kernel/branches/kolibri_pe/include/types.h +++ b/kernel/branches/kolibri_pe/include/types.h @@ -18,7 +18,7 @@ typedef u32_t eflags_t; typedef int bool; -#define true (bool)1 -#define false (bool)0 +#define true (bool)1 +#define false (bool)0 diff --git a/kernel/branches/kolibri_pe/kernel.asm b/kernel/branches/kolibri_pe/kernel.asm index 1db5203a56..98521e7fbf 100644 --- a/kernel/branches/kolibri_pe/kernel.asm +++ b/kernel/branches/kolibri_pe/kernel.asm @@ -131,10 +131,44 @@ public _rd_root_end public _load_file@4 -public _kernel_exports - public _strncmp@12 +public _LoadFile ; stdcall export + +public _CreateObject ; export +public _DestroyObject ; export + +public _CreateRingBuffer ; stdcall export +public _CommitPages ; export +public _GetPgAddr ; export +public _MapIoMem ; stdcall export +public _UnmapPages ; export + +public _Kmalloc ; export +public _Kfree ; export +public _UserAlloc ; stdcall export +public _UserFree ; stdcall export + +public _RegService ; stdcall export +public _SysMsgBoardStr ; export +public _SetScreen ; export FIXME make fastcall + + +public _PciApi ; export +public _PciRead8 ; stdcall export +public _PciRead16 ; stdcall export +public _PciRead32 ; stdcall export +public _PciWrite8 ; stdcall export +public _PciWrite16 ; stdcall export +public _PciWrite32 ; stdcall export + + +public _SelectHwCursor ; stdcall export +public _SetHwCursor ; stdcall export +public _HwCursorRestore ; export +public _HwCursorCreate ; export + + extrn __edata extrn _16bit_start @@ -152,8 +186,11 @@ extrn @core_free@4 extrn @init_heap@8 extrn @find_large_md@4 -extrn @mem_alloc@8 -extrn @mem_free@4 +extrn _MemAlloc +extrn _MemFree + +@mem_alloc@8 equ _MemAlloc +@mem_free@4 equ _MemFree extrn @load_pe@4 extrn @load_pe_driver@4 @@ -4545,6 +4582,8 @@ pic_delay: pdl1: ret +align 4 +_SysMsgBoardStr: sys_msg_board_str: pushad @@ -5060,7 +5099,9 @@ read_from_hd: ; Read from hd - fn not in use paleholder: ret + align 4 +_SetScreen: set_screen: cmp eax, [Screen_Max_X] jne .set diff --git a/kernel/branches/kolibri_pe/kernel32.inc b/kernel/branches/kolibri_pe/kernel32.inc index 66a6962191..1d1123af66 100644 --- a/kernel/branches/kolibri_pe/kernel32.inc +++ b/kernel/branches/kolibri_pe/kernel32.inc @@ -182,7 +182,6 @@ include "core/heap.inc" ; kernel and app heap include "core/malloc.inc" ; small kernel heap include "core/taskman.inc" include "core/dll.inc" -include "core/peload.inc" ; include "core/exports.inc" include "core/string.inc" include "core/v86.inc" ; virtual-8086 manager diff --git a/kernel/branches/kolibri_pe/ld.x b/kernel/branches/kolibri_pe/ld.x index a4bd738a4a..804f9ec9b0 100644 --- a/kernel/branches/kolibri_pe/ld.x +++ b/kernel/branches/kolibri_pe/ld.x @@ -20,6 +20,14 @@ SECTIONS { *(.flat) *(.text) *(.rdata) *(.data) } + + .edata ALIGN(32): + { + *(.edata) + _code_end = .; + . = ALIGN(4096); + } + __edata = . - 0xE0000000; .bss ALIGN(4096) : @@ -28,6 +36,8 @@ SECTIONS } __kernel_end = . - 0xE0000000; + + /DISCARD/ : { *(.debug$S) @@ -35,7 +45,6 @@ SECTIONS *(.debug$F) *(.drectve) *(.reloc) - *(.edata) } } diff --git a/kernel/branches/kolibri_pe/makefile b/kernel/branches/kolibri_pe/makefile index 878512faca..ad8f59b49a 100644 --- a/kernel/branches/kolibri_pe/makefile +++ b/kernel/branches/kolibri_pe/makefile @@ -35,13 +35,7 @@ PE_SRC:= \ boot/boot.asm \ boot/start.asm -#include -#include -#include -#include -#include -#include - + H_SRC:= \ include/types.h \ include/atomic.h \ @@ -60,8 +54,8 @@ all: kernel.gz kernel.gz :kernel.mnt 7z a -tgzip kernel.gz kernel.mnt -kernel.mnt: kernel.obj $(PE_OBJS) Makefile ld.x - ld $(LDFLAGS) -T ld.x -o $@ kernel.obj $(PE_OBJS) +kernel.mnt: kernel.obj bin/export.obj $(PE_OBJS) Makefile ld.x + ld $(LDFLAGS) -T ld.x -o $@ kernel.obj bin/export.obj $(PE_OBJS) bin/%.obj : core/%.c $(H_SRC) Makefile $(CC) $(CFLAGS) -o $@ $< @@ -69,6 +63,9 @@ bin/%.obj : core/%.c $(H_SRC) Makefile bin/%.obj: %.asm $(FASM) $< $@ +bin/export.obj: core/export.asm + as -o $@ $< + kernel.obj: $(KERNEL_SRC) $(FASM) kernel.asm