Subversion Repositories Kolibri OS

Rev

Rev 5195 | Rev 6767 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5195 Rev 6614
Line 35... Line 35...
35
exe_base        dd      ?
35
exe_base        dd      ?
36
stack_base      dd      ?
36
stack_base      dd      ?
37
stack_size      dd      ?
37
stack_size      dd      ?
38
exe_path        dd      ?
38
exe_path        dd      ?
39
command_line    dd      ?
39
command_line    dd      ?
-
 
40
environment     dd      ?
40
ends
41
ends
Line 41... Line 42...
41
 
42
 
-
 
43
include 'malloc.inc'
-
 
44
include 'peloader.inc'
Line 42... Line 45...
42
include 'malloc.inc'
45
include 'cmdline.inc'
43
 
46
 
44
proc syscall_int40
47
proc syscall_int40
45
        int     0x40
48
        int     0x40
Line -... Line 49...
-
 
49
        ret
-
 
50
endp
-
 
51
 
-
 
52
proc syscall_sysenter
-
 
53
        push    ebp
-
 
54
        mov     ebp, esp
-
 
55
        push    @f
-
 
56
        sysenter
-
 
57
@@:
-
 
58
        pop     edx
-
 
59
        pop     ecx
-
 
60
        ret
-
 
61
endp
-
 
62
 
-
 
63
proc syscall_syscall
-
 
64
        push    ecx
-
 
65
        syscall
-
 
66
        pop     ecx
46
        ret
67
        ret
47
endp
68
endp
48
 
69
 
Line 49... Line 70...
49
proc kercall
70
proc kercall
50
        jmp     FS_SYSCALL_PTR
71
        jmp     FS_SYSCALL_PTR
Line 51... Line 72...
51
endp
72
endp
-
 
73
 
-
 
74
prologue@proc equ fpo_prologue
-
 
75
epilogue@proc equ fpo_epilogue
-
 
76
 
52
 
77
proc start stdcall, dll_base, reason, reserved
53
prologue@proc equ fpo_prologue
78
locals
54
epilogue@proc equ fpo_epilogue
79
exe_base dd ?
-
 
80
exe_path_size dd ?
55
 
81
endl
56
proc start stdcall, dll_base, reason, reserved
82
; 1. Do nothing unless called by the kernel for DLL_PROCESS_ATTACH.
57
; 1. Do nothing unless called by the kernel for DLL_PROCESS_ATTACH.
83
        cmp     [reason], DLL_PROCESS_ATTACH
-
 
84
        jnz     .nothing
58
        cmp     [reason], DLL_PROCESS_ATTACH
85
; 2. Initialize process.
59
        jnz     .nothing
86
; 2a. Validate version of the init struct.
60
; 2. Validate version of the init struct.
87
; If not known, say a debug message and die.
61
; If not known, say a debug message and die.
-
 
62
        mov     ebp, [reserved]
88
        mov     ebp, [reserved]
63
        cmp     [ebp+kernel_init_data.version], 1
89
        mov     esi, [dll_base]
64
        jnz     .version_mismatch
-
 
65
; 3. Setup common data based on the init struct.
90
        cmp     [ebp+kernel_init_data.version], 1
66
        mov     eax, [ebp+kernel_init_data.stack_base]
91
        jnz     .version_mismatch
-
 
92
; 2b. Get the system call code.
-
 
93
; Note: relocations have not been fixed yet,
-
 
94
; so we cannot use absolute addresses, only RVAs.
-
 
95
        mov     eax, [ebp+kernel_init_data.syscall_method]
67
        mov     FS_STACK_MIN, eax
96
        cmp     eax, 0x10000
68
        add     eax, [ebp+kernel_init_data.stack_size]
97
        jae     .syscall_absolute
69
        mov     FS_STACK_MAX, eax
98
        dec     eax
-
 
99
        mov     edx, rva syscall_int40
-
 
100
        cmp     eax, num_syscall_methods
70
        mov     eax, [ebp+kernel_init_data.syscall_method]
101
        jae     @f
-
 
102
        mov     edx, [esi+eax*4+rva syscall_methods]
-
 
103
@@:
-
 
104
        lea     eax, [edx+esi]
-
 
105
.syscall_absolute:
-
 
106
        mov     FS_SYSCALL_PTR, eax
-
 
107
; 2c. Fixup relocations so that we can use absolute offsets instead of RVAs
-
 
108
; in rest of code.
-
 
109
; Note: this uses syscalls, so this step should be done after
-
 
110
; configuring FS_SYSCALL_PTR at step 2b.
-
 
111
        push    kolibri_dll
-
 
112
        call    fixup_pe_relocations
-
 
113
        pop     ecx
-
 
114
        jc      .die
-
 
115
; 2d. Allocate process data.
71
        cmp     eax, 0x10000
116
        mov     eax, 68
72
        jae     @f
117
        mov     ebx, 12
-
 
118
        mov     ecx, 0x1000
73
        mov     eax, syscall_int40
119
        call    FS_SYSCALL_PTR
74
@@:
120
        mov     FS_PROCESS_DATA, eax
75
        mov     FS_SYSCALL_PTR, eax
121
; 2e. Initialize process heap.
76
; 4. Initialize the process heap.
122
        mov     eax, [ebp+kernel_init_data.exe_base]
77
        mov     eax, [ebp+kernel_init_data.exe_base]
123
        mov     [exe_base], eax
78
        mov     edx, [eax+STRIPPED_PE_HEADER.SizeOfHeapReserve]
124
        mov     edx, [eax+STRIPPED_PE_HEADER.SizeOfHeapReserve]
79
        cmp     word [eax], 'MZ'
125
        cmp     word [eax], 'MZ'
-
 
126
        jnz     @f
-
 
127
        add     eax, [eax+IMAGE_DOS_HEADER.e_lfanew]
-
 
128
        mov     edx, [eax+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve]
-
 
129
@@:
-
 
130
        malloc_init
-
 
131
; 2f. Copy rest of init struct and free memory.
-
 
132
; Parse command line to argc/argv here and move arguments to the heap
-
 
133
; in order to save memory: init struct and heap use different pages,
-
 
134
; but typically data from init struct are far from the entire page,
-
 
135
; so moving it to heap does not increase actual physical heap size
-
 
136
; and allows to free init struct.
-
 
137
        mov     eax, [ebp+kernel_init_data.stack_base]
-
 
138
        mov     FS_STACK_MIN, eax
-
 
139
        add     eax, [ebp+kernel_init_data.stack_size]
-
 
140
        mov     FS_STACK_MAX, eax
-
 
141
        mov     eax, [ebp+kernel_init_data.exe_path]
-
 
142
@@:
-
 
143
        inc     eax
-
 
144
        cmp     byte [eax-1], 0
-
 
145
        jnz     @b
-
 
146
        sub     eax, [ebp+kernel_init_data.exe_path]
-
 
147
        mov     [exe_path_size], eax
-
 
148
        mov     esi, [ebp+kernel_init_data.command_line]
80
        jnz     @f
149
        xor     edx, edx
-
 
150
        xor     edi, edi
-
 
151
        call    parse_cmdline
-
 
152
        inc     ebx ; argv[0] = exe path
-
 
153
.argc equ dll_base
-
 
154
.argv equ reason
-
 
155
.envp equ reserved
-
 
156
        mov     [.argc], ebx
81
        add     eax, [eax+IMAGE_DOS_HEADER.e_lfanew]
157
        sub     esi, [ebp+kernel_init_data.command_line]
-
 
158
        lea     esi, [esi+(ebx+1)*4]
82
        mov     edx, [eax+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve]
159
        add     esi, [exe_path_size]
-
 
160
        stdcall malloc, esi
-
 
161
        mov     [.argv], eax
-
 
162
        mov     edx, eax
-
 
163
        lea     edi, [eax+ebx*4]
-
 
164
        mov     esi, [ebp+kernel_init_data.exe_path]
-
 
165
        mov     [edx], edi
-
 
166
        add     edx, 4
-
 
167
        mov     ecx, [exe_path_size]
-
 
168
        rep movsb
-
 
169
        mov     esi, [ebp+kernel_init_data.command_line]
-
 
170
        call    parse_cmdline
-
 
171
        and     dword [edx], 0 ; argv[argc] = NULL
-
 
172
        and     [.envp], 0
-
 
173
        mov     eax, 68
-
 
174
        mov     ebx, 13
-
 
175
        mov     ecx, ebp
-
 
176
        call    FS_SYSCALL_PTR
-
 
177
; 3. Configure modules: main EXE and possible statically linked DLLs.
-
 
178
        mov     esi, [exe_base]
-
 
179
        mov     eax, [.argv]
83
@@:
180
        pushd   [eax]
84
        malloc_init
181
        call    fixup_pe_relocations
85
; ...TBD...
182
        pop     ecx
86
; Call exe entry point.
183
        jc      .die
87
        mov     eax, [ebp+kernel_init_data.exe_base]
184
; 4. Call exe entry point.
88
        mov     edx, [eax+STRIPPED_PE_HEADER.AddressOfEntryPoint]
185
        mov     edx, [esi+STRIPPED_PE_HEADER.AddressOfEntryPoint]
89
        cmp     word [eax], 'MZ'
186
        cmp     word [esi], 'MZ'
90
        jnz     @f
187
        jnz     @f
-
 
188
        mov     ecx, [esi+IMAGE_DOS_HEADER.e_lfanew]
91
        mov     ecx, [eax+IMAGE_DOS_HEADER.e_lfanew]
189
        add     ecx, esi
92
        add     ecx, eax
190
        mov     edx, [ecx+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
93
        mov     edx, [ecx+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
191
@@:
94
@@:
-
 
95
        add     edx, eax
192
        add     edx, esi
-
 
193
        add     esp, fpo_localsize+4
-
 
194
        call    edx
96
        call    edx
195
; If exe entry point has returned control, die.
-
 
196
        jmp     .die
-
 
197
.version_mismatch:
97
; If exe entry point has returned control, die.
198
        lea     eax, [esi + rva syscall_int40]
-
 
199
        mov     FS_SYSCALL_PTR, eax
-
 
200
        add     esi, rva msg_version_mismatch
-
 
201
        call    sys_msg_board_str
-
 
202
.die:
-
 
203
        or      eax, -1
-
 
204
        call    FS_SYSCALL_PTR
98
        mov     eax, -1
205
.nothing:
99
        call    FS_SYSCALL_PTR
206
        ret
-
 
207
endp
100
.version_mismatch:
208
 
101
        mov     esi, version_mismatch_msg
209
proc sys_msg_board_str
102
        mov     eax, 63
210
        push    eax ebx
-
 
211
@@:
-
 
212
        push    ecx
103
        mov     ebx, 1
213
        mov     cl, [ecx]
-
 
214
        test    cl, cl
104
@@:
215
        jz      @f
105
        mov     cl, [esi]
216
        mov     eax, 63
106
        test    cl, cl
217
        mov     ebx, 1
107
        jz      @f
218
        call    FS_SYSCALL_PTR
108
        int     0x40    ; can't use FS_SYSCALL_PTR here, it has not yet been set
-
 
109
        inc     esi
-
 
110
        jmp     @b
219
        pop     ecx
111
@@:
220
        inc     ecx
Line 112... Line 221...
112
        mov     eax, -1
221
        jmp     @b
-
 
222
@@:
-
 
223
        pop     ecx ebx eax
-
 
224
        ret
-
 
225
endp
113
        int     0x40
226
 
114
.nothing:
227
align 4
115
        ret
228
syscall_methods dd rva syscall_int40, rva syscall_sysenter, rva syscall_syscall
116
endp
229
num_syscall_methods = ($ - syscall_methods) / 4
117
 
230
 
Line 134... Line 247...
134
        , mspace_realloc_in_place, 'mspace_realloc_in_place' \
247
        , mspace_realloc_in_place, 'mspace_realloc_in_place' \
135
        , mspace_memalign, 'mspace_memalign' \
248
        , mspace_memalign, 'mspace_memalign' \
Line 136... Line 249...
136
 
249
 
Line -... Line 250...
-
 
250
end data
-
 
251
 
137
end data
252
kolibri_dll             db      'kolibri.dll',0
-
 
253
 
-
 
254
msg_version_mismatch    db      'S : Version mismatch between kernel and kolibri.dll',13,10,0
-
 
255
msg_bad_relocation1     db      'S : Bad relocation type in ',0
-
 
256
msg_newline             db      13,10,0
Line 138... Line 257...
138
 
257
msg_relocated1          db      'S : fixups for ',0
139
version_mismatch_msg    db      'Version mismatch between kernel and kolibri.dll',13,10,0
258
msg_relocated2          db      ' applied',13,10,0
140
 
259
 
141
if FOOTERS
260
if FOOTERS