summaryrefslogtreecommitdiffstats
path: root/private/os2/loader/loader.c
blob: 0e02491670ed67936a17da252686c713e4481fc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#define INCL_OS2V20_TASKING
#define INCL_OS2V20_MEMORY
#define INCL_OS2V20_FILESYS
#define INCL_ERROR_H
#define INCL_TYPES
#include <stdio.h>
#include <ctype.h>
#include <bseerr.h>
#include <os2dll.h>
#include <os2dll16.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <os2defp.h>
#include <mi.h>
#include <ldrxport.h>
#if PMNT
#define INCL_32BIT
#include <pmnt.h>
extern  APIRET  PMNTMemMap(PSEL);
extern  APIRET  PMNTIOMap(void);
#endif

#ifndef MAXPATHLEN
#define MAXPATHLEN 260
#endif

ULONG GetTickCount(VOID);
PVOID   LDRExecInfo;
extern  ULONG   Od2Start16Stack;
extern  ULONG   Od2Start16DS;
extern  POD2_SIG_HANDLER_REC pSigHandlerRec;
extern  POD2_VEC_HANDLER_REC pVecHandlerRec;
USHORT  LDRDoscallsSel;
extern  VOID    Od2EnableCtrlHandling(VOID);
extern  VOID    Od2UpdateLocalInfoAtStart(VOID);
extern  char    Od2PgmFullPathBuf[];
extern  PSZ     Od2PgmFilePath;
extern  ULONG   timing;
extern  BOOLEAN Od2SignalEnabled;
extern  BOOL    Od2ExpandOd2LibPathElements(PCHAR ExpandedString, ULONG MaxSize);
extern  PVOID   EntryFlat(VOID);
extern  ULONG   Od2ExecPgmErrorText;
extern  ULONG   Od2ExecPgmErrorTextLength;
extern  ULONG   Od2EnvCommandOffset;
extern  POD2_PROCESS Od2Process;


#if PMNT
// From winbase.h

ULONG
SetThreadAffinityMask(
    HANDLE hThread,
    DWORD dwThreadAffinityMask
    );

HANDLE
GetCurrentThread(
    VOID
    );

#endif // PMNT

VOID
Ow2BoundAppLoadPopup(
    IN PSZ AppName
    );

ldrlibi_t InitRecords[MAX_INIT_RECORDS];

#if DBG
void LoadStop(
    int id
    )
{
        UNREFERENCED_PARAMETER(id);

        DbgPrint("LoadStop\n");
//      DbgUserBreakPoint();
}
#else
#define LoadStop(x)
#endif

int
Loader_main()
{
    PUCHAR      pname;
    ldrrei_t    exec_info;
    ldrrei_t    *pexec_info = &exec_info;
    PNT_TIB     ptib;
    PIB         *pib;
    PVOID       MemoryAddress;
    ULONG       RegionSize;
    PCHAR       pchar;
    ULONG       i;
    OD2_SIG_HANDLER_REC *pSig;
    OD2_VEC_HANDLER_REC *pVec;
    ULONG       address;
    int         rc;
    NTSTATUS    Status;
    I386DESCRIPTOR Desc;
    OS2_API_MSG m;
    P_LDRNEWEXE_MSG a = &m.u.LdrNewExe;
    POS2_CAPTURE_HEADER CaptureBuffer;
    ULONG       FileFlags;
    ULONG       FileType;
    STRING      ProcessNameString;
    STRING      LibPathNameString;
    STRING      FailNameString;
    // Remember that \OS2SS\DRIVES\ is added for each path element !
    // Let's assume the smallest component is 5 chars long (like C:\X;)
    // (although it could be less, like "." or "\")
    // => we need to add 14 * (MAXPATHLEN/5) = MAXPATHLEN*3
    CHAR        ExpandedLibPath[MAXPATHLEN*4];
    ULONG       NumOfInitRecords;

    DosGetThreadInfo(&ptib, &pib);

    pname = Od2PgmFullPathBuf;

    rc = Od2Canonicalize(pname,
                         CANONICALIZE_FILE_OR_DEV,
                         &ProcessNameString,
                         NULL,
                         &FileFlags,
                         &FileType
                        );
    /*
     * Reserve 64k at BASE_TILE, which corresponds with selector 0
     */
    MemoryAddress = (PVOID) BASE_TILE;
    RegionSize = _64K;
    if ((rc = NtAllocateVirtualMemory(NtCurrentProcess(),
                      &MemoryAddress,
                      0,
                      &RegionSize,
                      MEM_RESERVE,
                      PAGE_NOACCESS)) != NO_ERROR) {
        LoadStop(0);
        goto returnlabel;
    }

    Od2ExpandOd2LibPathElements(ExpandedLibPath, sizeof(ExpandedLibPath));
    RtlInitString(&LibPathNameString, ExpandedLibPath);

    FailNameString.Buffer = NULL;
    FailNameString.Length = 0;
    FailNameString.MaximumLength = 50; // same size as ErrorBuffer in client\dllinit.c

    CaptureBuffer = Od2AllocateCaptureBuffer(
                      4,
                      0,
                      ProcessNameString.MaximumLength +
                      LibPathNameString.MaximumLength +
                      FailNameString.MaximumLength +
                      MAX_INIT_RECORDS * sizeof(ldrlibi_t)
                    );
    if (CaptureBuffer == NULL) {
        LoadStop(6);
        goto returnlabel;
    }

    Od2CaptureMessageString( CaptureBuffer,
                             ProcessNameString.Buffer,
                             ProcessNameString.Length,
                             ProcessNameString.MaximumLength,
                             &a->ProcessName
                           );
    RtlFreeHeap(Od2Heap, 0, ProcessNameString.Buffer);

    Od2CaptureMessageString( CaptureBuffer,
                             FailNameString.Buffer,
                             FailNameString.Length,
                             FailNameString.MaximumLength,
                             &a->FailName
                           );

    Od2CaptureMessageString( CaptureBuffer,
                             LibPathNameString.Buffer,
                             LibPathNameString.Length,
                             LibPathNameString.MaximumLength,
                             &a->LibPathName
                           );

    Od2CaptureMessageString( CaptureBuffer,
                             NULL,
                             0,
                             MAX_INIT_RECORDS * sizeof(ldrlibi_t),
                             &a->InitRecords
                           );

    a->EntryFlatAddr = (ULONG)EntryFlat;

    Od2CallSubsystem( &m, CaptureBuffer, Ol2LdrNewExe, sizeof( *a ) );

#if DBG
    IF_OD2_DEBUG( LOADER ) {
        if (a->BoundApp) {
            KdPrint(("Loader_main: Bound App Detected\n"));
        }
    }
#endif

    Od2Process->BoundApp = a->BoundApp;             // store away the BoundApp flag

#if PMNT
    PMFlags = a->PMProcess;
#endif // PMNT

    rc = m.ReturnedErrorValue;
    if (rc != NO_ERROR) {
#if PMNT
        if (rc == ERROR_PMSHELL_NOT_UP || rc == ERROR_2ND_PMSHELL) {
            Od2FreeCaptureBuffer( CaptureBuffer );
            Ow2PMShellErrorPopup(Od2PgmFilePath,rc);
            DosExit(EXIT_PROCESS, rc);
            return(rc);
        }
#endif
        RtlCopyMemory((PVOID)Od2ExecPgmErrorText, a->FailName.Buffer, a->FailName.Length);
        Od2ExecPgmErrorTextLength = a->FailName.Length;
        *((PCHAR)Od2ExecPgmErrorText + Od2ExecPgmErrorTextLength) = '\0';
        Od2FreeCaptureBuffer( CaptureBuffer );
        LoadStop(5);

        if (a->BoundApp) {
            Ow2BoundAppLoadPopup(Od2PgmFilePath);
        }

        goto returnlabel;
    }

#if PMNT
    if (ProcessIsPMProcess() && !ProcessIsPMShell())
    {
        UNICODE_STRING EventString_U;
        OBJECT_ATTRIBUTES Obja;
        NTSTATUS Status;
        HANDLE Od2PMShellEvent;

        // Whether PMShell or just a PM app, we need to open the PMShell
        // synchronization semaphore

        RtlInitUnicodeString( &EventString_U, OS2_SS_PMSHELL_EVENT);
        InitializeObjectAttributes(
                    &Obja,
                    &EventString_U,
                    OBJ_CASE_INSENSITIVE,
                    NULL,
                    NULL);

        //
        // Open the global subsystem synchronization Nt semaphore
        //
        Status = NtOpenEvent(&Od2PMShellEvent,
                        EVENT_ALL_ACCESS,
                        &Obja);

        if (!NT_SUCCESS(Status))
        {
#if DBG
            DbgPrint("Os2: Loader_main(), failed to open PMShellEvent, Status %x\n", Status);
#endif // DBG
        }
        else
        {
            // Regular PM app - wait on the PMShell semaphore !
            Status = NtWaitForSingleObject(
                Od2PMShellEvent,
                TRUE,               // Alertable
                NULL
                );
#if DBG
            if (!NT_SUCCESS(Status))
            {
                DbgPrint("Os2: Loader_main(), failed to NtWaitForSingleObject PMShellEvent, Status %x\n", Status);
            }
#endif
        }
    }   // PM apps (non-PMShell) handling

    // Force all PM threads to run on processor#1. Otherwise, PM Desktop
    // locks-up
    if (ProcessIsPMProcess())
    {
        DWORD Ret;

        Ret = SetThreadAffinityMask(
            GetCurrentThread(),
            0x1);
#if DBG
        if (Ret == 0)
        {
            DbgPrint("OS2: main() - failed to SetThreadAffinityMask\n");
        }
        else if (Ret != 1)
        {
            DbgPrint("OS2: Loader_main() - SetThreadAffinityMask returned %x (now set to 1)\n",
                Ret);
        }
#endif // DBG
    }
#endif // PMNT

    NumOfInitRecords = a->NumOfInitRecords;
    RtlCopyMemory(InitRecords, a->InitRecords.Buffer,
                    NumOfInitRecords * sizeof(ldrlibi_t));
    RtlCopyMemory(pexec_info, &a->ExecInfo, sizeof(exec_info));
    LDRDoscallsSel = (USHORT)a->DoscallsSel;
    Od2FreeCaptureBuffer( CaptureBuffer );

    Desc.Limit = _64K - 1;
    Desc.Type = READ_WRITE_DATA;

        //
        // Set selectors for the tiled heap area
        //
    for (Desc.BaseAddress = OD2TILEDHEAP_BASE;
         Desc.BaseAddress < (OD2TILEDHEAP_BASE + OD2TILEDHEAP_SIZE);
         (ULONG)(Desc.BaseAddress) += _64K ) {

        Status = Nt386SetDescriptorLDT (
                    NULL,
                    FLATTOSEL(Desc.BaseAddress),
                    Desc);
        ASSERT (NT_SUCCESS( Status ));
    }

    Desc.BaseAddress = OD2ENVIRONMENT_BASE;
    Status = Nt386SetDescriptorLDT (
                NULL,
                FLATTOSEL(OD2ENVIRONMENT_BASE),
                Desc);
    ASSERT (NT_SUCCESS( Status ));

    pexec_info->ei_envsel = FLATTOSEL(Od2Environment);
    pchar = (PCHAR)Od2Environment;

    /*
     * Start at beginning of environment and find the command line
     * (Env, 0, fullpathname, cmdline)
     */

    pchar = (PCHAR)Od2Environment;
    while(*pchar++ != '\0') {
        if (*pchar == '\0') {
        pchar++;
        if (*pchar == '\0')
            break;
        }
    }
    pchar++;
    pchar += strlen(pname);
    pchar++;

    pexec_info->ei_comoff = (USHORT)((ULONG) pchar & 0xffff);
    Od2EnvCommandOffset = pexec_info->ei_comoff;

    /*
     * Set LDRExecInfo pointer for use in OS2DLL in DosGetInfoSeg
     */
    (PVOID) LDRExecInfo = pexec_info;

    Od2UpdateLocalInfoAtStart();

#if DBG
    IF_OD2_DEBUG ( LOADER ) {
        DbgPrint("Values in exec_info for program:\n");
        DbgPrint("ei_startaddr=%x\n", pexec_info->ei_startaddr);   /* instruction pointer */
        DbgPrint("ei_stackaddr=%x\n", pexec_info->ei_stackaddr);   /* instruction pointer */
        DbgPrint("ei_ds=%x\n", pexec_info->ei_ds);   /* instruction pointer */
        DbgPrint("ei_dgroupsize=%x\n", pexec_info->ei_dgroupsize);   /* instruction pointer */
        DbgPrint("ei_heapsize=%x\n", pexec_info->ei_heapsize);   /* instruction pointer */
        DbgPrint("ei_loadtype=%x\n", pexec_info->ei_loadtype);   /* instruction pointer */
        DbgPrint("ei_envsel=%x\n", pexec_info->ei_envsel);   /* instruction pointer */
        DbgPrint("ei_comoff=%x\n", pexec_info->ei_comoff);   /* instruction pointer */
        DbgPrint("ei_stacksize=%x\n", pexec_info->ei_stacksize);   /* instruction pointer */
        DbgPrint("ei_hmod=%x\n", pexec_info->ei_hmod);   /* instruction pointer */
    }
#endif

#ifdef PMNT
    if (ProcessIsPMApp())
    {
        SEL DummySel;

        PMNTMemMap(&DummySel);
        PMNTIOMap();
    }
#endif // PMNT

	/*
     * Setup Signal handling default values.
     */
    pSig = (POD2_SIG_HANDLER_REC) pSigHandlerRec;
    address = FLATTOFARPTR((ULONG)(SELTOFLAT(LDRDoscallsSel)));
    pSig->fholdenable = HLDSIG_ENABLE;
    pSig->outstandingsig[0].sighandleraddr =
    pSig->outstandingsig[1].sighandleraddr =
    pSig->outstandingsig[2].sighandleraddr =
    pSig->outstandingsig[3].sighandleraddr =
    pSig->outstandingsig[4].sighandleraddr =
    pSig->outstandingsig[5].sighandleraddr =
    pSig->outstandingsig[6].sighandleraddr = 0;
    pSig->doscallssel = address;
    pSig->sighandler[SIG_BROKENPIPE - 1] = (ULONG)
                (address | ThunkOffsetDosReturn);
    pSig->action[SIG_BROKENPIPE - 1] = SIGA_IGNORE;

    pSig->sighandler[SIG_CTRLBREAK - 1] = (ULONG)
                (address | ThunkOffsetExitProcessStub);
    pSig->action[SIG_CTRLBREAK - 1] = SIGA_ACCEPT;

    pSig->sighandler[SIG_CTRLC - 1] = (ULONG)
                (address | ThunkOffsetExitProcessStub);
    pSig->action[SIG_CTRLC - 1] = SIGA_ACCEPT;

    pSig->sighandler[SIG_KILLPROCESS - 1] = (ULONG)
                (address | ThunkOffsetExitProcessStub);
    pSig->action[SIG_KILLPROCESS - 1] = SIGA_ACCEPT;

    pSig->sighandler[SIG_PFLG_A - 1] = (ULONG)
                (address | ThunkOffsetDosReturn);
    pSig->action[SIG_PFLG_A - 1] = SIGA_ACCEPT;

    pSig->sighandler[SIG_PFLG_B - 1] = (ULONG)
                (address | ThunkOffsetDosReturn);
    pSig->action[SIG_PFLG_B - 1] = SIGA_ACCEPT;

    pSig->sighandler[SIG_PFLG_C - 1] = (ULONG)
                (address | ThunkOffsetDosReturn);
    pSig->action[SIG_PFLG_C - 1] = SIGA_ACCEPT;

    /*
     * Setup Vector handling default values.
     */
    pVec = (POD2_VEC_HANDLER_REC) pVecHandlerRec;
    pVec->doscallssel = address;
    pVec->VecHandler[0] = (ULONG) (address | ThunkOffsetDosReturn);
    pVec->VecHandler[1] = (ULONG) (address | ThunkOffsetDosReturn);
    pVec->VecHandler[2] = (ULONG) (address | ThunkOffsetDosReturn);
    pVec->VecHandler[3] = (ULONG) (address | ThunkOffsetDosReturn);
    pVec->VecHandler[4] = (ULONG) (address | ThunkOffsetDosReturn);
    pVec->VecHandler[5] = (ULONG) (address | ThunkOffsetDosReturn);

    //
    // Go over all DLLs init routines
    //
    for (i = 0; i < NumOfInitRecords; i++) {
        if (InitRecords[i].stackaddr.ptr_sel == 0) {
#if DBG
            IF_OD2_DEBUG ( LOADER ) {
                DbgPrint("Replacing SS:SP of module\n");
            }
#endif
            InitRecords[i].stackaddr.ptr_sel = pexec_info->ei_stackaddr.ptr_sel;
            InitRecords[i].stackaddr.ptr_off = pexec_info->ei_stackaddr.ptr_off;
        }
#if DBG
        IF_OD2_DEBUG ( LOADER ) {
            DbgPrint("=== Calling Init routine %d for app.\n", i);
            DbgPrint("Values of libi:\n");
            DbgPrint("startaddr=%x\n", InitRecords[i].startaddr);   /* instruction pointer */
            DbgPrint("stackaddr=%x\n", InitRecords[i].stackaddr);   /* instruction pointer */
            DbgPrint("ds=%x\n", InitRecords[i].ds);   /* instruction pointer */
            DbgPrint("heapsize=%x\n", InitRecords[i].heapsize);   /* instruction pointer */
            DbgPrint("handle=%x\n", InitRecords[i].handle);   /* instruction pointer */
        }
#endif
        rc = ldrLibiInit(&InitRecords[i], pexec_info);
#if DBG
    IF_OD2_DEBUG ( LOADER ) {
        DbgPrint("=== Init routine returned %d\n", rc);
    }
#endif
        if (rc == 0) {
            DosExit(EXIT_PROCESS, 255); // same as OS/2 1.x does
            return(rc);
        }
    }

#if PMNT
    // If this is PMShell, release waiting PM apps now
    if (ProcessIsPMShell())
    {
        UNICODE_STRING EventString_U;
        OBJECT_ATTRIBUTES Obja;
        NTSTATUS Status;
        HANDLE Od2PMShellEvent;

        RtlInitUnicodeString( &EventString_U, OS2_SS_PMSHELL_EVENT);
        InitializeObjectAttributes(
                    &Obja,
                    &EventString_U,
                    OBJ_CASE_INSENSITIVE,
                    NULL,
                    NULL);

        //
        // Open the global subsystem synchronization Nt semaphore
        //
        Status = NtOpenEvent(&Od2PMShellEvent,
                        EVENT_ALL_ACCESS,
                        &Obja);

        if (!NT_SUCCESS(Status))
        {
#if DBG
            DbgPrint("Os2: Loader_main(), after LdrLibiInit, failed to open PMShellEvent, Status %x\n", Status);
#endif // DBG
        }
        else
        {
            // For PMShell, release the PMShell synchronization event
            Status = NtSetEvent (
                    Od2PMShellEvent,
                    NULL);
#if DBG
            if (!NT_SUCCESS(Status))
            {
                DbgPrint("Os2: Loader_main(), after LdrLibiInit, failed to NtSetEvent PMShellEvent, Status %x\n", Status);
            }
#endif // DBG
        }
    }
#endif // PMNT

    /*
     * Save starting stack address & DS for exit list processing
     */
    (PULONG) Od2Start16Stack =
        (PULONG)(pexec_info->ei_stackaddr.ptr_flat);
    (PULONG) Od2Start16DS = (PULONG) (pexec_info->ei_ds);

    pSig->signature = 0xdead;
    Od2EnableCtrlHandling();
    if (timing)
    {
        printf("Os2 time before ldrstart is %d\n", (GetTickCount()) - timing);
    }
    Od2SignalEnabled = TRUE;

#if DBG
    IF_OD2_DEBUG ( LOADER ) {
        DbgPrint("Values in exec_info:\n");
        DbgPrint("ei_startaddr=%x\n", pexec_info->ei_startaddr);   /* instruction pointer */
        DbgPrint("ei_stackaddr=%x\n", pexec_info->ei_stackaddr);   /* instruction pointer */
        DbgPrint("ei_ds=%x\n", pexec_info->ei_ds);   /* instruction pointer */
        DbgPrint("ei_dgroupsize=%x\n", pexec_info->ei_dgroupsize);   /* instruction pointer */
        DbgPrint("ei_heapsize=%x\n", pexec_info->ei_heapsize);   /* instruction pointer */
        DbgPrint("ei_loadtype=%x\n", pexec_info->ei_loadtype);   /* instruction pointer */
        DbgPrint("ei_envsel=%x\n", pexec_info->ei_envsel);   /* instruction pointer */
        DbgPrint("ei_comoff=%x\n", pexec_info->ei_comoff);   /* instruction pointer */
        DbgPrint("ei_stacksize=%x\n", pexec_info->ei_stacksize);   /* instruction pointer */
        DbgPrint("ei_hmod=%x\n", pexec_info->ei_hmod);   /* instruction pointer */
    }
#endif

    ldrstart(pexec_info);

returnlabel:

    //
    // Set to indicate loader error to os2srv
    //

    DosExit(EXIT_PROCESS, 0x80000000 | rc);
    return(rc);
}