Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Rudolf Marek <r.marek@assembler.cz> |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009 coresystems GmbH |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 15 | */ |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 16 | |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 17 | #define WAKEUP_BASE 0x600 |
| 18 | #define RELOCATED(x) (x - __wakeup + WAKEUP_BASE) |
| 19 | |
| 20 | /* CR0 bits */ |
| 21 | #define PE (1 << 0) |
| 22 | |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 23 | #ifdef __x86_64__ |
| 24 | .code64 |
| 25 | #else |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 26 | .code32 |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 27 | #endif |
| 28 | |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 29 | .globl __wakeup |
| 30 | __wakeup: |
Stefan Reinauer | ac901e6 | 2015-07-31 16:46:28 -0700 | [diff] [blame] | 31 | #ifdef __x86_64__ |
| 32 | .intel_syntax noprefix |
| 33 | xor rax,rax |
| 34 | mov ax, ss |
| 35 | push rax |
| 36 | mov rax, rsp |
| 37 | add rax, 8 |
| 38 | push rax |
| 39 | pushfq |
| 40 | push 0x10 |
| 41 | lea rax,[rip+3] |
| 42 | push rax |
| 43 | iretq |
| 44 | |
| 45 | .code32 |
| 46 | |
| 47 | /* disable paging */ |
| 48 | mov eax, cr0 |
| 49 | btc eax, 31 |
| 50 | mov cr0, eax |
| 51 | |
| 52 | /* disable long mode */ |
| 53 | mov ecx, 0xC0000080 |
| 54 | rdmsr |
| 55 | btc eax, 8 |
| 56 | wrmsr |
| 57 | .att_syntax prefix |
| 58 | #endif |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 59 | /* First prepare the jmp to the resume vector */ |
| 60 | mov 0x4(%esp), %eax /* vector */ |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 61 | /* last 4 bits of linear addr are taken as offset */ |
| 62 | andw $0x0f, %ax |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 63 | movw %ax, (__wakeup_offset) |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 64 | mov 0x4(%esp), %eax |
| 65 | /* the rest is taken as segment */ |
| 66 | shr $4, %eax |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 67 | movw %ax, (__wakeup_segment) |
| 68 | |
| 69 | /* Then overwrite coreboot with our backed up memory */ |
Stefan Reinauer | 8acbc2a | 2011-11-17 13:03:38 -0800 | [diff] [blame] | 70 | cld |
| 71 | movl 8(%esp), %esi |
| 72 | movl 12(%esp), %edi |
| 73 | movl 16(%esp), %ecx |
| 74 | shrl $2, %ecx |
| 75 | rep movsl |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 76 | |
| 77 | /* Activate the right segment descriptor real mode. */ |
| 78 | ljmp $0x28, $RELOCATED(1f) |
| 79 | 1: |
| 80 | .code16 |
| 81 | /* 16 bit code from here on... */ |
| 82 | |
| 83 | /* Load the segment registers w/ properly configured |
| 84 | * segment descriptors. They will retain these |
| 85 | * configurations (limits, writability, etc.) once |
| 86 | * protected mode is turned off. |
| 87 | */ |
| 88 | mov $0x30, %ax |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 89 | mov %ax, %ds |
| 90 | mov %ax, %es |
| 91 | mov %ax, %fs |
| 92 | mov %ax, %gs |
| 93 | mov %ax, %ss |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 94 | |
| 95 | /* Turn off protection */ |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 96 | movl %cr0, %eax |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 97 | andl $~PE, %eax |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 98 | movl %eax, %cr0 |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 99 | |
| 100 | /* Now really going into real mode */ |
| 101 | ljmp $0, $RELOCATED(1f) |
| 102 | 1: |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 103 | movw $0x0, %ax |
| 104 | movw %ax, %ds |
| 105 | movw %ax, %es |
| 106 | movw %ax, %ss |
| 107 | movw %ax, %fs |
| 108 | movw %ax, %gs |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 109 | |
| 110 | /* This is a FAR JMP to the OS waking vector. The C code changed |
| 111 | * the address to be correct. |
| 112 | */ |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 113 | .byte 0xea |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 114 | |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 115 | __wakeup_offset = RELOCATED(.) |
| 116 | .word 0x0000 |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 117 | |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 118 | __wakeup_segment = RELOCATED(.) |
| 119 | .word 0x0000 |
Rudolf Marek | 33cafe5 | 2009-04-13 18:07:02 +0000 | [diff] [blame] | 120 | |
Stefan Reinauer | c0ac7e9 | 2009-11-10 22:17:15 +0000 | [diff] [blame] | 121 | .globl __wakeup_size |
Aaron Durbin | a146d58 | 2013-02-08 16:56:51 -0600 | [diff] [blame] | 122 | __wakeup_size: |
| 123 | .long . - __wakeup |