summaryrefslogtreecommitdiffstats
path: root/private/os2/client/dllvm.c
blob: 278e8ac1db0c8989271b63f4cfc7228141508fc8 (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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    dllvm.c

Abstract:

    This module implements the OS/2 V2.0 Memory Management API Calls


Author:

    Steve Wood (stevewo) 02-Nov-1989

Revision History:

    YaronS 18-APR-1991 - modified DosAllocMem such that all allocations
    are confined to a 512M address space. (set zero bits to 3 when
    call NtAllocateVirtualMemory.

    YaronS 6-SEP-1992 - flexible base 512M

--*/

#define INCL_OS2V20_MEMORY
#define INCL_OS2V20_ERRORS
#include "os2dll.h"
#include "os2dll16.h"

APIRET
DosAllocMem(
    OUT PVOID *BaseAddress,
    IN ULONG RegionSize,
    IN ULONG Flags
    )
{
    NTSTATUS Status;
    // PVOID MemoryAddress;
    APIRET rc;
    ULONG AllocationType, Protect;
    ULONG Bits;
    // PVOID FirstSharedBaseAddress;

    if (RegionSize == 0 || (Flags & ~(fALLOC|PAG_GUARD))) {
        return( ERROR_INVALID_PARAMETER );
        }

    rc = Or2MapFlagsToProtection( Flags, &Protect );
    if (rc != NO_ERROR) {
        return( rc );
        }

    if (Flags & PAG_COMMIT) {
        AllocationType = MEM_COMMIT;
        }
    else {
        AllocationType = MEM_RESERVE;
        }

    //
    // probe address pointer.
    //
    try {
        Od2ProbeForWrite(BaseAddress, sizeof(ULONG), 1);
        }
    except( EXCEPTION_EXECUTE_HANDLER ) {
       Od2ExitGP();
        }
    if (Flags & OBJ_TILE)
        Bits = 1;
    else
        Bits = 0;

    Status = NtAllocateVirtualMemory( NtCurrentProcess(),
                                      BaseAddress,
                                      Bits,
                                      &RegionSize,
                                      AllocationType,
                                      Protect
                                    );
    if (!NT_SUCCESS( Status )) {
        return( ERROR_NOT_ENOUGH_MEMORY );
    }

    return( NO_ERROR );
}


BOOLEAN
Od2ValidateBaseAddress(
    PVOID BaseAddress,
    PMEMORY_BASIC_INFORMATION MemoryInformation
    )
{
    NTSTATUS Status;

    //
    // If BaseAddress is within the first 64K of memory then it is not
    // a valid address.
    //

    if (((ULONG)BaseAddress & ~Od2NtSysInfo.AllocationGranularity) == 0) {
        return( FALSE );
        }

    Status = NtQueryVirtualMemory( NtCurrentProcess(),
                                   BaseAddress,
                                   MemoryBasicInformation,
                                   MemoryInformation,
                                   sizeof( *MemoryInformation ),
                                   NULL
                                 );
    if (!NT_SUCCESS( Status ) || BaseAddress != MemoryInformation->BaseAddress) {
        return( FALSE );
        }
    else {
        return( TRUE );
        }
}

// The parameter pRemoveLDTEntry is the pointer to boolean variable. It has
// the value "LDT entry wasn't removed yet".
// So on the entry of the function it will have value TRUE only in the case
// that LDT entry must be removed. On the exit it will be TRUE only in the
// case that LDT entry wasn't removed.

APIRET
DosFreeMem(
    PVOID BaseAddress,
    PBOOLEAN pRemoveLDTEntry
    )
{
    OS2_API_MSG m;
    POS2_DOSFREEMEM_MSG a = &m.u.DosFreeMem;
    ULONG RegionSize = 0;
    NTSTATUS Status;

    Status = NtFreeVirtualMemory( NtCurrentProcess(),
                                  &BaseAddress,
                                  &RegionSize,
                                  MEM_RELEASE
                                );
    if (NT_SUCCESS( Status )) {
        return( NO_ERROR );
    }
    else
    if ((Status == STATUS_UNABLE_TO_FREE_VM) ||
        (Status == STATUS_UNABLE_TO_DELETE_SECTION)) {

        // Shared memory.

        APIRET rc;

        a->BaseAddress = BaseAddress;

        // If LDT entry must be removed by the server.

        a->RemoveLDTEntry = *pRemoveLDTEntry;

        rc =  Od2CallSubsystem( &m, NULL, Os2FreeMem, sizeof( *a ) );
        if (rc == NO_ERROR) {

            // If server succeeded to remove LDT entry, sign that it must not
            // be removed any more.

            *pRemoveLDTEntry = FALSE;
        }
        return(rc);
    }
    else {
        return( ERROR_INVALID_ADDRESS );
    }
}


APIRET
DosSetMem(
    IN PVOID BaseAddress,
    IN ULONG RegionSize,
    IN ULONG Flags
    )
{
    OS2_API_MSG m;
    POS2_QUERYVIRTUALMEMORY_MSG a = &m.u.QueryVirtualMemory;
    ULONG Protect, OldProtect;
    APIRET rc;
    MEMORY_BASIC_INFORMATION MemoryInformation;
    NTSTATUS Status;

    if (RegionSize == 0) {
        return( ERROR_INVALID_PARAMETER );
        }

    if (Flags != PAG_DECOMMIT
        && (((Flags & (fPERM | PAG_DEFAULT)) == 0) ||
            ((Flags & (~(fSET|PAG_GUARD) | PAG_DECOMMIT)) != 0) ||
            ((Flags & (fPERM|PAG_GUARD)) && (Flags & PAG_DEFAULT))
           )
       ) {
        return( ERROR_INVALID_PARAMETER );
        }

    if (Flags == PAG_DECOMMIT) {
        Status = NtFreeVirtualMemory( NtCurrentProcess(),
                                      &BaseAddress,
                                      &RegionSize,
                                      MEM_DECOMMIT
                                    );
        //
        // The STATUS_UNABLE_TO_FREE_VM status is returned when trying
        // to decommit pages of mapped sections. This error should
        // not be reported to the user program.
        //
        if ((Status == STATUS_UNABLE_TO_FREE_VM) ||
            (Status == STATUS_UNABLE_TO_DELETE_SECTION)) {
            //BUGBUG - aren't there cases where the process is the last one
            //         to use this memory ? If so, why do we get this error
            //         from NT (maybe os2srv is holding the section by error).
            Status = STATUS_SUCCESS;
            }
        else
        if (Status == STATUS_UNABLE_TO_DECOMMIT_VM) {
            return( ERROR_ACCESS_DENIED );
            }
        }
    else {
        if (Flags & PAG_DEFAULT) {
            a->BaseAddress = BaseAddress;
            if (Od2CallSubsystem( &m,
                                  NULL,
                                  Oi2QueryVirtualMemory,
                                  sizeof( *a )
                                )
               ) {
                return( m.ReturnedErrorValue );
                }

            if (!a->SharedMemory) {
                Status = NtQueryVirtualMemory( NtCurrentProcess(),
                                               BaseAddress,
                                               MemoryBasicInformation,
                                               &MemoryInformation,
                                               sizeof( MemoryInformation ),
                                               NULL
                                             );

                if (MemoryInformation.State == MEM_FREE) {
                    return( ERROR_INVALID_ADDRESS );
                    }

                Protect = MemoryInformation.AllocationProtect;
                }
            else {
                rc = Or2MapFlagsToProtection( a->AllocationFlags, &Protect );
                if (rc != NO_ERROR) {
                    return( rc );
                    }
                }
            }
        else {
            rc = Or2MapFlagsToProtection( Flags, &Protect );
            if (rc != NO_ERROR) {
                return( rc );
                }
            }

        if (Flags & PAG_COMMIT) {
            Status = NtAllocateVirtualMemory( NtCurrentProcess(),
                                              &BaseAddress,
                                              1,
                                              &RegionSize,
                                              MEM_COMMIT,
                                              Protect
                                            );
            if (Status == STATUS_ALREADY_COMMITTED) {
                Status = STATUS_SUCCESS;
            }
        }
        else {
            Status = NtProtectVirtualMemory( NtCurrentProcess(),
                                             &BaseAddress,
                                             &RegionSize,
                                             Protect,
                                             &OldProtect
                                           );

            if (Status == STATUS_NOT_COMMITTED) {
                return( ERROR_ACCESS_DENIED );
                }
            }
        }

    if (NT_SUCCESS( Status )) {
        return( NO_ERROR );
        }
    else
    if (Status == STATUS_INVALID_PARAMETER) {
        return( ERROR_INVALID_ADDRESS );
        }
    else {
        return( Or2MapNtStatusToOs2Error( Status, ERROR_INVALID_ADDRESS ) );
        }
}


APIRET
DosGiveSharedMem(
    IN PVOID BaseAddress,
    IN PID ProcessId,
    IN ULONG Flags
    )
{
    OS2_API_MSG m;
    POS2_DOSGIVESHAREDMEM_MSG a = &m.u.DosGiveSharedMem;
    MEMORY_BASIC_INFORMATION MemoryInformation;
    APIRET rc;

    if (!Od2ValidateBaseAddress( BaseAddress, &MemoryInformation )) {
        return( ERROR_INVALID_ADDRESS );
        }

    if ((Flags & fPERM) == 0 || (Flags & ~(fGIVESHR|PAG_GUARD))) {
        return( ERROR_INVALID_PARAMETER );
        }

    if (MemoryInformation.State == MEM_PRIVATE) {
        return( ERROR_ACCESS_DENIED );
        }

    if (ProcessId == 0) {
        return( ERROR_INVALID_PROCID );
        }

    rc = Or2MapFlagsToProtection( a->Flags = Flags, &a->PageProtection );

    if (rc != NO_ERROR) {
        return( rc );
        }

    a->BaseAddress = BaseAddress;
    a->ProcessId = ProcessId;

    Od2CallSubsystem( &m, NULL, Os2GiveSharedMem, sizeof( *a ) );

    return(m.ReturnedErrorValue);
}

APIRET
DosGetSharedMem(
    IN PVOID BaseAddress,
    IN ULONG Flags
    )
{
    OS2_API_MSG m;
    POS2_DOSGETSHAREDMEM_MSG a = &m.u.DosGetSharedMem;
    APIRET rc;

    if ((Flags & fPERM) == 0 || (Flags & ~(fGETSHR|PAG_GUARD))) {
        return( ERROR_INVALID_PARAMETER );
        }

    rc = Or2MapFlagsToProtection( a->Flags = Flags, &a->PageProtection );
    if (rc != NO_ERROR) {
        return( rc );
        }

    a->BaseAddress = BaseAddress;

    Od2CallSubsystem( &m, NULL, Os2GetSharedMem, sizeof( *a ) );

    return(m.ReturnedErrorValue);
}

APIRET
DosGetNamedSharedMem(
    OUT PVOID *BaseAddress,
    IN PSZ ObjectName,
    IN ULONG Flags
    )
{
    OS2_API_MSG m;
    POS2_DOSGETNAMEDSHAREDMEM_MSG a = &m.u.DosGetNamedSharedMem;
    APIRET rc;
    POS2_CAPTURE_HEADER CaptureBuffer;

    if ((Flags & fPERM) == 0 || (Flags & ~(fGETNMSHR|PAG_GUARD))) {
        return( ERROR_INVALID_PARAMETER );
        }

    rc = Or2MapFlagsToProtection( a->Flags = Flags, &a->PageProtection );
    if (rc != NO_ERROR) {
        return( rc );
        }

    a->BaseAddress = NULL;

    //
    // probe address pointer.
    //

    try {
        *BaseAddress = 0;
        }
    except( EXCEPTION_EXECUTE_HANDLER ) {
       Od2ExitGP();
        }


    rc = Od2CaptureObjectName( ObjectName,
                               CANONICALIZE_SHARED_MEMORY,
                               0,
                               &CaptureBuffer,
                               &a->ObjectName
                             );
    if (rc != NO_ERROR) {
        return( rc );
        }
    Od2CallSubsystem( &m, CaptureBuffer, Os2GetNamedSharedMem, sizeof( *a ) );

    if (m.ReturnedErrorValue == NO_ERROR) {
        *BaseAddress = a->BaseAddress;
        }

    Od2FreeCaptureBuffer( CaptureBuffer );

    return( m.ReturnedErrorValue );
}

APIRET
DosAllocSharedMem(
    OUT PVOID *BaseAddress,
    IN PSZ ObjectName,
    IN ULONG RegionSize,
    IN ULONG Flags,
    IN BOOLEAN CreateLDTEntry   // Create LDT entry in the server.
    )
{
    OS2_API_MSG m;
    POS2_DOSALLOCSHAREDMEM_MSG a = &m.u.DosAllocSharedMem;
    APIRET rc;
    POS2_CAPTURE_HEADER CaptureBuffer;

    if (RegionSize == 0
        || (Flags & ~(fALLOCSHR|PAG_GUARD))
        || ((Flags & PAG_COMMIT) && (Flags & fPERM) == 0)
        || (ObjectName != NULL) && (Flags & (OBJ_GETTABLE|OBJ_GIVEABLE))
        || (ObjectName == NULL) && (Flags & (OBJ_GETTABLE|OBJ_GIVEABLE)) == 0
       ) {
        return( ERROR_INVALID_PARAMETER );
        }

    rc = Or2MapFlagsToProtection( a->Flags = Flags, &a->PageProtection );
    if (rc != NO_ERROR) {
        return( rc );
        }

    a->BaseAddress = NULL;
    a->RegionSize = RegionSize;
    a->CreateLDTEntry = CreateLDTEntry; // Server will create LDT entry.

    //
    // probe address pointer.
    //

    try {
        *BaseAddress = 0;
        }
    except( EXCEPTION_EXECUTE_HANDLER ) {
       Od2ExitGP();
        }

    rc = Od2CaptureObjectName( ObjectName,
                               CANONICALIZE_SHARED_MEMORY,
                               0,
                               &CaptureBuffer,
                               &a->ObjectName
                             );
    if (rc != NO_ERROR) {
        return( rc );
        }

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

    if (m.ReturnedErrorValue == NO_ERROR) {
        *BaseAddress = a->BaseAddress;
        }

    if (CaptureBuffer != NULL) {
       Od2FreeCaptureBuffer( CaptureBuffer );
       }

    return( m.ReturnedErrorValue );
}


APIRET
DosQueryMem(
    IN PVOID BaseAddress,
    IN OUT PULONG RegionSize,
    OUT PULONG Flags
    )
{
    NTSTATUS Status;
    MEMORY_BASIC_INFORMATION MemoryInformation;
    ULONG MemFlags, OriginalBaseAddress, OldEndAddress, NewEndAddress;
    ULONG Protection;
    SEL sel;
    POS21X_CSALIAS pCSAlias;
    BOOLEAN SelIsCSADS;

    OriginalBaseAddress = (ULONG)BaseAddress;
    sel = FLATTOSEL(BaseAddress);

    BaseAddress = (PVOID)((ULONG)BaseAddress & ~(Od2NtSysInfo.PageSize - 1));
    Status = NtQueryVirtualMemory( NtCurrentProcess(),
                                   BaseAddress,
                                   MemoryBasicInformation,
                                   &MemoryInformation,
                                   sizeof( MemoryInformation ),
                                   NULL
                                 );
    if (!NT_SUCCESS( Status )) {
        return( ERROR_INVALID_ADDRESS );
        }

    MemFlags = 0;
    Protection = MemoryInformation.Protect;

    if (MemoryInformation.State == MEM_COMMIT) {
        MemFlags |= PAG_COMMIT;
        }
    else
    if (MemoryInformation.State == MEM_FREE) {
        MemFlags |= PAG_FREE;
        }
    else
    if (MemoryInformation.State == MEM_RESERVE) {
        Protection = MemoryInformation.AllocationProtect;
        }

    // Check if this is a Data Segment of a CSAlias Only for "Pseudo shared"
    // READ/WRITE Data Segments
    // This is done since when creating a CSAlias we change the DS page
    // protection from PAGE_EXECUTE_WRITE_COPY TO PAGE_READ_WRITE since it is
    // mapped twice (once to the DS and once to the CS)

    SelIsCSADS = FALSE;
    if (MemoryInformation.AllocationProtect == PAGE_READWRITE)
    {
        AcquireTaskLock();
        if (Od2CSAliasListHead != 0)
        {
            for (pCSAlias = (POS21X_CSALIAS) Od2CSAliasListHead;
                 pCSAlias != NULL;
                 pCSAlias = (POS21X_CSALIAS) (pCSAlias->Next))
            {
                if (pCSAlias->selDS == sel)
                {
                    SelIsCSADS = TRUE;
                    break;
                }
            }
        }
        ReleaseTaskLock();
    }

    if ((MemoryInformation.Type != MEM_PRIVATE) &&
        (MemoryInformation.AllocationProtect != PAGE_EXECUTE_WRITECOPY) &&
        (sel >= FIRST_SHARED_SELECTOR) &&
        (!SelIsCSADS))
    {
        MemFlags |= PAG_SHARED;
    }

    if (MemoryInformation.State != MEM_FREE &&
        MemoryInformation.AllocationBase == MemoryInformation.BaseAddress
       ) {
        MemFlags |= PAG_BASE;
        }

    switch( Protection & 0xFF) {
        case PAGE_NOACCESS          : break;
        case PAGE_READONLY          : MemFlags |= PAG_READ;  break;
        case PAGE_READWRITE         : MemFlags |= PAG_READ | PAG_WRITE;  break;
        case PAGE_WRITECOPY         : MemFlags |= PAG_READ | PAG_WRITE;  break;
        case PAGE_EXECUTE           : MemFlags |= PAG_EXECUTE;  break;
        case PAGE_EXECUTE_READ      : MemFlags |= PAG_EXECUTE | PAG_READ;  break;
        case PAGE_EXECUTE_READWRITE : MemFlags |= PAG_EXECUTE | PAG_READ | PAG_WRITE;  break;
        case PAGE_EXECUTE_WRITECOPY : MemFlags |= PAG_EXECUTE | PAG_READ | PAG_WRITE;  break;
        }

    if (Protection & PAGE_GUARD) {
        MemFlags |= PAG_GUARD;
        }

    try {
        //
        // Must specify a non-zero region size to begin with
        //

        if (*RegionSize == 0) {
            return ERROR_INVALID_PARAMETER;
            }

        //
        // See if the specified region size is too large.  Either because
        // the base address was not the actual base address or the region
        // size given was greater than the actual region size.
        //

        OldEndAddress = OriginalBaseAddress + *RegionSize;
        NewEndAddress = (ULONG)MemoryInformation.BaseAddress +
                        MemoryInformation.RegionSize;

        if (OldEndAddress > NewEndAddress) {
            *RegionSize = NewEndAddress - OriginalBaseAddress;
            }
        else
        if (*RegionSize > MemoryInformation.RegionSize) {
            *RegionSize = MemoryInformation.RegionSize;
            }

        //
        // Return the calculated flags for the region.
        //

        *Flags = MemFlags;
        }
    except( EXCEPTION_EXECUTE_HANDLER ) {
       Od2ExitGP();
        }
    return( NO_ERROR );
}