summaryrefslogtreecommitdiffstats
path: root/private/ntos/rtl/alpha/context.c
blob: dfcf0aa1eab5b7c72eeb8ba16801713445a76f2e (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
/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    context.c

Abstract:

    This module implements user-mode callable context manipulation routines.

Author:

    Mark Lucovsky (markl) 20-Jun-1989

Revision History:

    David N. Cutler (davec) 18-Apr-1990

        Revise for MIPS environment.

    Thomas Van Baak (tvb) 11-May-1992

        Adapted for Alpha AXP.

--*/

#include <nt.h>
#include <ntrtl.h>
#include <alphaops.h>

VOID
RtlInitializeContext(
    IN HANDLE Process,
    OUT PCONTEXT Context,
    IN PVOID Parameter OPTIONAL,
    IN PVOID InitialPc OPTIONAL,
    IN PVOID InitialSp OPTIONAL
    )

/*++

Routine Description:

    This function initializes a context structure so that it can be used in
    a subsequent call to NtCreateThread.

Arguments:

    Process - Supplies an open handle to the target process. This argument
        is ignored by this function.

    Context - Supplies a pointer to a context record that is to be initialized.

    Parameter - Supplies an initial value for register A0.

    InitialPc - Supplies an initial program counter value.

    InitialSp - Supplies an initial stack pointer value.

Return Value:

    Raises STATUS_BAD_INITIAL_STACK if the value of InitialSp is not properly
           aligned.

    Raises STATUS_BAD_INITIAL_PC if the value of InitialPc is not properly
           aligned.

--*/

{

    //
    // Check for proper initial stack and PC alignment.
    //

    if (((ULONG)InitialSp & 0xF) != 0) {
        RtlRaiseStatus(STATUS_BAD_INITIAL_STACK);
    }
    if (((ULONG)InitialPc & 0x3) != 0) {
        RtlRaiseStatus(STATUS_BAD_INITIAL_PC);
    }

    //
    // Initialize the integer registers to contain their register number
    // (they must be initialized and using register numbers instead of 0
    // can be useful for debugging). Integer registers a0, ra, gp, and sp
    // are set later.
    //

    Context->IntV0 = 0;
    Context->IntT0 = 1;
    Context->IntT1 = 2;
    Context->IntT2 = 3;
    Context->IntT3 = 4;
    Context->IntT4 = 5;
    Context->IntT5 = 6;
    Context->IntT6 = 7;
    Context->IntT7 = 8;
    Context->IntS0 = 9;
    Context->IntS1 = 10;
    Context->IntS2 = 11;
    Context->IntS3 = 12;
    Context->IntS4 = 13;
    Context->IntS5 = 14;
    Context->IntFp = 15;
    Context->IntA1 = 17;
    Context->IntA2 = 18;
    Context->IntA3 = 19;
    Context->IntA4 = 20;
    Context->IntA5 = 21;
    Context->IntT8 = 22;
    Context->IntT9 = 23;
    Context->IntT10 = 24;
    Context->IntT11 = 25;
    Context->IntT12 = 27;
    Context->IntAt = 28;

    //
    // Initialize the floating point registers to contain the integer value
    // of their register number (they must be initialized and using register
    // numbers instead of 0 can be useful for debugging).
    //

    Context->FltF0 = 0;
    Context->FltF1 = 1;
    Context->FltF2 = 2;
    Context->FltF3 = 3;
    Context->FltF4 = 4;
    Context->FltF5 = 5;
    Context->FltF6 = 6;
    Context->FltF7 = 7;
    Context->FltF8 = 8;
    Context->FltF9 = 9;
    Context->FltF10 = 10;
    Context->FltF11 = 11;
    Context->FltF12 = 12;
    Context->FltF13 = 13;
    Context->FltF14 = 14;
    Context->FltF15 = 15;
    Context->FltF16 = 16;
    Context->FltF17 = 17;
    Context->FltF18 = 18;
    Context->FltF19 = 19;
    Context->FltF20 = 20;
    Context->FltF21 = 21;
    Context->FltF22 = 22;
    Context->FltF23 = 23;
    Context->FltF24 = 24;
    Context->FltF25 = 25;
    Context->FltF26 = 26;
    Context->FltF27 = 27;
    Context->FltF28 = 28;
    Context->FltF29 = 29;
    Context->FltF30 = 30;
    Context->FltF31 = 0;

    //
    // Initialize the control registers.
    //
    // Gp: will be set in LdrpInitialize at thread startup.
    // Ra: some debuggers compare for 1 as a top-of-stack indication.
    //
    // N.B. ULONG becomes canonical longword with (ULONGLONG)(LONG) cast.
    //

    Context->IntGp = 0;
    Context->IntSp = (ULONGLONG)(LONG)InitialSp;
    Context->IntRa = 1;
    Context->Fir = (ULONGLONG)(LONG)InitialPc;

    //
    // Set default Alpha floating point control register values.
    //

    Context->Fpcr = (ULONGLONG)0;
    ((PFPCR)(&Context->Fpcr))->DynamicRoundingMode = ROUND_TO_NEAREST;
    Context->SoftFpcr = (ULONGLONG)0;

    Context->Psr = 0;
    Context->ContextFlags = CONTEXT_FULL;

    //
    // Set the initial context of the thread in a machine specific way.
    //

    Context->IntA0 = (ULONGLONG)(LONG)Parameter;
}

NTSTATUS
RtlRemoteCall(
    HANDLE Process,
    HANDLE Thread,
    PVOID CallSite,
    ULONG ArgumentCount,
    PULONG Arguments,
    BOOLEAN PassContext,
    BOOLEAN AlreadySuspended
    )

/*++

Routine Description:

    This function calls a procedure in another thread/process by using
    NtGetContext and NtSetContext. Parameters are passed to the target
    procedure via the nonvolatile registers (s0 - s5).

Arguments:

    Process - Supplies an open handle to the target process.

    Thread - Supplies an open handle to the target thread within the target
        process.

    CallSite - Supplies the address of the procedure to call in the target
        process.

    ArgumentCount - Supplies the number of 32 bit parameters to pass to the
        target procedure.

    Arguments - Supplies a pointer to the array of 32 bit parameters to pass.

    PassContext - Supplies a boolean value that determines whether a parameter
        is to be passed that points to a context record. This parameter is
        ignored on MIPS and Alpha hosts.

    AlreadySuspended - Supplies a boolean value that determines whether the
        target thread is already in a suspended or waiting state.

Return Value:

    Status - Status value.

--*/

{

    NTSTATUS Status;
    CONTEXT Context;
    ULONG Index;
    ULONGLONG NewSp;

    if ((ArgumentCount > 6) ||
        (PassContext && (ArgumentCount > 5))) {
        return(STATUS_INVALID_PARAMETER);
    }

    //
    // If necessary, suspend the target thread before getting the thread's
    // current state.
    //

    if (AlreadySuspended == FALSE) {
        Status = NtSuspendThread(Thread, NULL);
        if (NT_SUCCESS(Status) == FALSE) {
            return(Status);
        }
    }

    //
    // Get the current state of the target thread.
    //

    Context.ContextFlags = CONTEXT_FULL;
    Status = NtGetContextThread(Thread, &Context);
    if (NT_SUCCESS(Status) == FALSE) {
        if (AlreadySuspended == FALSE) {
            NtResumeThread(Thread, NULL);
        }
        return(Status);
    }

    if (AlreadySuspended) {
        Context.IntV0 = STATUS_ALERTED;
    }

    //
    // Pass the parameters to the other thread via the non-volatile registers
    // s0 - s5. The context record is passed on the stack of the target thread.
    //

    NewSp = Context.IntSp - sizeof(CONTEXT);
    Status = NtWriteVirtualMemory(Process, (PVOID)NewSp, &Context,
                                  sizeof(CONTEXT), NULL);
    if (NT_SUCCESS(Status) == FALSE) {
        if (AlreadySuspended == FALSE) {
            NtResumeThread(Thread, NULL);
        }
        return(Status);
    }

    //
    // N.B. Each ULONG argument is converted to canonical form with the
    //      (ULONGLONG)(LONG) cast as required by the calling standard.
    //
    // N.B. Given the PULONG Arguments prototype, this function cannot pass
    //      quadword arguments (including structures such as LARGE_INTEGER).
    //

    Context.IntSp = NewSp;

    if (PassContext) {
        Context.IntS0 = NewSp;
        for (Index = 0; Index < ArgumentCount; Index += 1) {
            (&Context.IntS1)[Index] = (ULONGLONG)(LONG)Arguments[Index];
        }

    } else {
        for (Index = 0; Index < ArgumentCount; Index += 1) {
            (&Context.IntS0)[Index] = (ULONGLONG)(LONG)Arguments[Index];
        }
    }

    //
    // Set the address of the target code into FIR and set the thread context
    // to cause the target procedure to be executed.
    //
    // N.B. The PVOID CallSite is stored as a canonical longword in order
    //      for Fir to be a valid 64-bit address.
    //

    Context.Fir = (ULONGLONG)(LONG)CallSite;
    Status = NtSetContextThread(Thread, &Context);
    if (AlreadySuspended == FALSE) {
        NtResumeThread(Thread, NULL);
    }
    return(Status);
}