Subversion Repositories Kolibri OS

Rev

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

Rev 892 Rev 897
Line 7... Line 7...
7
#include 
7
#include 
8
 
8
 
Line 9... Line 9...
9
int __stdcall strncmp(const char *s1, const char *s2, size_t n);
9
int __stdcall strncmp(const char *s1, const char *s2, size_t n);
Line -... Line 10...
-
 
10
 
-
 
11
extern int __stdcall mnt_exec(void *raw, size_t raw_size, char *path,
-
 
12
              char *cmdline, u32_t flags) asm ("mnt_exec");
10
 
13
 
Line 11... Line 14...
11
static dll_t core_dll;
14
static dll_t core_dll;
12
 
15
 
13
static char* strupr(char *str )
16
static char* strupr(char *str )
Line 25... Line 28...
25
 
28
 
Line 26... Line 29...
26
    return( str );
29
    return( str );
27
}
30
}
Line -... Line 31...
-
 
31
 
-
 
32
void * memcpy(void * _dest, const void *_src, size_t _n)
-
 
33
{
-
 
34
int d0, d1, d2;
-
 
35
 __asm__ __volatile__(
-
 
36
	"rep ; movsl\n\t"
-
 
37
	"testb $2,%b4\n\t"
-
 
38
	"je 1f\n\t"
-
 
39
	"movsw\n"
-
 
40
	"1:\ttestb $1,%b4\n\t"
-
 
41
	"je 2f\n\t"
-
 
42
	"movsb\n"
-
 
43
	"2:"
-
 
44
	: "=&c" (d0), "=&D" (d1), "=&S" (d2)
-
 
45
	:"0" (_n/4), "q" (_n),"1" ((long)_dest),"2" ((long)_src)
-
 
46
	: "memory");
-
 
47
 return (_dest);
-
 
48
}
-
 
49
 
-
 
50
size_t strlen(const char *str)
-
 
51
{
-
 
52
int d0;
-
 
53
register int __res;
-
 
54
__asm__ __volatile__(
-
 
55
	"repne\n\t"
-
 
56
	"scasb\n\t"
-
 
57
	"notl %0\n\t"
-
 
58
	"decl %0"
-
 
59
	:"=c" (__res), "=&D" (d0) :"1" (str),"a" (0), "0" (0xffffffff));
-
 
60
return __res;
-
 
61
}
28
 
62
 
29
void init_core_dll()
63
void init_core_dll()
30
{
64
{
31
    PIMAGE_DOS_HEADER        dos;
65
    PIMAGE_DOS_HEADER        dos;
32
    PIMAGE_NT_HEADERS32      nt;
66
    PIMAGE_NT_HEADERS32      nt;
Line 122... Line 156...
122
        return NULL;
156
        return NULL;
123
    }
157
    }
124
}
158
}
125
159
 
Line -... Line 160...
-
 
160
typedef struct
-
 
161
{
-
 
162
    int a_type;
-
 
163
    union
-
 
164
    {
-
 
165
        long  a_val;
-
 
166
        void *a_ptr;
-
 
167
        void  (*a_fcn)( ) ;
-
 
168
    }a_un;
-
 
169
}auxv_t;
-
 
170
 
-
 
171
#define AUX_COUNT       0
-
 
172
 
-
 
173
typedef struct
-
 
174
{
-
 
175
    int     argc;       /*  always 2                                */
-
 
176
    char   *path;       /*  argv[0]   program path                  */
-
 
177
    char   *cmdline;    /*  argv[1]   command  line. May be null    */
-
 
178
    u32_t   sep1;       /*  separator.  must be zero                */
-
 
179
    char   *env;        /*  single environment string               */
-
 
180
    u32_t   sep2;       /*  separator.  must be zero                */
-
 
181
    auxv_t  aux[1];     /*  aux. AT_NULL for now                    */
-
 
182
}exec_stack_t;
-
 
183
 
-
 
184
 
-
 
185
addr_t new_app_space(void);
-
 
186
 
-
 
187
int __stdcall pe_app_param(char *path, void *raw, addr_t ex_pg_dir,
-
 
188
                          addr_t ex_stack_page) asm ("pe_app_param");
-
 
189
 
-
 
190
int sys_exec(char *path, char *cmdline, u32_t flags)
-
 
191
{
-
 
192
    addr_t        ex_pg_dir;
-
 
193
    addr_t        ex_stack_tab;
-
 
194
    addr_t        ex_stack_page;
-
 
195
    addr_t        ex_pl0_stack;
-
 
196
 
-
 
197
    exec_stack_t *ex_stack;
-
 
198
    int           stack_size;
-
 
199
    char         *ex_path;
-
 
200
    char         *ex_cmdline = NULL;
-
 
201
 
-
 
202
    size_t        raw_size;
-
 
203
    u32_t        *raw;
-
 
204
 
-
 
205
    int pathsize = 0;
-
 
206
    int cmdsize  = 0;
-
 
207
    int envsize  = 0;
-
 
208
 
-
 
209
    u32_t tmp;
-
 
210
 
-
 
211
    DBG("\nexec %s cmd %s flags %x\n", path, cmdline, flags);
-
 
212
 
-
 
213
    if( ! path)
-
 
214
    {
-
 
215
        DBG("invalid path\n");
-
 
216
        return;
-
 
217
    };
-
 
218
 
-
 
219
    raw = load_file(path, &raw_size);
-
 
220
 
-
 
221
    if( ! raw )
-
 
222
        return -5;                                      /* FIXME */
-
 
223
 
-
 
224
    if( (raw[0] == 0x554E454D) &&
-
 
225
        ( ( raw[1] == 0x31305445) ||
-
 
226
          ( raw[1] == 0x30305445) ) )
-
 
227
 
-
 
228
    {
-
 
229
        DBG("leagacy Kolibri application\n");
-
 
230
        int tmp =  mnt_exec(raw, raw_size, path, cmdline, flags);
-
 
231
        return tmp;
-
 
232
    }
-
 
233
 
-
 
234
    if( ! validate_pe(raw, raw_size) )
-
 
235
    {
-
 
236
        DBG("invalid executable file %s\n", path);
-
 
237
        mem_free(raw);
-
 
238
        return -31;
-
 
239
    }
-
 
240
 
-
 
241
    pathsize = strlen(path)+1;
-
 
242
 
-
 
243
    if( cmdline )
-
 
244
        cmdsize = strlen(cmdline)+1;
-
 
245
 
-
 
246
    stack_size = sizeof(exec_stack_t) + pathsize +
-
 
247
                 cmdsize + envsize + AUX_COUNT*sizeof(auxv_t);
-
 
248
 
-
 
249
    stack_size = (stack_size + 15) & ~15;               /* keep stack aligned */
-
 
250
 
-
 
251
    DBG("stacksize %d\n", stack_size);
-
 
252
 
-
 
253
    if( stack_size > 4096 )
-
 
254
    {
-
 
255
        DBG("command line too long\n");
-
 
256
        return -30;
-
 
257
    }
-
 
258
 
-
 
259
    ex_pg_dir      = new_app_space();
-
 
260
 
-
 
261
    if( !ex_pg_dir )
-
 
262
    {
-
 
263
        mem_free(raw);
-
 
264
        return -30;                                    /* FIXME          */
-
 
265
    };
-
 
266
 
-
 
267
    ex_stack_tab   = ex_pg_dir + 4096;
-
 
268
    ex_pl0_stack   = ex_pg_dir + 4096 * 2;
-
 
269
 
-
 
270
    ex_stack_page  = core_alloc(0);                    /* 2^0 = 1 page   */
-
 
271
 
-
 
272
    if( ! ex_stack_page )
-
 
273
    {
-
 
274
        core_free(ex_stack_tab);
-
 
275
        mem_free(raw);
-
 
276
        return -30;                                    /* FIXME          */
-
 
277
    };
-
 
278
 
-
 
279
    __asm__ __volatile__ (
-
 
280
    "xorl %%eax, %%eax      \n\t"
-
 
281
    "rep stosl"
-
 
282
    :"=c"(tmp),"=D"(tmp)
-
 
283
    :"c"(1024),"D"(ex_stack_page + OS_BASE)
-
 
284
    :"eax","cc");
-
 
285
 
-
 
286
    ((u32_t*)(ex_stack_tab+OS_BASE))[1023] = ex_stack_page | 7;
-
 
287
 
-
 
288
    ex_stack = (exec_stack_t*)(ex_stack_page + OS_BASE
-
 
289
                               + PAGE_SIZE - stack_size);
-
 
290
    ex_stack->argc = 2;
-
 
291
 
-
 
292
    ex_path = MakePtr(char*, ex_stack, sizeof(exec_stack_t)+AUX_COUNT*sizeof(auxv_t));
-
 
293
 
-
 
294
    memcpy(ex_path, path, pathsize);
-
 
295
    ex_stack->path = (char*)(((addr_t)ex_path & 0xFFF) + 0x7FCFF000);  /* top of stack */
-
 
296
 
-
 
297
    if( cmdline )
-
 
298
    {
-
 
299
        ex_cmdline = ex_path + pathsize;
-
 
300
        memcpy(ex_cmdline, cmdline, cmdsize);
-
 
301
        ex_stack->cmdline = ex_stack->path + pathsize;
-
 
302
    };
-
 
303
 
-
 
304
/*
-
 
305
    ex_stack.env = null
-
 
306
    ex_stack.aux[0] = AT_NULL
-
 
307
 */
-
 
308
 
-
 
309
    DBG("create stack at %x\n\tpath %x\n\tcmdline %x\n",
-
 
310
         ex_stack, ex_stack->path, ex_stack->cmdline);
-
 
311
 
-
 
312
    pe_app_param(path, raw, ex_pg_dir, ex_stack_page);
-
 
313
    return 0;
-
 
314
};
-
 
315
 
-
 
316
#define  master_tab    (page_tabs+ (page_tabs>>10))
-
 
317
 
-
 
318
void sys_app_entry(addr_t raw, addr_t ex_stack)
-
 
319
{
-
 
320
    PIMAGE_DOS_HEADER     dos;
-
 
321
    PIMAGE_NT_HEADERS32   nt;
-
 
322
 
-
 
323
    size_t   img_size;
-
 
324
    count_t  img_pages;
-
 
325
    count_t  img_tabs;
-
 
326
    count_t  i;
-
 
327
    u32_t    tmp;
-
 
328
 
-
 
329
    __asm__ __volatile__ ("sti");
-
 
330
 
-
 
331
    DBG("pe_app_entry: raw %x esp %x\n", raw, ex_stack);
-
 
332
 
-
 
333
    dos = (PIMAGE_DOS_HEADER)raw;
-
 
334
    nt =  MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew);
-
 
335
 
-
 
336
    img_size  =  nt->OptionalHeader.SizeOfImage;
-
 
337
 
-
 
338
    img_pages = img_size >> 12;
-
 
339
    img_tabs  = ((img_size + 0x3FFFFF) & ~0x3FFFFF) >> 22;
-
 
340
 
-
 
341
    DBG("app pages %d app tabs %d\n", img_pages, img_tabs);
-
 
342
 
-
 
343
    for(i = 0; i < img_tabs; i++)
-
 
344
    {
-
 
345
        addr_t tab = core_alloc(0);
-
 
346
        ((u32_t*)master_tab)[i] = tab|7;                            /*   FIXME     */
-
 
347
    }
-
 
348
 
-
 
349
    ((u32_t*)master_tab)[0x7FC/4] = (ex_stack & 0xFFFFF000)|7;                                /*   FIXME     */
-
 
350
 
-
 
351
    __asm__ __volatile__ (
-
 
352
    "xorl %%eax, %%eax      \n\t"
-
 
353
    "rep stosl"
-
 
354
    :"=c"(tmp),"=D"(tmp)
-
 
355
    :"c"(img_tabs<<10),"D"(page_tabs)
-
 
356
    :"eax","cc");
-
 
357
 
-
 
358
    for(i = 0; i < img_pages; i++)
-
 
359
    {
-
 
360
        addr_t page = core_alloc(0);
-
 
361
        ((u32_t*)page_tabs)[i] = page | 7;                          /*   FIXME     */
-
 
362
    }
-
 
363
 
-
 
364
    create_image(0, raw);
-
 
365
 
-
 
366
    __asm__ __volatile__ (
-
 
367
    "xchgw %bx, %bx");
-
 
368
 
-
 
369
    addr_t entry = nt->OptionalHeader.AddressOfEntryPoint +
-
 
370
                   nt->OptionalHeader.ImageBase;
-
 
371
 
-
 
372
  //  __asm__ __volatile__ (
-
 
373
  //  "call %0":: "r" (entry));
-
 
374
 
-
 
375
    while(1);
-
 
376
 
-
 
377
};