summaryrefslogtreecommitdiffstats
path: root/private/mvdm/ieuvddex/pmode.c
blob: fee8eea5eed6ad1151285f68ed58cfc981aefe37 (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
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    pmode.c

Abstract:

    This file contains 386 code for debugger extension associated with
    segemented protected mode.

Author:

    Dave Hastings (daveh) 3-Apr-1992

Revision History:

--*/

#include <ieuvddex.h>
#include <stdio.h>

//
// Local constants
//

#define SELECTOR_LDT            0x04
#define SELECTOR_RPL            0x03
#define SELECTOR_SYSTEM         0x10
#define SELECTOR_CODE           0x08
#define SELECTOR_CONFORMEXPAND  0x04
#define SELECTOR_READWRITE      0x02
#define SELECTOR_ACCESSED       0x01

VOID
Selp(
    IN HANDLE CurrentProcess,
    IN HANDLE CurrentThread,
    IN LPSTR ArgumentString
    )
/*++

Routine Description:

    This routine dumps LDT selectors.  The selectors are dumped from the
    user mode Ldt, rather than the system ldt.

Arguments:

    CurrentProcess -- Supplies a handle to the process to dump selectors for
    CurrentThread -- Supplies a handle to the thread to dump selectors for
    ArgumentString -- Supplies the arguments to the !sel command

Return Value

    None.

--*/
{
    BOOL Status;
    ULONG BytesRead;
    LONG l;
    ULONG Selector, Number, ul, Index;
    VDMLDT_ENTRY LdtEntries[16];
    PVOID LdtAddress;

    UNREFERENCED_PARAMETER(CurrentThread);

    //
    // Get starting selector number and number of selectors
    //

    if ((l = sscanf(ArgumentString, "%lx l%lx", &Selector, &Number)) < 1) {
        (*Print)("Starting selector number must be specified\n");
        return;
    }

    if (l == 1) {
        Number = 16;
    }

    //
    // Check starting selector for LDT/GDT, and remove rpl info
    //

    if (!(Selector & SELECTOR_LDT)) {
        (*Print)("Starting selector must be an ldt selector\n");
        return;
    }

    Selector &= ~(SELECTOR_LDT | SELECTOR_RPL);

    //
    // Get address of Ldt
    //

    LdtAddress = (PVOID)(*GetExpression)(
        "ntvdm!Ldt"
        );

    if (!LdtAddress) {
        (*Print)("Error geting Ldt address\n");
        return;
    }

    Status = ReadProcessMem(
            CurrentProcess,
            LdtAddress,
            &LdtAddress,
            sizeof(ULONG),
            &BytesRead
            );

    if ((!Status) || (BytesRead != sizeof(ULONG))) {
        GetLastError();
        (*Print)("Error reading the address of LDT table\n");
        return;
    }

    (PUCHAR)LdtAddress += Selector;

    //
    // Read and dump selectors until the desired number have been done
    //

    while (Number > 0) {
        if (Number > 16) {
            ul = 16;
        } else {
            ul = Number;
        }

        Status = ReadProcessMem(
            CurrentProcess,
            LdtAddress,
            LdtEntries,
            ul * sizeof(VDMLDT_ENTRY),
            &BytesRead
            );

        if ((!Status) || (BytesRead != ul * sizeof(VDMLDT_ENTRY))) {
            GetLastError();
            (*Print)("Error reading selectors\n");
            return;
        }

        for (Index = 0; Index < ul; Index++) {
            PrintDescriptor(
                CurrentProcess,
                &LdtEntries[Index],
                Selector + Index * sizeof(VDMLDT_ENTRY)
                );
        }

        Number -= ul;
        (PUCHAR)LdtAddress += ul * sizeof(VDMLDT_ENTRY);
        Selector += ul * sizeof(VDMLDT_ENTRY);
    }
}

VOID
PrintDescriptor(
    IN HANDLE hProcess,
    IN LPVDMLDT_ENTRY Descriptor,
    IN ULONG Selector
    )
/*++

Routine Description:

    This routine prints out the contents of a descriptor

Arguments:

    Descriptor -- Supplies a pointer to the descriptor to dump

Return Value:

    None.

--*/
{
    ULONG Base, Limit;

    Base = (Descriptor->HighWord.Bytes.BaseHi << 24)
        + (Descriptor->HighWord.Bytes.BaseMid << 16)
        + (Descriptor->BaseLow);

    Limit = (Descriptor->HighWord.Bits.LimitHi << 16)
        + (Descriptor->LimitLow);

#ifdef i386
    (*Print)("%04x  Base=%08lx  Limit=%08lx", Selector, Base, Limit);
#else
    (*Print)("%04x  Base=%08lx (%08lx)  Limit=%08lx", Selector, Base, Base+GetIntelBase(hProcess), Limit);
#endif

    if (Descriptor->HighWord.Bits.Granularity) {
        (*Print)(" Pages ");
    } else {
        (*Print)(" Bytes ");
    }

    (*Print)("DPL=%x", Descriptor->HighWord.Bits.Dpl);

    if (Descriptor->HighWord.Bits.Pres) {
        (*Print)("  P ");
    } else {
        (*Print)(" NP ");
    }

    if (!(Descriptor->HighWord.Bits.Type & SELECTOR_SYSTEM)) {
        (*Print)("System");
    } else {
        if (Descriptor->HighWord.Bits.Type & SELECTOR_CODE) {
            (*Print)("Code  ");
            if (Descriptor->HighWord.Bits.Type & SELECTOR_CONFORMEXPAND) {
                (*Print)("C");
            } else {
                (*Print)(" ");
            }
            if (Descriptor->HighWord.Bits.Type & SELECTOR_READWRITE) {
                (*Print)("R");
            } else {
                (*Print)(" ");
            }
        } else {
            (*Print)("Data  ");
            if (Descriptor->HighWord.Bits.Type & SELECTOR_CONFORMEXPAND) {
                (*Print)("E");
            } else {
                (*Print)(" ");
            }
            if (Descriptor->HighWord.Bits.Type & SELECTOR_READWRITE) {
                (*Print)("W");
            } else {
                (*Print)(" ");
            }
        }
        if (Descriptor->HighWord.Bits.Type & SELECTOR_ACCESSED) {
            (*Print)(" A");
        } else {
            (*Print)("  ");
        }
        if (Descriptor->HighWord.Bits.Default_Big) {
            (*Print)(" Big");
        }
    }


    (*Print)("\n");
}

ULONG GetIntelBase(
    HANDLE      hCurrentProcess
    )
{
#ifndef i386
    ULONG IntelBase;
    BOOL        b;

    IntelBase = (ULONG)(*GetExpression)("ntvdm!Start_of_M_area");
    if ( IntelBase ) {
        b = ReadProcessMem( hCurrentProcess,
                               (LPVOID) IntelBase, &IntelBase,
                               sizeof(ULONG), NULL );
        if ( !b ) {
            (*Print)("Could not read symbol 'ntvdm!Start_of_M_area\n");
            return(0);
        }

    } else {
        (*Print)("Could not find the symbol 'ntvdm!Start_of_M_area'\n");
    }

    return(IntelBase);
#else
    return(0);
#endif
}