Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 1 | // Code for manipulating stack locations. |
| 2 | // |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 3 | // Copyright (C) 2009-2015 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 4 | // |
| 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 6 | |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 7 | #include "biosvar.h" // GET_GLOBAL |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 8 | #include "bregs.h" // CR0_PE |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 9 | #include "fw/paravirt.h" // PORT_SMI_CMD |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 10 | #include "hw/rtc.h" // rtc_use |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 11 | #include "list.h" // hlist_node |
Kevin O'Connor | 9dea590 | 2013-09-14 20:23:54 -0400 | [diff] [blame] | 12 | #include "malloc.h" // free |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 13 | #include "output.h" // dprintf |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 14 | #include "romfile.h" // romfile_loadint |
Kevin O'Connor | 3df600b | 2013-09-14 19:28:55 -0400 | [diff] [blame] | 15 | #include "stacks.h" // struct mutex_s |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 16 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 17 | #include "util.h" // useRTC |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 18 | |
Kevin O'Connor | 7acaa3c | 2013-10-02 20:20:03 -0400 | [diff] [blame] | 19 | #define MAIN_STACK_MAX (1024*1024) |
| 20 | |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 21 | |
| 22 | /**************************************************************** |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 23 | * 16bit / 32bit calling |
Kevin O'Connor | 9c447c3 | 2010-05-23 10:24:22 -0400 | [diff] [blame] | 24 | ****************************************************************/ |
| 25 | |
Kevin O'Connor | 15721bf | 2014-09-30 12:55:54 -0400 | [diff] [blame] | 26 | struct { |
| 27 | u8 method; |
| 28 | u8 cmosindex; |
Kevin O'Connor | bfb7b58 | 2014-10-11 13:27:16 -0400 | [diff] [blame] | 29 | u8 a20; |
Kevin O'Connor | 15721bf | 2014-09-30 12:55:54 -0400 | [diff] [blame] | 30 | u16 ss, fs, gs; |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 31 | u32 cr0; |
Kevin O'Connor | 15721bf | 2014-09-30 12:55:54 -0400 | [diff] [blame] | 32 | struct descloc_s gdt; |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 33 | } Call16Data VARLOW; |
Kevin O'Connor | 15721bf | 2014-09-30 12:55:54 -0400 | [diff] [blame] | 34 | |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 35 | #define C16_BIG 1 |
| 36 | #define C16_SMM 2 |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 37 | |
| 38 | int HaveSmmCall32 VARFSEG; |
| 39 | |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 40 | // Backup state in preparation for call32 |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 41 | static int |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 42 | call32_prep(u8 method) |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 43 | { |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 44 | if (!CONFIG_CALL32_SMM || method != C16_SMM) { |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 45 | // Backup cr0 |
| 46 | u32 cr0 = cr0_read(); |
| 47 | if (cr0 & CR0_PE) |
| 48 | // Called in 16bit protected mode?! |
| 49 | return -1; |
| 50 | SET_LOW(Call16Data.cr0, cr0); |
| 51 | |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 52 | // Backup fs/gs and gdt |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 53 | SET_LOW(Call16Data.fs, GET_SEG(FS)); |
| 54 | SET_LOW(Call16Data.gs, GET_SEG(GS)); |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 55 | struct descloc_s gdt; |
| 56 | sgdt(&gdt); |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 57 | SET_LOW(Call16Data.gdt.length, gdt.length); |
| 58 | SET_LOW(Call16Data.gdt.addr, gdt.addr); |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 59 | |
| 60 | // Enable a20 and backup its previous state |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 61 | SET_LOW(Call16Data.a20, set_a20(1)); |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 64 | // Backup ss |
| 65 | SET_LOW(Call16Data.ss, GET_SEG(SS)); |
| 66 | |
| 67 | // Backup cmos index register and disable nmi |
| 68 | u8 cmosindex = inb(PORT_CMOS_INDEX); |
Kevin O'Connor | dee3c15 | 2017-05-16 11:59:10 -0400 | [diff] [blame] | 69 | if (!(cmosindex & NMI_DISABLE_BIT)) { |
| 70 | outb(cmosindex | NMI_DISABLE_BIT, PORT_CMOS_INDEX); |
| 71 | inb(PORT_CMOS_DATA); |
| 72 | } |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 73 | SET_LOW(Call16Data.cmosindex, cmosindex); |
| 74 | |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 75 | SET_LOW(Call16Data.method, method); |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 76 | return 0; |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 79 | // Restore state backed up during call32 |
| 80 | static u8 |
| 81 | call32_post(void) |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 82 | { |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 83 | u8 method = GET_LOW(Call16Data.method); |
| 84 | SET_LOW(Call16Data.method, 0); |
| 85 | SET_LOW(Call16Data.ss, 0); |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 86 | |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 87 | if (!CONFIG_CALL32_SMM || method != C16_SMM) { |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 88 | // Restore a20 |
Kevin O'Connor | 8ebb33b | 2017-05-16 11:47:27 -0400 | [diff] [blame] | 89 | u8 a20 = GET_LOW(Call16Data.a20); |
| 90 | if (!a20) |
| 91 | set_a20(0); |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 92 | |
| 93 | // Restore gdt and fs/gs |
| 94 | struct descloc_s gdt; |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 95 | gdt.length = GET_LOW(Call16Data.gdt.length); |
| 96 | gdt.addr = GET_LOW(Call16Data.gdt.addr); |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 97 | lgdt(&gdt); |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 98 | SET_SEG(FS, GET_LOW(Call16Data.fs)); |
| 99 | SET_SEG(GS, GET_LOW(Call16Data.gs)); |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 100 | |
| 101 | // Restore cr0 |
| 102 | u32 cr0_caching = GET_LOW(Call16Data.cr0) & (CR0_CD|CR0_NW); |
| 103 | if (cr0_caching) |
| 104 | cr0_mask(CR0_CD|CR0_NW, cr0_caching); |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 107 | // Restore cmos index register |
Kevin O'Connor | dee3c15 | 2017-05-16 11:59:10 -0400 | [diff] [blame] | 108 | u8 cmosindex = GET_LOW(Call16Data.cmosindex); |
| 109 | if (!(cmosindex & NMI_DISABLE_BIT)) { |
| 110 | outb(cmosindex, PORT_CMOS_INDEX); |
| 111 | inb(PORT_CMOS_DATA); |
| 112 | } |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 113 | return method; |
| 114 | } |
| 115 | |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 116 | // Force next call16() to restore to a pristine cpu environment state |
| 117 | static void |
| 118 | call16_override(int big) |
| 119 | { |
| 120 | ASSERT32FLAT(); |
| 121 | if (getesp() > BUILD_STACK_ADDR) |
| 122 | panic("call16_override with invalid stack\n"); |
| 123 | memset(&Call16Data, 0, sizeof(Call16Data)); |
| 124 | if (big) { |
| 125 | Call16Data.method = C16_BIG; |
| 126 | Call16Data.a20 = 1; |
| 127 | } else { |
| 128 | Call16Data.a20 = !CONFIG_DISABLE_A20; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // 16bit handler code called from call16() / call16_smm() |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 133 | u32 VISIBLE16 |
| 134 | call16_helper(u32 eax, u32 edx, u32 (*func)(u32 eax, u32 edx)) |
| 135 | { |
| 136 | u8 method = call32_post(); |
| 137 | u32 ret = func(eax, edx); |
| 138 | call32_prep(method); |
| 139 | return ret; |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Kevin O'Connor | ed675ad | 2014-12-03 12:53:01 -0500 | [diff] [blame] | 142 | #define ASM32_SWITCH16 " .pushsection .text.32fseg." UNIQSEC "\n .code16\n" |
| 143 | #define ASM32_BACK32 " .popsection\n .code32\n" |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 144 | #define ASM16_SWITCH32 " .code32\n" |
| 145 | #define ASM16_BACK16 " .code16gcc\n" |
| 146 | |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 147 | // Call a SeaBIOS C function in 32bit mode using smm trampoline |
| 148 | static u32 |
| 149 | call32_smm(void *func, u32 eax) |
| 150 | { |
| 151 | ASSERT16(); |
| 152 | dprintf(9, "call32_smm %p %x\n", func, eax); |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 153 | call32_prep(C16_SMM); |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 154 | u32 bkup_esp; |
| 155 | asm volatile( |
| 156 | // Backup esp / set esp to flat stack location |
| 157 | " movl %%esp, %0\n" |
| 158 | " movl %%ss, %%eax\n" |
| 159 | " shll $4, %%eax\n" |
| 160 | " addl %%eax, %%esp\n" |
| 161 | |
| 162 | // Transition to 32bit mode, call func, return to 16bit |
| 163 | " movl $" __stringify(CALL32SMM_CMDID) ", %%eax\n" |
| 164 | " movl $" __stringify(CALL32SMM_ENTERID) ", %%ecx\n" |
| 165 | " movl $(" __stringify(BUILD_BIOS_ADDR) " + 1f), %%ebx\n" |
| 166 | " outb %%al, $" __stringify(PORT_SMI_CMD) "\n" |
| 167 | " rep; nop\n" |
| 168 | " hlt\n" |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 169 | |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 170 | ASM16_SWITCH32 |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 171 | "1:movl %1, %%eax\n" |
| 172 | " calll *%2\n" |
| 173 | " movl %%eax, %1\n" |
| 174 | |
| 175 | " movl $" __stringify(CALL32SMM_CMDID) ", %%eax\n" |
| 176 | " movl $" __stringify(CALL32SMM_RETURNID) ", %%ecx\n" |
| 177 | " movl $2f, %%ebx\n" |
| 178 | " outb %%al, $" __stringify(PORT_SMI_CMD) "\n" |
| 179 | " rep; nop\n" |
| 180 | " hlt\n" |
| 181 | |
| 182 | // Restore esp |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 183 | ASM16_BACK16 |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 184 | "2:movl %0, %%esp\n" |
| 185 | : "=&r" (bkup_esp), "+r" (eax) |
| 186 | : "r" (func) |
| 187 | : "eax", "ecx", "edx", "ebx", "cc", "memory"); |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 188 | call32_post(); |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 189 | |
| 190 | dprintf(9, "call32_smm done %p %x\n", func, eax); |
| 191 | return eax; |
| 192 | } |
| 193 | |
Kevin O'Connor | ed675ad | 2014-12-03 12:53:01 -0500 | [diff] [blame] | 194 | static u32 |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 195 | call16_smm(u32 eax, u32 edx, void *func) |
| 196 | { |
| 197 | ASSERT32FLAT(); |
| 198 | if (!CONFIG_CALL32_SMM) |
| 199 | return eax; |
| 200 | func -= BUILD_BIOS_ADDR; |
| 201 | dprintf(9, "call16_smm %p %x %x\n", func, eax, edx); |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 202 | u32 stackoffset = Call16Data.ss << 4; |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 203 | asm volatile( |
| 204 | // Restore esp |
| 205 | " subl %0, %%esp\n" |
| 206 | |
| 207 | // Transition to 16bit mode, call func, return to 32bit |
| 208 | " movl $" __stringify(CALL32SMM_CMDID) ", %%eax\n" |
| 209 | " movl $" __stringify(CALL32SMM_RETURNID) ", %%ecx\n" |
| 210 | " movl $(1f - " __stringify(BUILD_BIOS_ADDR) "), %%ebx\n" |
| 211 | " outb %%al, $" __stringify(PORT_SMI_CMD) "\n" |
| 212 | " rep; nop\n" |
| 213 | " hlt\n" |
| 214 | |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 215 | ASM32_SWITCH16 |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 216 | "1:movl %1, %%eax\n" |
| 217 | " movl %3, %%ecx\n" |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 218 | " calll _cfunc16_call16_helper\n" |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 219 | " movl %%eax, %1\n" |
| 220 | |
| 221 | " movl $" __stringify(CALL32SMM_CMDID) ", %%eax\n" |
| 222 | " movl $" __stringify(CALL32SMM_ENTERID) ", %%ecx\n" |
| 223 | " movl $2f, %%ebx\n" |
| 224 | " outb %%al, $" __stringify(PORT_SMI_CMD) "\n" |
| 225 | " rep; nop\n" |
| 226 | " hlt\n" |
| 227 | |
| 228 | // Set esp to flat stack location |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 229 | ASM32_BACK32 |
Kevin O'Connor | 55215cd | 2014-04-11 11:20:41 -0400 | [diff] [blame] | 230 | "2:addl %0, %%esp\n" |
| 231 | : "+r" (stackoffset), "+r" (eax), "+d" (edx) |
| 232 | : "r" (func) |
| 233 | : "eax", "ecx", "ebx", "cc", "memory"); |
| 234 | return eax; |
| 235 | } |
Kevin O'Connor | 7acaa3c | 2013-10-02 20:20:03 -0400 | [diff] [blame] | 236 | |
Kevin O'Connor | 63766c4 | 2015-09-11 16:15:23 -0400 | [diff] [blame] | 237 | // Call a 32bit SeaBIOS function from a 16bit SeaBIOS function. |
| 238 | u32 VISIBLE16 |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 239 | __call32(void *func, u32 eax, u32 errret) |
Kevin O'Connor | a7fc5de | 2009-12-13 11:48:18 -0500 | [diff] [blame] | 240 | { |
| 241 | ASSERT16(); |
Kevin O'Connor | 63766c4 | 2015-09-11 16:15:23 -0400 | [diff] [blame] | 242 | if (CONFIG_CALL32_SMM && GET_GLOBAL(HaveSmmCall32)) |
| 243 | return call32_smm(func, eax); |
Kevin O'Connor | 63766c4 | 2015-09-11 16:15:23 -0400 | [diff] [blame] | 244 | // Jump direclty to 32bit mode - this clobbers the 16bit segment |
| 245 | // selector registers. |
Kevin O'Connor | 62de31b | 2015-09-22 12:35:00 -0400 | [diff] [blame] | 246 | int ret = call32_prep(C16_BIG); |
| 247 | if (ret) |
| 248 | return errret; |
Kevin O'Connor | a7fc5de | 2009-12-13 11:48:18 -0500 | [diff] [blame] | 249 | u32 bkup_ss, bkup_esp; |
| 250 | asm volatile( |
| 251 | // Backup ss/esp / set esp to flat stack location |
| 252 | " movl %%ss, %0\n" |
| 253 | " movl %%esp, %1\n" |
| 254 | " shll $4, %0\n" |
| 255 | " addl %0, %%esp\n" |
Kevin O'Connor | af9629b | 2010-11-25 09:17:31 -0500 | [diff] [blame] | 256 | " shrl $4, %0\n" |
Kevin O'Connor | a7fc5de | 2009-12-13 11:48:18 -0500 | [diff] [blame] | 257 | |
Kevin O'Connor | fb214dc | 2009-12-20 13:11:17 -0500 | [diff] [blame] | 258 | // Transition to 32bit mode, call func, return to 16bit |
Kevin O'Connor | 4057f98 | 2010-11-25 08:52:50 -0500 | [diff] [blame] | 259 | " movl $(" __stringify(BUILD_BIOS_ADDR) " + 1f), %%edx\n" |
Kevin O'Connor | 423542e | 2015-09-11 16:19:02 -0400 | [diff] [blame] | 260 | " jmp transition32_nmi_off\n" |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 261 | ASM16_SWITCH32 |
Kevin O'Connor | af9629b | 2010-11-25 09:17:31 -0500 | [diff] [blame] | 262 | "1:calll *%3\n" |
Kevin O'Connor | 4057f98 | 2010-11-25 08:52:50 -0500 | [diff] [blame] | 263 | " movl $2f, %%edx\n" |
Kevin O'Connor | a7fc5de | 2009-12-13 11:48:18 -0500 | [diff] [blame] | 264 | " jmp transition16big\n" |
| 265 | |
| 266 | // Restore ds/ss/esp |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 267 | ASM16_BACK16 |
Kevin O'Connor | a7fc5de | 2009-12-13 11:48:18 -0500 | [diff] [blame] | 268 | "2:movl %0, %%ds\n" |
| 269 | " movl %0, %%ss\n" |
| 270 | " movl %1, %%esp\n" |
Kevin O'Connor | af9629b | 2010-11-25 09:17:31 -0500 | [diff] [blame] | 271 | : "=&r" (bkup_ss), "=&r" (bkup_esp), "+a" (eax) |
Kevin O'Connor | bca0736 | 2010-03-20 20:41:38 -0400 | [diff] [blame] | 272 | : "r" (func) |
Kevin O'Connor | af9629b | 2010-11-25 09:17:31 -0500 | [diff] [blame] | 273 | : "ecx", "edx", "cc", "memory"); |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 274 | call32_post(); |
Kevin O'Connor | af9629b | 2010-11-25 09:17:31 -0500 | [diff] [blame] | 275 | return eax; |
Kevin O'Connor | a7fc5de | 2009-12-13 11:48:18 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 278 | // Call a 16bit SeaBIOS function, restoring the mode from last call32(). |
Kevin O'Connor | ed675ad | 2014-12-03 12:53:01 -0500 | [diff] [blame] | 279 | static u32 |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 280 | call16(u32 eax, u32 edx, void *func) |
Kevin O'Connor | 1188480 | 2014-09-30 12:13:44 -0400 | [diff] [blame] | 281 | { |
| 282 | ASSERT32FLAT(); |
| 283 | if (getesp() > MAIN_STACK_MAX) |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 284 | panic("call16 with invalid stack\n"); |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 285 | if (CONFIG_CALL32_SMM && Call16Data.method == C16_SMM) |
| 286 | return call16_smm(eax, edx, func); |
| 287 | |
| 288 | extern void transition16big(void); |
| 289 | extern void transition16(void); |
| 290 | void *thunk = transition16; |
| 291 | if (Call16Data.method == C16_BIG || in_post()) |
| 292 | thunk = transition16big; |
Kevin O'Connor | 1188480 | 2014-09-30 12:13:44 -0400 | [diff] [blame] | 293 | func -= BUILD_BIOS_ADDR; |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 294 | u32 stackseg = Call16Data.ss; |
Kevin O'Connor | 1188480 | 2014-09-30 12:13:44 -0400 | [diff] [blame] | 295 | asm volatile( |
| 296 | // Transition to 16bit mode |
| 297 | " movl $(1f - " __stringify(BUILD_BIOS_ADDR) "), %%edx\n" |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 298 | " jmp *%%ecx\n" |
Kevin O'Connor | 1188480 | 2014-09-30 12:13:44 -0400 | [diff] [blame] | 299 | // Setup ss/esp and call func |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 300 | ASM32_SWITCH16 |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 301 | "1:movl %2, %%ecx\n" |
| 302 | " shll $4, %2\n" |
Kevin O'Connor | 1188480 | 2014-09-30 12:13:44 -0400 | [diff] [blame] | 303 | " movw %%cx, %%ss\n" |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 304 | " subl %2, %%esp\n" |
Kevin O'Connor | 1188480 | 2014-09-30 12:13:44 -0400 | [diff] [blame] | 305 | " movw %%cx, %%ds\n" |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 306 | " movl %4, %%edx\n" |
| 307 | " movl %3, %%ecx\n" |
Kevin O'Connor | 25eb061 | 2015-09-11 15:38:43 -0400 | [diff] [blame] | 308 | " calll _cfunc16_call16_helper\n" |
Kevin O'Connor | 1188480 | 2014-09-30 12:13:44 -0400 | [diff] [blame] | 309 | // Return to 32bit and restore esp |
| 310 | " movl $2f, %%edx\n" |
Kevin O'Connor | 423542e | 2015-09-11 16:19:02 -0400 | [diff] [blame] | 311 | " jmp transition32_nmi_off\n" |
Kevin O'Connor | ff6d551 | 2014-12-03 12:39:28 -0500 | [diff] [blame] | 312 | ASM32_BACK32 |
Kevin O'Connor | 46a346e | 2015-09-11 16:07:59 -0400 | [diff] [blame] | 313 | "2:addl %2, %%esp\n" |
| 314 | : "+a" (eax), "+c"(thunk), "+r"(stackseg) |
| 315 | : "r" (func), "r" (edx) |
| 316 | : "edx", "cc", "memory"); |
Kevin O'Connor | 1188480 | 2014-09-30 12:13:44 -0400 | [diff] [blame] | 317 | return eax; |
| 318 | } |
| 319 | |
Kevin O'Connor | bfac2b5 | 2013-09-29 10:48:24 -0400 | [diff] [blame] | 320 | |
| 321 | /**************************************************************** |
Kevin O'Connor | 1389eee | 2014-09-29 19:08:57 -0400 | [diff] [blame] | 322 | * Extra 16bit stack |
| 323 | ****************************************************************/ |
| 324 | |
| 325 | // Space for a stack for 16bit code. |
| 326 | u8 ExtraStack[BUILD_EXTRA_STACK_SIZE+1] VARLOW __aligned(8); |
| 327 | u8 *StackPos VARLOW; |
| 328 | |
| 329 | // Test if currently on the extra stack |
Kevin O'Connor | fabc1b5 | 2014-09-29 19:18:25 -0400 | [diff] [blame] | 330 | int |
Kevin O'Connor | 1389eee | 2014-09-29 19:08:57 -0400 | [diff] [blame] | 331 | on_extra_stack(void) |
| 332 | { |
| 333 | return MODE16 && GET_SEG(SS) == SEG_LOW && getesp() > (u32)ExtraStack; |
| 334 | } |
| 335 | |
| 336 | // Switch to the extra stack and call a function. |
| 337 | u32 |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 338 | __stack_hop(u32 eax, u32 edx, void *func) |
Kevin O'Connor | 1389eee | 2014-09-29 19:08:57 -0400 | [diff] [blame] | 339 | { |
| 340 | if (on_extra_stack()) |
| 341 | return ((u32 (*)(u32, u32))func)(eax, edx); |
| 342 | ASSERT16(); |
| 343 | u16 stack_seg = SEG_LOW; |
| 344 | u32 bkup_ss, bkup_esp; |
| 345 | asm volatile( |
| 346 | // Backup current %ss/%esp values. |
| 347 | "movw %%ss, %w3\n" |
| 348 | "movl %%esp, %4\n" |
| 349 | // Copy stack seg to %ds/%ss and set %esp |
| 350 | "movw %w6, %%ds\n" |
| 351 | "movw %w6, %%ss\n" |
| 352 | "movl %5, %%esp\n" |
| 353 | "pushl %3\n" |
| 354 | "pushl %4\n" |
| 355 | // Call func |
| 356 | "calll *%2\n" |
| 357 | "popl %4\n" |
| 358 | "popl %3\n" |
| 359 | // Restore segments and stack |
| 360 | "movw %w3, %%ds\n" |
| 361 | "movw %w3, %%ss\n" |
| 362 | "movl %4, %%esp" |
| 363 | : "+a" (eax), "+d" (edx), "+c" (func), "=&r" (bkup_ss), "=&r" (bkup_esp) |
| 364 | : "m" (StackPos), "r" (stack_seg) |
| 365 | : "cc", "memory"); |
| 366 | return eax; |
| 367 | } |
| 368 | |
| 369 | // Switch back to original caller's stack and call a function. |
| 370 | u32 |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 371 | __stack_hop_back(u32 eax, u32 edx, void *func) |
Kevin O'Connor | 1389eee | 2014-09-29 19:08:57 -0400 | [diff] [blame] | 372 | { |
Kevin O'Connor | 8056825 | 2014-09-29 19:39:31 -0400 | [diff] [blame] | 373 | if (!MODESEGMENT) |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 374 | return call16(eax, edx, func); |
Kevin O'Connor | 83c8276 | 2014-11-12 17:49:33 -0500 | [diff] [blame] | 375 | if (!MODE16 || !on_extra_stack()) |
Kevin O'Connor | 1389eee | 2014-09-29 19:08:57 -0400 | [diff] [blame] | 376 | return ((u32 (*)(u32, u32))func)(eax, edx); |
| 377 | ASSERT16(); |
| 378 | u16 bkup_ss; |
| 379 | u32 bkup_stack_pos, temp; |
| 380 | asm volatile( |
| 381 | // Backup stack_pos and current %ss/%esp |
| 382 | "movl %6, %4\n" |
| 383 | "movw %%ss, %w3\n" |
| 384 | "movl %%esp, %6\n" |
| 385 | // Restore original callers' %ss/%esp |
| 386 | "movl -4(%4), %5\n" |
| 387 | "movl %5, %%ss\n" |
| 388 | "movw %%ds:-8(%4), %%sp\n" |
| 389 | "movl %5, %%ds\n" |
| 390 | // Call func |
| 391 | "calll *%2\n" |
| 392 | // Restore %ss/%esp and stack_pos |
| 393 | "movw %w3, %%ds\n" |
| 394 | "movw %w3, %%ss\n" |
| 395 | "movl %6, %%esp\n" |
| 396 | "movl %4, %6" |
| 397 | : "+a" (eax), "+d" (edx), "+c" (func), "=&r" (bkup_ss) |
| 398 | , "=&r" (bkup_stack_pos), "=&r" (temp), "+m" (StackPos) |
| 399 | : |
| 400 | : "cc", "memory"); |
| 401 | return eax; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | /**************************************************************** |
Kevin O'Connor | bfac2b5 | 2013-09-29 10:48:24 -0400 | [diff] [blame] | 406 | * External 16bit interface calling |
| 407 | ****************************************************************/ |
| 408 | |
Kevin O'Connor | 2f898d5 | 2012-05-28 10:56:20 -0400 | [diff] [blame] | 409 | // Far call 16bit code with a specified register state. |
| 410 | void VISIBLE16 |
Kevin O'Connor | 2d97756 | 2013-09-29 20:21:40 -0400 | [diff] [blame] | 411 | _farcall16(struct bregs *callregs, u16 callregseg) |
Kevin O'Connor | 2f898d5 | 2012-05-28 10:56:20 -0400 | [diff] [blame] | 412 | { |
Kevin O'Connor | fabc1b5 | 2014-09-29 19:18:25 -0400 | [diff] [blame] | 413 | if (need_hop_back()) { |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 414 | stack_hop_back(_farcall16, callregs, callregseg); |
Kevin O'Connor | bfac2b5 | 2013-09-29 10:48:24 -0400 | [diff] [blame] | 415 | return; |
| 416 | } |
Kevin O'Connor | 8056825 | 2014-09-29 19:39:31 -0400 | [diff] [blame] | 417 | ASSERT16(); |
Kevin O'Connor | 2f898d5 | 2012-05-28 10:56:20 -0400 | [diff] [blame] | 418 | asm volatile( |
| 419 | "calll __farcall16\n" |
Kevin O'Connor | 2d97756 | 2013-09-29 20:21:40 -0400 | [diff] [blame] | 420 | : "+a" (callregs), "+m" (*callregs), "+d" (callregseg) |
| 421 | : |
| 422 | : "ebx", "ecx", "esi", "edi", "cc", "memory"); |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 423 | } |
| 424 | |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 425 | // Invoke external 16bit code. |
Kevin O'Connor | 79c3ab3 | 2014-09-30 00:11:38 -0400 | [diff] [blame] | 426 | void |
Kevin O'Connor | 2f898d5 | 2012-05-28 10:56:20 -0400 | [diff] [blame] | 427 | farcall16(struct bregs *callregs) |
| 428 | { |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 429 | call16_override(0); |
| 430 | _farcall16(callregs, 0); |
Kevin O'Connor | 2f898d5 | 2012-05-28 10:56:20 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 433 | // Invoke external 16bit code in "big real" mode. |
Kevin O'Connor | 79c3ab3 | 2014-09-30 00:11:38 -0400 | [diff] [blame] | 434 | void |
Kevin O'Connor | 2f898d5 | 2012-05-28 10:56:20 -0400 | [diff] [blame] | 435 | farcall16big(struct bregs *callregs) |
| 436 | { |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 437 | call16_override(1); |
| 438 | _farcall16(callregs, 0); |
Kevin O'Connor | 2f898d5 | 2012-05-28 10:56:20 -0400 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // Invoke a 16bit software interrupt. |
Kevin O'Connor | 79c3ab3 | 2014-09-30 00:11:38 -0400 | [diff] [blame] | 442 | void |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 443 | __call16_int(struct bregs *callregs, u16 offset) |
| 444 | { |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 445 | callregs->code.offset = offset; |
Kevin O'Connor | 79c3ab3 | 2014-09-30 00:11:38 -0400 | [diff] [blame] | 446 | if (!MODESEGMENT) { |
| 447 | callregs->code.seg = SEG_BIOS; |
Kevin O'Connor | 4c568d8 | 2015-09-13 10:03:12 -0400 | [diff] [blame] | 448 | _farcall16((void*)callregs - Call16Data.ss * 16, Call16Data.ss); |
Kevin O'Connor | 79c3ab3 | 2014-09-30 00:11:38 -0400 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | callregs->code.seg = GET_SEG(CS); |
| 452 | _farcall16(callregs, GET_SEG(SS)); |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 453 | } |
| 454 | |
Kevin O'Connor | 43197a2 | 2014-06-06 13:49:33 -0400 | [diff] [blame] | 455 | // Reset the machine |
| 456 | void |
| 457 | reset(void) |
| 458 | { |
| 459 | extern void reset_vector(void) __noreturn; |
Kevin O'Connor | 9984b9d | 2014-09-30 00:17:44 -0400 | [diff] [blame] | 460 | if (!MODE16) |
Kevin O'Connor | 4c599ef | 2015-09-22 14:38:14 -0400 | [diff] [blame] | 461 | call16(0, 0, reset_vector); |
Kevin O'Connor | 43197a2 | 2014-06-06 13:49:33 -0400 | [diff] [blame] | 462 | reset_vector(); |
| 463 | } |
| 464 | |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 465 | |
| 466 | /**************************************************************** |
| 467 | * Threads |
| 468 | ****************************************************************/ |
| 469 | |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 470 | // Thread info - stored at bottom of each thread stack - don't change |
| 471 | // without also updating the inline assembler below. |
| 472 | struct thread_info { |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 473 | void *stackpos; |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 474 | struct hlist_node node; |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 475 | }; |
Kevin O'Connor | 89a2f96 | 2013-02-18 23:36:03 -0500 | [diff] [blame] | 476 | struct thread_info MainThread VARFSEG = { |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 477 | NULL, { &MainThread.node, &MainThread.node.next } |
Kevin O'Connor | e77c705 | 2012-05-28 22:06:42 -0400 | [diff] [blame] | 478 | }; |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 479 | #define THREADSTACKSIZE 4096 |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 480 | |
Kevin O'Connor | 2a8633f | 2013-06-08 22:11:07 -0400 | [diff] [blame] | 481 | // Check if any threads are running. |
| 482 | static int |
| 483 | have_threads(void) |
| 484 | { |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 485 | return (CONFIG_THREADS |
| 486 | && GET_FLATPTR(MainThread.node.next) != &MainThread.node); |
Kevin O'Connor | 2a8633f | 2013-06-08 22:11:07 -0400 | [diff] [blame] | 487 | } |
| 488 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 489 | // Return the 'struct thread_info' for the currently running thread. |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 490 | struct thread_info * |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 491 | getCurThread(void) |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 492 | { |
| 493 | u32 esp = getesp(); |
Kevin O'Connor | 7acaa3c | 2013-10-02 20:20:03 -0400 | [diff] [blame] | 494 | if (esp <= MAIN_STACK_MAX) |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 495 | return &MainThread; |
| 496 | return (void*)ALIGN_DOWN(esp, THREADSTACKSIZE); |
| 497 | } |
| 498 | |
Kevin O'Connor | 8b9942f | 2015-07-14 15:44:26 -0400 | [diff] [blame] | 499 | static u8 CanInterrupt, ThreadControl; |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 500 | |
| 501 | // Initialize the support for internal threads. |
| 502 | void |
Kevin O'Connor | 8b9942f | 2015-07-14 15:44:26 -0400 | [diff] [blame] | 503 | thread_setup(void) |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 504 | { |
Kevin O'Connor | 8b9942f | 2015-07-14 15:44:26 -0400 | [diff] [blame] | 505 | CanInterrupt = 1; |
Kevin O'Connor | 5869a6b | 2017-05-16 11:36:43 -0400 | [diff] [blame] | 506 | call16_override(1); |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 507 | if (! CONFIG_THREADS) |
| 508 | return; |
| 509 | ThreadControl = romfile_loadint("etc/threads", 1); |
| 510 | } |
| 511 | |
| 512 | // Should hardware initialization threads run during optionrom execution. |
| 513 | int |
| 514 | threads_during_optionroms(void) |
| 515 | { |
Kevin O'Connor | bc46ebe | 2015-08-13 11:43:27 -0400 | [diff] [blame] | 516 | return CONFIG_THREADS && CONFIG_RTC_TIMER && ThreadControl == 2 && in_post(); |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 519 | // Switch to next thread stack. |
| 520 | static void |
| 521 | switch_next(struct thread_info *cur) |
| 522 | { |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 523 | struct thread_info *next = container_of( |
| 524 | cur->node.next, struct thread_info, node); |
Kevin O'Connor | fb214dc | 2009-12-20 13:11:17 -0500 | [diff] [blame] | 525 | if (cur == next) |
| 526 | // Nothing to do. |
| 527 | return; |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 528 | asm volatile( |
| 529 | " pushl $1f\n" // store return pc |
| 530 | " pushl %%ebp\n" // backup %ebp |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 531 | " movl %%esp, (%%eax)\n" // cur->stackpos = %esp |
| 532 | " movl (%%ecx), %%esp\n" // %esp = next->stackpos |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 533 | " popl %%ebp\n" // restore %ebp |
| 534 | " retl\n" // restore pc |
| 535 | "1:\n" |
| 536 | : "+a"(cur), "+c"(next) |
| 537 | : |
| 538 | : "ebx", "edx", "esi", "edi", "cc", "memory"); |
| 539 | } |
| 540 | |
Kevin O'Connor | 48367e2 | 2013-12-22 22:44:08 -0500 | [diff] [blame] | 541 | // Last thing called from a thread (called on MainThread stack). |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 542 | static void |
| 543 | __end_thread(struct thread_info *old) |
| 544 | { |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 545 | hlist_del(&old->node); |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 546 | dprintf(DEBUG_thread, "\\%08x/ End thread\n", (u32)old); |
Kevin O'Connor | 48367e2 | 2013-12-22 22:44:08 -0500 | [diff] [blame] | 547 | free(old); |
Kevin O'Connor | 2a8633f | 2013-06-08 22:11:07 -0400 | [diff] [blame] | 548 | if (!have_threads()) |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 549 | dprintf(1, "All threads complete.\n"); |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 550 | } |
| 551 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 552 | // Create a new thread and start executing 'func' in it. |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 553 | void |
| 554 | run_thread(void (*func)(void*), void *data) |
| 555 | { |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 556 | ASSERT32FLAT(); |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 557 | if (! CONFIG_THREADS || ! ThreadControl) |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 558 | goto fail; |
| 559 | struct thread_info *thread; |
| 560 | thread = memalign_tmphigh(THREADSTACKSIZE, THREADSTACKSIZE); |
| 561 | if (!thread) |
| 562 | goto fail; |
| 563 | |
Kevin O'Connor | 48367e2 | 2013-12-22 22:44:08 -0500 | [diff] [blame] | 564 | dprintf(DEBUG_thread, "/%08x\\ Start thread\n", (u32)thread); |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 565 | thread->stackpos = (void*)thread + THREADSTACKSIZE; |
| 566 | struct thread_info *cur = getCurThread(); |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 567 | hlist_add_after(&thread->node, &cur->node); |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 568 | asm volatile( |
| 569 | // Start thread |
| 570 | " pushl $1f\n" // store return pc |
| 571 | " pushl %%ebp\n" // backup %ebp |
Kevin O'Connor | 0039a94 | 2013-06-08 21:50:15 -0400 | [diff] [blame] | 572 | " movl %%esp, (%%edx)\n" // cur->stackpos = %esp |
| 573 | " movl (%%ebx), %%esp\n" // %esp = thread->stackpos |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 574 | " calll *%%ecx\n" // Call func |
| 575 | |
| 576 | // End thread |
Kevin O'Connor | 48367e2 | 2013-12-22 22:44:08 -0500 | [diff] [blame] | 577 | " movl %%ebx, %%eax\n" // %eax = thread |
| 578 | " movl 4(%%ebx), %%ebx\n" // %ebx = thread->node.next |
| 579 | " movl (%5), %%esp\n" // %esp = MainThread.stackpos |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 580 | " calll %4\n" // call __end_thread(thread) |
Kevin O'Connor | 48367e2 | 2013-12-22 22:44:08 -0500 | [diff] [blame] | 581 | " movl -4(%%ebx), %%esp\n" // %esp = next->stackpos |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 582 | " popl %%ebp\n" // restore %ebp |
| 583 | " retl\n" // restore pc |
| 584 | "1:\n" |
| 585 | : "+a"(data), "+c"(func), "+b"(thread), "+d"(cur) |
Kevin O'Connor | 48367e2 | 2013-12-22 22:44:08 -0500 | [diff] [blame] | 586 | : "m"(*(u8*)__end_thread), "m"(MainThread) |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 587 | : "esi", "edi", "cc", "memory"); |
| 588 | return; |
| 589 | |
| 590 | fail: |
| 591 | func(data); |
| 592 | } |
| 593 | |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 594 | |
| 595 | /**************************************************************** |
| 596 | * Thread helpers |
| 597 | ****************************************************************/ |
| 598 | |
| 599 | // Low-level irq enable. |
| 600 | void VISIBLE16 |
| 601 | check_irqs(void) |
| 602 | { |
Kevin O'Connor | 2a91b34 | 2015-08-07 16:13:18 -0400 | [diff] [blame] | 603 | if (!MODESEGMENT && !CanInterrupt) { |
Kevin O'Connor | ec52068 | 2015-08-07 16:12:02 -0400 | [diff] [blame] | 604 | // Can't enable interrupts (PIC and/or IVT not yet setup) |
Kevin O'Connor | 2a91b34 | 2015-08-07 16:13:18 -0400 | [diff] [blame] | 605 | cpu_relax(); |
Kevin O'Connor | ec52068 | 2015-08-07 16:12:02 -0400 | [diff] [blame] | 606 | return; |
Kevin O'Connor | 2a91b34 | 2015-08-07 16:13:18 -0400 | [diff] [blame] | 607 | } |
Kevin O'Connor | fabc1b5 | 2014-09-29 19:18:25 -0400 | [diff] [blame] | 608 | if (need_hop_back()) { |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 609 | stack_hop_back(check_irqs, 0, 0); |
Kevin O'Connor | bfac2b5 | 2013-09-29 10:48:24 -0400 | [diff] [blame] | 610 | return; |
| 611 | } |
Kevin O'Connor | bd5f6c7 | 2015-08-10 16:14:48 -0400 | [diff] [blame] | 612 | if (MODE16) |
| 613 | clock_poll_irq(); |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 614 | asm volatile("sti ; nop ; rep ; nop ; cli ; cld" : : :"memory"); |
| 615 | } |
| 616 | |
| 617 | // Briefly permit irqs to occur. |
| 618 | void |
| 619 | yield(void) |
| 620 | { |
Kevin O'Connor | 8056825 | 2014-09-29 19:39:31 -0400 | [diff] [blame] | 621 | if (MODESEGMENT || !CONFIG_THREADS) { |
Kevin O'Connor | ec52068 | 2015-08-07 16:12:02 -0400 | [diff] [blame] | 622 | check_irqs(); |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 623 | return; |
| 624 | } |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 625 | struct thread_info *cur = getCurThread(); |
Kevin O'Connor | ec52068 | 2015-08-07 16:12:02 -0400 | [diff] [blame] | 626 | if (cur == &MainThread) |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 627 | // Permit irqs to fire |
Kevin O'Connor | 8056825 | 2014-09-29 19:39:31 -0400 | [diff] [blame] | 628 | check_irqs(); |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 629 | |
| 630 | // Switch to the next thread |
| 631 | switch_next(cur); |
| 632 | } |
| 633 | |
| 634 | void VISIBLE16 |
| 635 | wait_irq(void) |
| 636 | { |
Kevin O'Connor | fabc1b5 | 2014-09-29 19:18:25 -0400 | [diff] [blame] | 637 | if (need_hop_back()) { |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 638 | stack_hop_back(wait_irq, 0, 0); |
Kevin O'Connor | bfac2b5 | 2013-09-29 10:48:24 -0400 | [diff] [blame] | 639 | return; |
| 640 | } |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 641 | asm volatile("sti ; hlt ; cli ; cld": : :"memory"); |
| 642 | } |
| 643 | |
| 644 | // Wait for next irq to occur. |
| 645 | void |
| 646 | yield_toirq(void) |
| 647 | { |
Kevin O'Connor | bd5f6c7 | 2015-08-10 16:14:48 -0400 | [diff] [blame] | 648 | if (!CONFIG_HARDWARE_IRQ |
| 649 | || (!MODESEGMENT && (have_threads() || !CanInterrupt))) { |
Kevin O'Connor | ec52068 | 2015-08-07 16:12:02 -0400 | [diff] [blame] | 650 | // Threads still active or irqs not available - do a yield instead. |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 651 | yield(); |
| 652 | return; |
| 653 | } |
Kevin O'Connor | ec52068 | 2015-08-07 16:12:02 -0400 | [diff] [blame] | 654 | wait_irq(); |
Kevin O'Connor | 3cf301f | 2013-06-08 22:05:19 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 657 | // Wait for all threads (other than the main thread) to complete. |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 658 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 659 | wait_threads(void) |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 660 | { |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 661 | ASSERT32FLAT(); |
Kevin O'Connor | 2a8633f | 2013-06-08 22:11:07 -0400 | [diff] [blame] | 662 | while (have_threads()) |
Kevin O'Connor | 7cefbfa | 2009-12-10 21:35:49 -0500 | [diff] [blame] | 663 | yield(); |
| 664 | } |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 665 | |
Kevin O'Connor | e908665 | 2010-02-27 13:49:47 -0500 | [diff] [blame] | 666 | void |
| 667 | mutex_lock(struct mutex_s *mutex) |
| 668 | { |
| 669 | ASSERT32FLAT(); |
| 670 | if (! CONFIG_THREADS) |
| 671 | return; |
| 672 | while (mutex->isLocked) |
| 673 | yield(); |
| 674 | mutex->isLocked = 1; |
| 675 | } |
| 676 | |
| 677 | void |
| 678 | mutex_unlock(struct mutex_s *mutex) |
| 679 | { |
| 680 | ASSERT32FLAT(); |
| 681 | if (! CONFIG_THREADS) |
| 682 | return; |
| 683 | mutex->isLocked = 0; |
| 684 | } |
| 685 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 686 | |
| 687 | /**************************************************************** |
| 688 | * Thread preemption |
| 689 | ****************************************************************/ |
| 690 | |
Kevin O'Connor | 89a2f96 | 2013-02-18 23:36:03 -0500 | [diff] [blame] | 691 | int CanPreempt VARFSEG; |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 692 | static u32 PreemptCount; |
| 693 | |
| 694 | // Turn on RTC irqs and arrange for them to check the 32bit threads. |
| 695 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 696 | start_preempt(void) |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 697 | { |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 698 | if (! threads_during_optionroms()) |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 699 | return; |
| 700 | CanPreempt = 1; |
| 701 | PreemptCount = 0; |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 702 | rtc_use(); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // Turn off RTC irqs / stop checking for thread execution. |
| 706 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 707 | finish_preempt(void) |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 708 | { |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 709 | if (! threads_during_optionroms()) { |
Kevin O'Connor | a7eb8fc | 2010-04-02 13:13:23 -0400 | [diff] [blame] | 710 | yield(); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 711 | return; |
Kevin O'Connor | a7eb8fc | 2010-04-02 13:13:23 -0400 | [diff] [blame] | 712 | } |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 713 | CanPreempt = 0; |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 714 | rtc_release(); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 715 | dprintf(9, "Done preempt - %d checks\n", PreemptCount); |
Kevin O'Connor | a7eb8fc | 2010-04-02 13:13:23 -0400 | [diff] [blame] | 716 | yield(); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 717 | } |
| 718 | |
Kevin O'Connor | d7eb27e | 2010-03-20 18:17:19 -0400 | [diff] [blame] | 719 | // Check if preemption is on, and wait for it to complete if so. |
| 720 | int |
| 721 | wait_preempt(void) |
| 722 | { |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 723 | if (MODESEGMENT || !CONFIG_THREADS || !CanPreempt |
Kevin O'Connor | 7acaa3c | 2013-10-02 20:20:03 -0400 | [diff] [blame] | 724 | || getesp() < MAIN_STACK_MAX) |
Kevin O'Connor | d7eb27e | 2010-03-20 18:17:19 -0400 | [diff] [blame] | 725 | return 0; |
| 726 | while (CanPreempt) |
| 727 | yield(); |
| 728 | return 1; |
| 729 | } |
| 730 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 731 | // Try to execute 32bit threads. |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 732 | void VISIBLE32INIT |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 733 | yield_preempt(void) |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 734 | { |
| 735 | PreemptCount++; |
| 736 | switch_next(&MainThread); |
| 737 | } |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 738 | |
| 739 | // 16bit code that checks if threads are pending and executes them if so. |
| 740 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 741 | check_preempt(void) |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 742 | { |
Kevin O'Connor | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 743 | if (CONFIG_THREADS && GET_GLOBAL(CanPreempt) && have_threads()) |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 744 | call32(yield_preempt, 0, 0); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 745 | } |
Kevin O'Connor | 7f54bf7 | 2013-12-30 22:04:28 -0500 | [diff] [blame] | 746 | |
| 747 | |
| 748 | /**************************************************************** |
| 749 | * call32 helper |
| 750 | ****************************************************************/ |
| 751 | |
| 752 | struct call32_params_s { |
| 753 | void *func; |
| 754 | u32 eax, edx, ecx; |
| 755 | }; |
| 756 | |
| 757 | u32 VISIBLE32FLAT |
| 758 | call32_params_helper(struct call32_params_s *params) |
| 759 | { |
| 760 | return ((u32 (*)(u32, u32, u32))params->func)( |
| 761 | params->eax, params->edx, params->ecx); |
| 762 | } |
| 763 | |
| 764 | u32 |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 765 | __call32_params(void *func, u32 eax, u32 edx, u32 ecx, u32 errret) |
Kevin O'Connor | 7f54bf7 | 2013-12-30 22:04:28 -0500 | [diff] [blame] | 766 | { |
| 767 | ASSERT16(); |
| 768 | struct call32_params_s params = {func, eax, edx, ecx}; |
Kevin O'Connor | b4cca86 | 2015-10-09 11:53:02 -0400 | [diff] [blame] | 769 | return call32(call32_params_helper, MAKE_FLATPTR(GET_SEG(SS), ¶ms) |
| 770 | , errret); |
Kevin O'Connor | 7f54bf7 | 2013-12-30 22:04:28 -0500 | [diff] [blame] | 771 | } |