aboutsummaryrefslogtreecommitdiff
path: root/src/vendorcode/amd/agesa/Legacy/bridge32.inc
blob: 8685c6c077775c69b70044fca83adc6048fe088f (plain)
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
; ****************************************************************************
; *
; * @file
; *
; * Agesa structures and definitions
; *
; * Contains AMD AGESA core interface
; *
; * @xrefitem bom "File Content Label" "Release Content"
; * @e project:      AGESA
; * @e sub-project:  Include
; * @e \$Revision: 23714 $   @e \$Date: 2009-12-10 07:28:37 +0800 (Thu, 10 Dec 2009) $
;
;*****************************************************************************
; 
;  Copyright (c) 2011, Advanced Micro Devices, Inc.
;  All rights reserved.
;  
;  Redistribution and use in source and binary forms, with or without
;  modification, are permitted provided that the following conditions are met:
;      * Redistributions of source code must retain the above copyright
;        notice, this list of conditions and the following disclaimer.
;      * Redistributions in binary form must reproduce the above copyright
;        notice, this list of conditions and the following disclaimer in the
;        documentation and/or other materials provided with the distribution.
;      * Neither the name of Advanced Micro Devices, Inc. nor the names of 
;        its contributors may be used to endorse or promote products derived 
;        from this software without specific prior written permission.
;  
;  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
;  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;  DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
;  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
;  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
;  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;  
;*****************************************************************************

PARAM1          textequ <[bp+8]>
PARAM2          textequ <[bp+12]>
PARAM3          textequ <[bp+16]>
RETAddress      textequ <[bp+4]>

AMD_PRIVATE_PARAMS STRUCT
    Gate16_CS   DW ?                ; Segment of AMD_BRIDGE_32 and AMD_CALLOUT_16
    Gate16_SS   DW ?                ; RM stack segment
    Router_Seg  DW ?                ; Segment of oem router
    Router_Off  DW ?                ; Offset of oem router
AMD_PRIVATE_PARAMS ENDS

; OEM may pre-define the GDT and selector offsets. If they do not, use our defaults.
IFNDEF  AGESA_SELECTOR_GDT
        AGESA_SELECTOR_GDT         EQU 00h
ENDIF
IFNDEF  AGESA_SELECTOR_CODE16
        AGESA_SELECTOR_CODE16      EQU 08h
ENDIF
IFNDEF  AGESA_SELECTOR_DATA16
        AGESA_SELECTOR_DATA16      EQU 10h
ENDIF
IFNDEF  AGESA_SELECTOR_CODE32
        AGESA_SELECTOR_CODE32      EQU 18h
ENDIF
IFNDEF  AGESA_SELECTOR_DATA32
        AGESA_SELECTOR_DATA32      EQU 20h
ENDIF


AMD_BRIDGE_32_GDT MACRO  GDT_Name:REQ

        GDT_Name LABEL BYTE
            DD 000000000h, 000000000h       ; NULL descriptor
            DD 00000ffffh, 000009b00h       ; 16-bit code, fixed up
            DD 00000ffffh, 000009300h       ; 16-bit data, fixed up
            DD 00000ffffh, 000CF9B00h       ; 32-bit protected mode code
            DD 00000ffffh, 000CF9300h       ; 32-bit protected mode data
        GDT_Length EQU ($-GDT_Name)

ENDM

;+-------------------------------------------------------------------------
;
;       AMD_BRIDGE_32 - Execute Agesa through Pushhigh interface
;
;   Processing:
;       The following steps are taken:
;       1) Enter 32bit Protected Mode (PM32)
;       2) Run AGESA code
;       3) Restore Real Mode (RM)
;
; Entry:
;       [big real mode] : ds, es set to base 0 limit 4G segment
;       EDX - if not 0, provides a FAR PTR to oem router (Seg | Offset)
;       ESI - configuration block pointer
;
; Exit:
;       EAX - return value
;       ESI - configuration block pointer
;       ds, es, fs, gs - Set to 4GB segment limit for Big Real Mode
;
; Modified:
;      None
;

AMD_BRIDGE_32 MACRO GDT_Name

        local   copyGDT
        local   flushTo16PM
        local   agesaReturnAddress
        local   leave32bitPM
        local   flush2RM

        push    gs
        push    fs
        push    ebx
        push    ecx
        push    edi
        mov     eax, esp
        push    eax
        movzx   esp, sp
;
; Do not use any locals here, BP will be changed frequently during RM->PM32->RM
;
        pushf
        cli                         ; Disable interrupts during AGESA
        cld                         ; Need known direction flag during AGESA

;
; Save the FAR PTR input parameter
;
        mov     gs, dx                  ; Offset
        shr     edx, 16
        mov     fs, dx                  ; Segment
;
; Determine where our binary file is and get entry point
;
        mov     edx, (AMD_CONFIG_PARAMS PTR [esi]).ImageBasePtr
        add     edx, (AMD_IMAGE_HEADER PTR [edx]).EntryPointAddress
;
; Figure out the return address we will use after calling AGESA
; and store it in ebx until we have our stack set up properly
;
        mov     ebx, cs
        shl     ebx, 4
        add     ebx, OFFSET agesaReturnAddress
;
; Save our current RM stack AND entry EBP
;
        push    ebp
;       push    esp
        push    ss

;
; BEGIN --- STACK MUST BE BALANCED AT THIS POINT --- BEGIN
;
; Copy the GDT onto the stack for modification
;
        mov     cx, GDT_Length
        sub     sp, cx
        mov     bp, sp
        lea     di, GDT_Name
copyGDT:
        mov     al, cs:[di]
        mov     [bp], al
        inc     di
        inc     bp
        loop    copyGDT
;
; Patch 16-bit code and data descriptors on stack.  We will
; fix up CS and SS for PM16 during the callout if applicable.
;
        mov     bp, sp

        mov     eax, cs
        shl     eax, 4
        mov     [bp+AGESA_SELECTOR_CODE16+2], ax
        shr     eax, 16
        mov     [bp+AGESA_SELECTOR_CODE16+4], al

        mov     eax, ss
        shl     eax, 4
        mov     [bp+AGESA_SELECTOR_DATA16+2], ax
        shr     eax, 16
        mov     [bp+AGESA_SELECTOR_DATA16+4], al
;
; Need to place Length and Address on GDT
;
        mov     eax, ss
        shl     eax, 4
        add     eax, esp
        push    eax
        push    WORD PTR (GDT_Length-1)
;
; Load the GDT
;
        mov     bp, sp
        lgdt    FWORD PTR [bp]
;
; TABLE 1
;
; Place PRIVATE DATA on stack DIRECTLY following GDT
; During this routine, stack data is critical.  If
; order is changed or additional added, bad things
; will happen!
;
; HIGHEST PHYSICAL ADDRESS
;
; | ...                  |
; ------------------------
; | old RM SP            |
; | old RM SS            |
; ------------------------ sp + SIZEOF AMD_PRIVATE_PARAMS + (SIZEOF GDT_LENGTH + 6 {size, address})
; | GDT_DATA32           |
; | ...                  |
; | GDT_NULL             |
; | GDT Addr, Length     |
; ------------------------ sp + SIZEOF AMD_PRIVATE_PARAMS
; | Priv.Gate16_SS       |
; | Priv.Gate16_CS       |
; ------------------------ sp
; ------ THEN PUSH -------
; | Return to 16-bit CS  |
; | Return to 16-bit Off |
; | ...                  |
;
; LOWEST  PHYSICAL ADDRESS
;
        mov     edi, esp
        sub     edi, SIZEOF AMD_PRIVATE_PARAMS
        mov     ax, cs
        mov     (AMD_PRIVATE_PARAMS PTR ss:[edi]).Gate16_CS, ax
        mov     ax, ss
        mov     (AMD_PRIVATE_PARAMS PTR ss:[edi]).Gate16_SS, ax
        mov     (AMD_PRIVATE_PARAMS PTR ss:[edi]).Router_Off, gs
        mov     (AMD_PRIVATE_PARAMS PTR ss:[edi]).Router_Seg, fs

        mov     esp, edi
;
; Save an address for returning to 16 bit real mode on stack,
; we'll use it in a far ret after turning off CR0.PE so that
; we can take our address off and force a far jump.  Be sure
; no unexpected data is on the stack after this!
;
        mov     ax, cs
        push    cs
        lea     ax, flush2RM
        push    ax
;
; Convert ss:esp to "flat"
;

        mov     ax, sp
        push    ax
        mov     eax, ss
        shl     eax, 4
        add     eax, esp
        mov     esp, eax                ; Load the zero based ESP

;
; Set CR0.PE
;
        mov     eax, CR0                ; Get CPU control word 0
        or      al, 01                  ; Enable CPU protected mode
        mov     CR0, eax                ; Write back to CPU control word 0
        jmp     flushTo16PM

flushTo16PM:
;
; 16-bit protected mode
;
        mov     ax, AGESA_SELECTOR_DATA32
        mov     ds, ax
        mov     es, ax
        mov     fs, ax
        mov     gs, ax
        mov     ss, ax
;
; Push our parameters RIGHT TO LEFT, and then return address
;
        push    esi                     ; AGESA configuration block pointer (data)
        push    ebx                     ; after AGESA return offset (32PM flat) - consumed by dispatcher ret
        pushd   AGESA_SELECTOR_CODE32   ; AGESA entry selector (32PM flat)
        push    edx                     ; AGESA entry point (32PM flat)

        DB      066h
        retf                            ; <><><> Enter AGESA 32-bit code!!! <><><>

agesaReturnAddress:
;
; Returns from the Agesa 32-bit code still PM32
;
        DB      0EAh
        DD      OFFSET leave32bitPM
        DW      AGESA_SELECTOR_CODE16

leave32bitPM:
;
; Now in 16-bit PM
;
        add     esp, 4                  ; +4 to remove our config block pointer
;
; Eax reserve AGESA_STATUS return code, save it
;
        mov     ebx, eax
;
; Turn off CR0.PE, restore 64K stack limit
;
        pop     ax
        mov     sp, ax
        mov     ax, AGESA_SELECTOR_DATA16
        mov     ss, ax

        mov     eax, CR0
        and     al, NOT 1               ; Disable protected mode
        mov     CR0, eax                ; Write back CR0.PE
;
; Jump far to enter RM, we saved this address on the stack
; already.  Hopefully stack is balanced through AGESA
; nor were any params added by pushing them on the stack and
; not removing them between BEGIN-END comments.
;
        retf

flush2RM:
;
; Set segments registers for big real mode before returning
;
        xor     ax, ax
        mov     ds, ax
        mov     es, ax
        mov     fs, ax
        mov     gs, ax
;
; Discard GDT, +6 for GDT pointer/size, privates
;
        add     esp, GDT_Length + 6 + SIZEOF AMD_PRIVATE_PARAMS
;
; Restore real mode stack and entry EBP
;
        pop     cx
;       mov     esp, [esp]
        mov     ss, cx
        pop     ebp
;
; Restore AGESA_STATUS return code to eax
;
        mov     eax, ebx
;
; END --- STACK MUST BE BALANCED TO THIS POINT --- END
;

        popf
        pop     ebx
        mov     esp, ebx
        pop     edi
        pop     ecx
        pop     ebx
        pop     fs
        pop     gs
                            ; EXIT AMD_BRIDGE_32
ENDM
;+-------------------------------------------------------------------------
;
; AMD_CALLOUT_16 - Execute Callback from Pushhigh interface
;
;   Processing:
;       The following steps are taken:
;       1) Enter PM16
;       2) Setup stack, get private params
;       3) Enter RM
;       4) Get 3 params
;       5) Call oemCallout OR oem router
;       6) Enter PM32
;       7) Return to Agesa PH
;
; Entry:
;       [32-bit protected mode]
;       [esp+8] Func
;       [esp+12] Data
;       [esp+16] Configuration Block
;       [esp+4] return address to Agesa
;
; Exit:
;       [32-bit protected mode]
;
; Modified:
;     None
;
AMD_CALLOUT_16 MACRO  LocalOemCalloutRouter
;
; Note that we are still PM32, so MASM may work strangely
;

        push    bp                     ; Save our original SP to access params
        mov     bp, sp
        push    bx
        push    si
        push    di
        push    cx
        push    dx
        push    di

        DB      066h, 0EAh
        DW      OFFSET PM16Entry
        DW      AGESA_SELECTOR_CODE16

PM16Entry:
;
; PM16 CS, but still PM32 SS, as we need to access our private params
; before we enter RM.
;
; Note: we are working below the stack temporarily, and and it will
; not affect our ability to get entry params
;
        xor     ecx, ecx
        xor     edx, edx
;
; SGDT will give us the original location of the GDT on our CAS stack.
; We need this value because our private parameters are located just
; below the GDT.
;
        mov     edi, esp
        sub     edi, GDT_Length + 6
        sgdt    FWORD PTR [edi]        ; [edi] = word size, dword address
        mov     edi, DWORD PTR [edi+2]  ; Get the PM32 address only
        sub     edi, SIZEOF AMD_PRIVATE_PARAMS + 6
;
; cx = code segment of this code in RM
; dx = stack segment of CAS in RM
; fs = code segment of oem router (save for later)
; gs = offset of oem router (save for later)
; fs and gs are loaded after switch to real mode because we can't
; use them as scratch pad registers in protected mode
;
        mov     cx, (AMD_PRIVATE_PARAMS PTR ss:[edi]).Gate16_CS
        mov     dx, (AMD_PRIVATE_PARAMS PTR ss:[edi]).Gate16_SS

        mov     eax, edi             ; Save edi in eax for after RM switch
        mov     edi, esp             ; Save our current ESP for RM

        movzx   ebx, dx
        shl     ebx, 4
        sub     esp, ebx

;
; We had been accessing the stack in PM32, we will now change to PM16 so we
; will make the stack segment 64KB limit so SP needs to be fixed made PM16
; compatible.
;
        mov     bx, AGESA_SELECTOR_DATA16
        mov     ss, bx

;
; Save the RM segment and RM offset of the jump we will need to make in
; order to enter RM so that code in this segment is relocatable.
;
; BEGIN --- Don't unbalance the stack --- BEGIN
;
        push    cx
        pushw   OFFSET RMEntry

        mov     ebx, CR0
        and     bl, NOT 1
        mov     CR0, ebx                ; CR0.PE cleared
;
; Far jump to clear segment descriptor cache and enter RM
;
        retf

RMEntry:
;
; We are in RM, setup RM stack
;
        movzx   ebx, dx               ; Get RM SS in ebx
        shl     ebx, 4                  ; Get our stack top on entry in EBP to
        sub     ebp, ebx                ; access our entry parameters
        sub     eax, ebx                ; save copy of parameters address
        mov     ss, dx                  ; Set stack segment
;
; We are going to figure out the address to use when we return
; and have to go back into PM32 while we have access to it
;
        movzx   ebx, cx               ; Get original CS in ebx
        shl     ebx, 4
        add     ebx, OFFSET PM32Entry
;
; Now we put our data, func, block params into calling convention
; for our hook
;
; ECX = Func
; EDX = Data
; ESI = config pointer
;
        mov     ecx, PARAM1             ; Func
        mov     edx, PARAM2             ; Data
        mov     esi, PARAM3             ; pointer

        push    ebx                    ; Save PM32 mode switch address
        push    edi                    ; Save PM32 stack pointer
        pushf
;
; Get Router Function Address
;
        mov     edi, eax
        mov     ax, (AMD_PRIVATE_PARAMS PTR ss:[edi]).Router_Seg
        mov     fs, ax
        mov     ax, (AMD_PRIVATE_PARAMS PTR ss:[edi]).Router_Off
        mov     gs, ax

        mov     eax, AGESA_UNSUPPORTED  ; Default return value
;
; If AMD_BRIDGE_32 EDX == 0 call oemCallout
; otherwise call FAR PTR EDX
;
; Critical:
;   sp+2 - EDI aka PM32 stack address
;   sp+4 - address of PM32Entry in PM32
;
        mov     bx, fs
        shl     ebx, 16
        mov     bx, gs

        .if (ebx == 0)
            call        LocalOemCalloutRouter
        .else
;
; Make far call to Router function
;
        push    cs
        push    offset CalloutReturn
        push    ebx
        retf
CalloutReturn:
    .endif
;
; Restore PM32 esp from RM stack
;
        popf
        pop     edi                     ; Our PM32 stack pointer
        pop     edx                     ; Our PM32 mode switch address

        mov     ebx, CR0
        or      bl, 1                   ; CR0.PE set
        mov     CR0, ebx

        mov     ebx, AGESA_SELECTOR_DATA32
        pushd   AGESA_SELECTOR_CODE32 ; PM32 selector
        push    edx                    ; PM32 entry point

        DB      066h
        retf                        ; Far jump to enter PM32

PM32Entry:
;
; END --- Don't unbalance the stack --- END
; We are now PM32, so remember MASM is assembling in 16-bit again
;
        mov     ss, bx
        mov     ds, bx
        mov     es, bx
        mov     fs, bx
        mov     gs, bx

        mov     sp, di
        pop     di
        pop     dx
        pop     cx
        pop     di
        pop     si
        pop     bx
        pop     bp
                            ; EXIT AMD_CALLOUT_16
ENDM