Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 ChromeOS Authors |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2 of |
| 9 | * the License. |
| 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. |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * The stub is a generic wrapper for bootstrapping a C-based SMM handler. Its |
| 19 | * primary purpose is to put the CPU into protected mode with a stack and call |
| 20 | * into the C handler. |
| 21 | * |
| 22 | * The stub_entry_params structure needs to correspond to the C structure |
| 23 | * found in smm.h. |
| 24 | */ |
| 25 | |
Aaron Durbin | 9e01a0b | 2017-06-16 14:12:55 -0500 | [diff] [blame] | 26 | #include <cpu/x86/cr.h> |
| 27 | |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 28 | .code32 |
| 29 | .section ".module_parameters", "aw", @progbits |
| 30 | stub_entry_params: |
| 31 | stack_size: |
| 32 | .long 0 |
| 33 | stack_top: |
| 34 | .long 0 |
| 35 | c_handler: |
| 36 | .long 0 |
| 37 | c_handler_arg: |
| 38 | .long 0 |
Aaron Durbin | 8ade68a | 2017-06-16 15:16:13 -0500 | [diff] [blame] | 39 | fxsave_area: |
| 40 | .long 0 |
| 41 | fxsave_area_size: |
| 42 | .long 0 |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 43 | /* struct smm_runtime begins here. */ |
| 44 | smm_runtime: |
| 45 | smbase: |
| 46 | .long 0 |
| 47 | save_state_size: |
| 48 | .long 0 |
Elyes HAOUAS | d82be92 | 2016-07-28 18:58:27 +0200 | [diff] [blame] | 49 | /* apic_to_cpu_num is a table mapping the default APIC id to CPU num. If the |
| 50 | * APIC id is found at the given index, the contiguous CPU number is index |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 51 | * into the table. */ |
| 52 | apic_to_cpu_num: |
| 53 | .fill CONFIG_MAX_CPUS,1,0xff |
| 54 | /* end struct smm_runtime */ |
| 55 | |
| 56 | .data |
Elyes HAOUAS | d82be92 | 2016-07-28 18:58:27 +0200 | [diff] [blame] | 57 | /* Provide fallback stack to use when a valid CPU number cannot be found. */ |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 58 | fallback_stack_bottom: |
| 59 | .skip 128 |
| 60 | fallback_stack_top: |
| 61 | |
Aaron Durbin | 9e01a0b | 2017-06-16 14:12:55 -0500 | [diff] [blame] | 62 | #define CR0_CLEAR_FLAGS \ |
| 63 | (CR0_CD | CR0_NW | CR0_PG | CR0_AM | CR0_WP | \ |
| 64 | CR0_NE | CR0_TS | CR0_EM | CR0_MP) |
| 65 | |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 66 | .text |
| 67 | .code16 |
Aaron Durbin | dde7629 | 2015-09-05 12:59:26 -0500 | [diff] [blame] | 68 | .global _start |
| 69 | _start: |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 70 | movl $(smm_relocate_gdt), %ebx |
Edward O'Callaghan | 4e2294b | 2017-01-08 19:14:42 +1100 | [diff] [blame] | 71 | lgdtl (%ebx) |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 72 | |
| 73 | movl %cr0, %eax |
Aaron Durbin | 9e01a0b | 2017-06-16 14:12:55 -0500 | [diff] [blame] | 74 | andl $~CR0_CLEAR_FLAGS, %eax |
| 75 | orl $CR0_PE, %eax |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 76 | movl %eax, %cr0 |
| 77 | |
| 78 | /* Enable protected mode */ |
Edward O'Callaghan | 4e2294b | 2017-01-08 19:14:42 +1100 | [diff] [blame] | 79 | ljmpl $0x8, $smm_trampoline32 |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 80 | |
| 81 | .align 4 |
| 82 | smm_relocate_gdt: |
| 83 | /* The first GDT entry is used for the lgdt instruction. */ |
| 84 | .word smm_relocate_gdt_end - smm_relocate_gdt - 1 |
| 85 | .long smm_relocate_gdt |
| 86 | .word 0x0000 |
| 87 | |
| 88 | /* gdt selector 0x08, flat code segment */ |
| 89 | .word 0xffff, 0x0000 |
| 90 | .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, 4GB limit */ |
| 91 | |
| 92 | /* gdt selector 0x10, flat data segment */ |
| 93 | .word 0xffff, 0x0000 |
| 94 | .byte 0x00, 0x93, 0xcf, 0x00 |
| 95 | smm_relocate_gdt_end: |
| 96 | |
| 97 | .align 4 |
| 98 | .code32 |
| 99 | .global smm_trampoline32 |
| 100 | smm_trampoline32: |
| 101 | /* Use flat data segment */ |
| 102 | movw $0x10, %ax |
| 103 | movw %ax, %ds |
| 104 | movw %ax, %es |
| 105 | movw %ax, %ss |
| 106 | movw %ax, %fs |
| 107 | movw %ax, %gs |
| 108 | |
| 109 | /* The CPU number is calculated by reading the initial APIC id. Since |
| 110 | * the OS can maniuplate the APIC id use the non-changing cpuid result |
| 111 | * for APIC id (ebx[31:24]). A table is used to handle a discontiguous |
| 112 | * APIC id space. */ |
| 113 | mov $1, %eax |
| 114 | cpuid |
| 115 | bswap %ebx /* Default APIC id in bl. */ |
| 116 | mov $(apic_to_cpu_num), %eax |
| 117 | xor %ecx, %ecx |
| 118 | |
| 119 | 1: |
| 120 | cmp (%eax, %ecx, 1), %bl |
| 121 | je 1f |
| 122 | inc %ecx |
| 123 | cmp $CONFIG_MAX_CPUS, %ecx |
| 124 | jne 1b |
Elyes HAOUAS | d82be92 | 2016-07-28 18:58:27 +0200 | [diff] [blame] | 125 | /* This is bad. One cannot find a stack entry because a CPU num could |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 126 | * not be assigned. Use the fallback stack and check this condition in |
| 127 | * C handler. */ |
| 128 | movl $(fallback_stack_top), %esp |
Aaron Durbin | 8ade68a | 2017-06-16 15:16:13 -0500 | [diff] [blame] | 129 | /* Clear fxsave location as there will be no saving/restoring. */ |
| 130 | xor %edi, %edi |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 131 | jmp 2f |
| 132 | 1: |
| 133 | movl stack_size, %eax |
Raul E Rangel | 0140541 | 2018-06-25 14:22:27 -0600 | [diff] [blame] | 134 | mul %ecx /* %eax(stack_size) * %ecx(cpu) = %eax(offset) */ |
Raul E Rangel | e18e5ab | 2018-06-25 14:22:27 -0600 | [diff] [blame^] | 135 | movl stack_top, %ebx |
| 136 | subl %eax, %ebx /* global_stack_top - offset = stack_top */ |
| 137 | mov %ebx, %esp |
Raul E Rangel | 8711568 | 2018-06-25 14:22:27 -0600 | [diff] [blame] | 138 | |
| 139 | /* Create stack frame by pushing a NULL stack base pointer */ |
| 140 | pushl $0x0 |
Raul E Rangel | 0140541 | 2018-06-25 14:22:27 -0600 | [diff] [blame] | 141 | mov %esp, %ebp |
| 142 | |
| 143 | /* Allocate locals (fxsave) */ |
| 144 | subl $0x4, %esp |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 145 | |
Aaron Durbin | 8ade68a | 2017-06-16 15:16:13 -0500 | [diff] [blame] | 146 | /* calculate fxsave location */ |
| 147 | mov fxsave_area, %edi |
| 148 | test %edi, %edi |
| 149 | jz 2f |
| 150 | movl fxsave_area_size, %eax |
| 151 | mul %ecx |
| 152 | add %eax, %edi |
| 153 | |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 154 | 2: |
Aaron Durbin | 8ade68a | 2017-06-16 15:16:13 -0500 | [diff] [blame] | 155 | /* Save location of fxsave area. */ |
Raul E Rangel | 0140541 | 2018-06-25 14:22:27 -0600 | [diff] [blame] | 156 | mov %edi, -4(%ebp) |
Aaron Durbin | 8ade68a | 2017-06-16 15:16:13 -0500 | [diff] [blame] | 157 | test %edi, %edi |
| 158 | jz 1f |
| 159 | |
| 160 | /* Enable sse instructions. */ |
| 161 | mov %cr4, %eax |
| 162 | orl $(CR4_OSFXSR | CR4_OSXMMEXCPT), %eax |
| 163 | mov %eax, %cr4 |
| 164 | |
| 165 | /* Save FP state. */ |
| 166 | fxsave (%edi) |
| 167 | |
| 168 | 1: |
| 169 | /* Align stack to 16 bytes. Another 16 bytes are pushed below. */ |
| 170 | andl $0xfffffff0, %esp |
| 171 | |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 172 | /* Call into the c-based SMM relocation function with the platform |
| 173 | * parameters. Equivalent to: |
Raul E Rangel | 0140541 | 2018-06-25 14:22:27 -0600 | [diff] [blame] | 174 | * struct arg = { c_handler_params, cpu_num, smm_runtime }; |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 175 | * c_handler(&arg) |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 176 | */ |
| 177 | push $(smm_runtime) |
Raul E Rangel | 0140541 | 2018-06-25 14:22:27 -0600 | [diff] [blame] | 178 | push %ecx /* int cpu */ |
| 179 | push c_handler_arg /* void *arg */ |
| 180 | push %esp /* smm_module_params *arg (allocated on stack). */ |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 181 | mov c_handler, %eax |
| 182 | call *%eax |
| 183 | |
Aaron Durbin | 8ade68a | 2017-06-16 15:16:13 -0500 | [diff] [blame] | 184 | /* Retrieve fxsave location. */ |
Raul E Rangel | 0140541 | 2018-06-25 14:22:27 -0600 | [diff] [blame] | 185 | mov -4(%ebp), %edi |
Aaron Durbin | 8ade68a | 2017-06-16 15:16:13 -0500 | [diff] [blame] | 186 | test %edi, %edi |
| 187 | jz 1f |
| 188 | |
| 189 | /* Restore FP state. */ |
| 190 | fxrstor (%edi) |
| 191 | |
| 192 | 1: |
Aaron Durbin | 50a3464 | 2013-01-03 17:38:47 -0600 | [diff] [blame] | 193 | /* Exit from SM mode. */ |
| 194 | rsm |