PE loader. "C" version

git-svn-id: svn://kolibrios.org@889 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Sergey Semyonov (Serge) 2008-10-30 06:30:13 +00:00
parent b7083f5742
commit cb4549795b
14 changed files with 265 additions and 511 deletions

View File

@ -562,6 +562,7 @@ endp
; loaded by the load_file() function ; loaded by the load_file() function
align 4 align 4
_load_file@4:
proc load_file stdcall, file_name:dword proc load_file stdcall, file_name:dword
locals locals
attr dd ? attr dd ?

View File

@ -8,15 +8,6 @@
#define page_tabs 0xDF800000 #define page_tabs 0xDF800000
typedef struct
{
link_t link;
link_t adj;
addr_t base;
size_t size;
void *parent;
u32_t state;
}md_t;
#define MD_FREE 1 #define MD_FREE 1
#define MD_USED 2 #define MD_USED 2
@ -83,7 +74,6 @@ int __fastcall init_heap(addr_t base, size_t size)
list_initialize(&lheap.used); list_initialize(&lheap.used);
list_initialize(&sheap.used); list_initialize(&sheap.used);
md_slab = slab_cache_create(sizeof(md_t), 32,NULL,NULL,SLAB_CACHE_MAGDEFERRED); md_slab = slab_cache_create(sizeof(md_t), 32,NULL,NULL,SLAB_CACHE_MAGDEFERRED);
md = (md_t*)slab_alloc(md_slab,0); md = (md_t*)slab_alloc(md_slab,0);
@ -622,16 +612,12 @@ void __fastcall free_mapped_md(md_t *md)
}; };
void * __fastcall mem_alloc(size_t size, u32_t flags) md_t* __fastcall md_alloc(size_t size, u32_t flags)
{ {
eflags_t efl; eflags_t efl;
md_t *md; md_t *md;
DBG("\nmem_alloc: %x bytes\n", size);
ASSERT(size != 0);
size = (size+4095)&~4095; size = (size+4095)&~4095;
if( flags & PG_MAP ) if( flags & PG_MAP )
@ -641,6 +627,9 @@ void * __fastcall mem_alloc(size_t size, u32_t flags)
if( !md ) if( !md )
return NULL; return NULL;
ASSERT(md->state == MD_USED);
ASSERT(md->parent != NULL);
md_t *lmd = (md_t*)md->parent; md_t *lmd = (md_t*)md->parent;
ASSERT( lmd != NULL); ASSERT( lmd != NULL);
@ -661,14 +650,33 @@ void * __fastcall mem_alloc(size_t size, u32_t flags)
}; };
} }
else else
{
md = find_unmapped_md(size); md = find_unmapped_md(size);
if( !md ) if( !md )
return NULL; return NULL;
ASSERT(md->parent != NULL); ASSERT(md->parent != NULL);
ASSERT(md->state == MD_USED); ASSERT(md->state == MD_USED);
}
return md;
};
void * __fastcall mem_alloc(size_t size, u32_t flags)
{
eflags_t efl;
md_t *md;
DBG("\nmem_alloc: %x bytes\n", size);
ASSERT(size != 0);
md = md_alloc(size, flags);
if( !md )
return NULL;
efl = safe_cli(); efl = safe_cli();
spinlock_lock(&sheap.lock); spinlock_lock(&sheap.lock);

View File

@ -146,6 +146,7 @@ proc user_free stdcall, base:dword
mov eax, [page_tabs+(esi-1)*4] mov eax, [page_tabs+(esi-1)*4]
test al, USED_BLOCK test al, USED_BLOCK
jz .cantfree jz .cantfree
test al, DONT_FREE_BLOCK test al, DONT_FREE_BLOCK
jnz .cantfree jnz .cantfree
@ -159,6 +160,7 @@ proc user_free stdcall, base:dword
mov ebx, edi mov ebx, edi
shr edi, 12 shr edi, 12
jz .released jz .released
.release: .release:
xor ecx, ecx xor ecx, ecx
xchg ecx, [page_tabs+esi*4] xchg ecx, [page_tabs+esi*4]

View File

@ -243,6 +243,7 @@ proc new_mem_resize stdcall, new_size:dword
mov ecx, [app_page_tabs+edi*4] mov ecx, [app_page_tabs+edi*4]
test ecx, 1 test ecx, 1
jz .next jz .next
mov dword [app_page_tabs+edi*4], 2 mov dword [app_page_tabs+edi*4], 2
mov ebx, edi mov ebx, edi
shl ebx, 12 shl ebx, 12
@ -986,50 +987,16 @@ new_services:
cmp ebx, OS_BASE cmp ebx, OS_BASE
jae .fail jae .fail
stdcall load_PE, ebx mov ecx, ebx
call @load_pe_driver@4
test eax, eax
jz @F
mov esi, eax
stdcall eax, DRV_ENTRY
test eax, eax
jz @F
mov [eax+SRV.entry], esi
@@:
mov [esp+36], eax mov [esp+36], eax
ret ret
.fail: .fail:
xor eax, eax xor eax, eax
mov [esp+36], eax mov [esp+36], eax
ret ret
align 4
proc load_pe_driver stdcall, file:dword
stdcall load_PE, [file]
test eax, eax
jz .fail
mov esi, eax
stdcall eax, DRV_ENTRY
test eax, eax
jz .fail
mov [eax+SRV.entry], esi
ret
.fail:
xor eax, eax
ret
endp
align 4 align 4
proc init_mtrr proc init_mtrr

View File

@ -387,12 +387,10 @@ static void __fastcall buddy_system_free(zone_t *z, link_t *block)
return; return;
} }
} }
/* /*
* Insert block into the list of order i. * Insert block into the list of order i.
*/ */
list_append(block, &z->order[i]); list_append(block, &z->order[i]);
} }
static inline frame_t * zone_get_frame(zone_t *zone, index_t frame_idx) static inline frame_t * zone_get_frame(zone_t *zone, index_t frame_idx)

View File

@ -11,277 +11,9 @@ include 'export.inc'
align 4 align 4
proc load_PE stdcall, file_name:dword
locals
image dd ?
entry dd ?
base dd ?
endl
stdcall load_file, [file_name]
test eax, eax
jz .fail
mov [image], eax
mov ebx, [eax+60]
mov ecx, [eax+80+edx]
mov edx, PG_SW
call @mem_alloc@8
test eax, eax
mov [base], eax
jz .cleanup
stdcall map_PE, eax, [image]
mov [entry], eax
test eax, eax
jnz .cleanup
mov ecx, [base]
call @mem_free@4
.cleanup:
mov ecx, [image]
call @mem_free@4
mov eax, [entry]
ret
.fail:
xor eax, eax
ret
endp
DWORD equ dword
PTR equ
align 4
map_PE: ;stdcall base:dword, image:dword
cld
push ebp
push edi
push esi
push ebx
sub esp, 60
mov ebx, DWORD PTR [esp+84]
mov ebp, DWORD PTR [esp+80]
mov edx, ebx
mov esi, ebx
add edx, DWORD PTR [ebx+60]
mov edi, ebp
mov DWORD PTR [esp+32], edx
mov ecx, DWORD PTR [edx+84]
shr ecx, 2
rep movsd
movzx eax, WORD PTR [edx+6]
mov DWORD PTR [esp+36], 0
mov DWORD PTR [esp+16], eax
jmp L2
L3:
mov eax, DWORD PTR [edx+264]
test eax, eax
je L4
mov esi, ebx
mov edi, ebp
add esi, DWORD PTR [edx+268]
mov ecx, eax
add edi, DWORD PTR [edx+260]
shr ecx, 2
rep movsd
L4:
mov ecx, DWORD PTR [edx+256]
add ecx, 4095
and ecx, -4096
cmp ecx, eax
jbe L6
sub ecx, eax
add eax, DWORD PTR [edx+260]
lea edi, [eax+ebp]
xor eax, eax
rep stosb
L6:
inc DWORD PTR [esp+36]
add edx, 40
L2:
mov esi, DWORD PTR [esp+16]
cmp DWORD PTR [esp+36], esi
jne L3
mov edi, DWORD PTR [esp+32]
cmp DWORD PTR [edi+164], 0
je L9
mov esi, ebp
mov ecx, ebp
sub esi, DWORD PTR [edi+52]
add ecx, DWORD PTR [edi+160]
mov eax, esi
shr eax, 16
mov DWORD PTR [esp+12], eax
jmp L11
L12:
lea ebx, [eax-8]
xor edi, edi
shr ebx,1
jmp L13
L14:
movzx eax, WORD PTR [ecx+8+edi*2]
mov edx, eax
shr eax, 12
and edx, 4095
add edx, DWORD PTR [ecx]
cmp ax, 2
je L17
cmp ax, 3
je L18
dec ax
jne L15
mov eax, DWORD PTR [esp+12]
add WORD PTR [edx+ebp], ax
L17:
add WORD PTR [edx+ebp], si
L18:
add DWORD PTR [edx+ebp], esi
L15:
inc edi
L13:
cmp edi, ebx
jne L14
add ecx, DWORD PTR [ecx+4]
L11:
mov eax, DWORD PTR [ecx+4]
test eax, eax
jne L12
L9:
mov edx, DWORD PTR [esp+32]
cmp DWORD PTR [edx+132], 0
je L20
mov eax, ebp
add eax, DWORD PTR [edx+128]
mov DWORD PTR [esp+40], 0
add eax, 20
mov DWORD PTR [esp+56], eax
L22:
mov ecx, DWORD PTR [esp+56]
cmp DWORD PTR [ecx-16], 0
jne L23
cmp DWORD PTR [ecx-8], 0
je L25
L23:
mov edi, DWORD PTR [__exports+32]
mov esi, DWORD PTR [__exports+28]
mov eax, DWORD PTR [esp+56]
mov DWORD PTR [esp+20], edi
add edi, OS_BASE
add esi, OS_BASE
mov DWORD PTR [esp+44], esi
mov ecx, DWORD PTR [eax-4]
mov DWORD PTR [esp+48], edi
mov edx, DWORD PTR [eax-20]
mov DWORD PTR [esp+52], 0
add ecx, ebp
add edx, ebp
mov DWORD PTR [esp+24], edx
mov DWORD PTR [esp+28], ecx
L26:
mov esi, DWORD PTR [esp+52]
mov edi, DWORD PTR [esp+24]
mov eax, DWORD PTR [edi+esi*4]
test eax, eax
je L27
test eax, eax
js L27
lea edi, [ebp+eax]
mov eax, DWORD PTR [esp+28]
mov DWORD PTR [eax+esi*4], 0
lea esi, [edi+2]
push eax
push 32
movzx eax, WORD PTR [edi]
mov edx, DWORD PTR [esp+56]
mov eax, DWORD PTR [edx+eax*4]
add eax, OS_BASE
push eax
push esi
call strncmp
pop ebx
xor ebx, ebx
test eax, eax
jne L32
jmp L30
L33:
push ecx
push 32
mov ecx, DWORD PTR [esp+28]
mov eax, DWORD PTR [ecx+OS_BASE+ebx*4]
add eax, OS_BASE
push eax
push esi
call strncmp
pop edx
test eax, eax
jne L34
mov esi, DWORD PTR [esp+44]
mov edx, DWORD PTR [esp+52]
mov ecx, DWORD PTR [esp+28]
mov eax, DWORD PTR [esi+ebx*4]
add eax, OS_BASE
mov DWORD PTR [ecx+edx*4], eax
jmp L36
L34:
inc ebx
L32:
cmp ebx, DWORD PTR [__exports+24]
jb L33
L36:
cmp ebx, DWORD PTR [__exports+24]
jne L37
mov esi, msg_unresolved
call sys_msg_board_str
lea esi, [edi+2]
call sys_msg_board_str
mov esi, msg_CR
call sys_msg_board_str
mov DWORD PTR [esp+40], 1
jmp L37
L30:
movzx eax, WORD PTR [edi]
mov esi, DWORD PTR [esp+44]
mov edi, DWORD PTR [esp+52]
mov edx, DWORD PTR [esp+28]
mov eax, DWORD PTR [esi+eax*4]
add eax, OS_BASE
mov DWORD PTR [edx+edi*4], eax
L37:
inc DWORD PTR [esp+52]
jmp L26
L27:
add DWORD PTR [esp+56], 20
jmp L22
L25:
xor eax, eax
cmp DWORD PTR [esp+40], 0
jne L40
L20:
mov ecx, DWORD PTR [esp+32]
mov eax, ebp
add eax, DWORD PTR [ecx+40]
L40:
add esp, 60
pop ebx
pop esi
pop edi
pop ebp
ret 8
align 16 align 16
__exports: _kernel_exports:
export 'KERNEL', \ export 'KERNEL', \
commit_pages, 'CommitPages', \ ; eax, ebx, ecx commit_pages, 'CommitPages', \ ; eax, ebx, ecx
create_kernel_object, 'CreateObject', \ create_kernel_object, 'CreateObject', \
@ -292,6 +24,8 @@ __exports:
free, 'Kfree', \ free, 'Kfree', \
map_io_mem, 'MapIoMem', \ ; stdcall map_io_mem, 'MapIoMem', \ ; stdcall
get_pg_addr, 'GetPgAddr', \ ; eax get_pg_addr, 'GetPgAddr', \ ; eax
@mem_alloc@8, 'mem_alloc', \ ; fastcall
@mem_free@4, 'mem_free', \ ; fastcall
\ \
select_hw_cursor, 'SelectHwCursor', \ ; stdcall select_hw_cursor, 'SelectHwCursor', \ ; stdcall
set_hw_cursor, 'SetHwCursor', \ ; stdcall set_hw_cursor, 'SetHwCursor', \ ; stdcall

View File

@ -57,6 +57,7 @@ proc strncat stdcall, s1:dword, s2:dword, n:dword
endp endp
align 4 align 4
_strncmp@12:
proc strncmp stdcall, s1:dword, s2:dword, n:dword proc strncmp stdcall, s1:dword, s2:dword, n:dword
push esi push esi

View File

@ -640,11 +640,13 @@ term9:
mov ecx, [edi+APPDATA.io_map] mov ecx, [edi+APPDATA.io_map]
cmp ecx, (tss._io_map_0-OS_BASE+PG_MAP) cmp ecx, (tss._io_map_0-OS_BASE+PG_MAP)
je @F je @F
call @core_free@4 call @core_free@4
@@: @@:
mov ecx, [edi+APPDATA.io_map+4] mov ecx, [edi+APPDATA.io_map+4]
cmp ecx, (tss._io_map_1-OS_BASE+PG_MAP) cmp ecx, (tss._io_map_1-OS_BASE+PG_MAP)
je @F je @F
call @core_free@4 call @core_free@4
@@: @@:
mov eax, 0x20202020 mov eax, 0x20202020

View File

@ -416,7 +416,6 @@ proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword
jnz @B jnz @B
mov edi, page_tabs mov edi, page_tabs
mov ecx, [app_tabs] mov ecx, [app_tabs]
shl ecx, 10 shl ecx, 10
xor eax, eax xor eax, eax
@ -624,7 +623,6 @@ check_process_region:
mov eax,1 mov eax,1
ret ret
; call MEM_Get_Linear_Address ; call MEM_Get_Linear_Address
; push ebx ; push ebx
; push ecx ; push ecx

View File

@ -92,7 +92,7 @@ static inline void _bts(u32_t *data, count_t val)
); );
} }
static inline void _btr(u32_t *data, count_t val) extern inline void _btr(u32_t *data, count_t val)
{ {
asm volatile ("btr %0, %1 \n\t" asm volatile ("btr %0, %1 \n\t"
: :
@ -100,3 +100,22 @@ static inline void _btr(u32_t *data, count_t val)
:"cc" :"cc"
); );
} }
extern inline void* load_file(const char *path, size_t *size)
{
void* retval;
size_t tmp;
__asm__ __volatile__ (
"pushl %%eax \n\t"
"call _load_file@4 \n\t"
:"=eax" (retval), "=ebx"(tmp)
:"a" (path) );
if(size)
*size = tmp;
return retval;
};
//extern __fastcall void* load_file(const char *path, size_t *size);

View File

@ -22,14 +22,25 @@ typedef struct {
int flags; int flags;
} zone_t; } zone_t;
typedef struct typedef struct
{ {
count_t count; link_t link;
addr_t frames[18]; link_t adj;
}phismem_t; addr_t base;
size_t size;
void *parent;
u32_t state;
}md_t;
#define PG_MAP 1 #define PG_MAP 1
#define PG_WRITE 2
#define PG_USER 4
#define PG_SW 3
#define PG_UW 7
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
@ -60,15 +71,19 @@ static inline pfn_t ADDR2PFN(addr_t addr)
void init_mm(); void init_mm();
void* __fastcall frame_get_parent(pfn_t pfn);
void __fastcall frame_set_parent(pfn_t pfn, void *data);
void frame_free(pfn_t frame);
addr_t __fastcall core_alloc(u32_t order); addr_t __fastcall core_alloc(u32_t order);
void __fastcall core_free(addr_t frame); void __fastcall core_free(addr_t frame);
pfn_t alloc_page() __attribute__ ((deprecated)); pfn_t alloc_page() __attribute__ ((deprecated));
pfn_t __stdcall alloc_pages(count_t count) __asm__ ("_alloc_pages") __attribute__ ((deprecated));
void frame_free(pfn_t frame);
void __fastcall frame_set_parent(pfn_t pfn, void *data);
void* __fastcall frame_get_parent(pfn_t pfn);
md_t* __fastcall md_alloc(size_t size, u32_t flags);
void* __fastcall mem_alloc(size_t size, u32_t flags); void* __fastcall mem_alloc(size_t size, u32_t flags);
void __fastcall mem_free(void *mem);

View File

@ -129,6 +129,11 @@ public _rd_fat_end
public _rd_root public _rd_root
public _rd_root_end public _rd_root_end
public _load_file@4
public _kernel_exports
public _strncmp@12
extrn __edata extrn __edata
@ -146,11 +151,13 @@ extrn @core_free@4
extrn @init_heap@8 extrn @init_heap@8
extrn @find_large_md@4 extrn @find_large_md@4
extrn @phis_alloc@4
extrn @mem_alloc@8 extrn @mem_alloc@8
extrn @mem_free@4 extrn @mem_free@4
extrn @load_pe@4
extrn @load_pe_driver@4
extrn _slab_cache_init extrn _slab_cache_init
extrn _alloc_page extrn _alloc_page
@ -779,7 +786,8 @@ include 'detect/disks.inc'
mov [SLOT_BASE+APPDATA.cursor],eax mov [SLOT_BASE+APPDATA.cursor],eax
mov [SLOT_BASE+APPDATA.cursor+256],eax mov [SLOT_BASE+APPDATA.cursor+256],eax
; stdcall load_pe_driver, szAtiHW ;mov ecx, szAtiHW
;call @load_pe_driver@4
; READ TSC / SECOND ; READ TSC / SECOND

View File

@ -6,7 +6,7 @@ INCLUDE = include/
DEFS = -DUSE_SMP -DCONFIG_DEBUG DEFS = -DUSE_SMP -DCONFIG_DEBUG
CFLAGS = -c -O2 -I $(INCLUDE) -fomit-frame-pointer -fno-builtin-printf CFLAGS = -c -O2 -I $(INCLUDE) -fomit-frame-pointer -fno-builtin
LDFLAGS = -shared -s -Map kernel.map --image-base 0x100000 --file-alignment 32 LDFLAGS = -shared -s -Map kernel.map --image-base 0x100000 --file-alignment 32
KERNEL_SRC:= \ KERNEL_SRC:= \
@ -30,6 +30,7 @@ PE_SRC:= \
mm.c \ mm.c \
slab.c \ slab.c \
heap.c \ heap.c \
dll.c \
spinlock.c \ spinlock.c \
boot/boot.asm \ boot/boot.asm \
boot/start.asm boot/start.asm