Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

e820mangler.S 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER )
  19. .text
  20. .arch i386
  21. .code16
  22. #define SMAP 0x534d4150
  23. /* Most documentation refers to the E820 buffer as being 20 bytes, and
  24. * the API makes it perfectly legitimate to pass only a 20-byte buffer
  25. * and expect to get valid data. However, some morons at ACPI decided
  26. * to extend the data structure by adding an extra "extended
  27. * attributes" field and by including critical information within this
  28. * field, such as whether or not the region is enabled. A caller who
  29. * passes in only a 20-byte buffer therefore risks getting very, very
  30. * misleading information.
  31. *
  32. * I have personally witnessed an HP BIOS that returns a value of
  33. * 0x0009 in the extended attributes field. If we don't pass this
  34. * value through to the caller, 32-bit WinPE will die, usually with a
  35. * PAGE_FAULT_IN_NONPAGED_AREA blue screen of death.
  36. *
  37. * Allow a ridiculously large maximum value (64 bytes) for the E820
  38. * buffer as a guard against insufficiently creative idiots in the
  39. * future.
  40. */
  41. #define E820MAXSIZE 64
  42. /****************************************************************************
  43. *
  44. * Allowed memory windows
  45. *
  46. * There are two ways to view this list. The first is as a list of
  47. * (non-overlapping) allowed memory regions, sorted by increasing
  48. * address. The second is as a list of (non-overlapping) hidden
  49. * memory regions, again sorted by increasing address. The second
  50. * view is offset by half an entry from the first: think about this
  51. * for a moment and it should make sense.
  52. *
  53. * xxx_memory_window is used to indicate an "allowed region"
  54. * structure, hidden_xxx_memory is used to indicate a "hidden region"
  55. * structure. Each structure is 16 bytes in length.
  56. *
  57. ****************************************************************************
  58. */
  59. .section ".data16", "aw", @progbits
  60. .align 16
  61. .globl hidemem_base
  62. .globl hidemem_umalloc
  63. .globl hidemem_textdata
  64. memory_windows:
  65. base_memory_window: .long 0x00000000, 0x00000000 /* Start of memory */
  66. hidemem_base: .long 0x000a0000, 0x00000000 /* Changes at runtime */
  67. ext_memory_window: .long 0x000a0000, 0x00000000 /* 640kB mark */
  68. hidemem_umalloc: .long 0xffffffff, 0xffffffff /* Changes at runtime */
  69. .long 0xffffffff, 0xffffffff /* Changes at runtime */
  70. hidemem_textdata: .long 0xffffffff, 0xffffffff /* Changes at runtime */
  71. .long 0xffffffff, 0xffffffff /* Changes at runtime */
  72. .long 0xffffffff, 0xffffffff /* End of memory */
  73. memory_windows_end:
  74. /****************************************************************************
  75. * Truncate region to memory window
  76. *
  77. * Parameters:
  78. * %edx:%eax Start of region
  79. * %ecx:%ebx Length of region
  80. * %si Memory window
  81. * Returns:
  82. * %edx:%eax Start of windowed region
  83. * %ecx:%ebx Length of windowed region
  84. ****************************************************************************
  85. */
  86. .section ".text16", "ax", @progbits
  87. window_region:
  88. /* Convert (start,len) to (start, end) */
  89. addl %eax, %ebx
  90. adcl %edx, %ecx
  91. /* Truncate to window start */
  92. cmpl 4(%si), %edx
  93. jne 1f
  94. cmpl 0(%si), %eax
  95. 1: jae 2f
  96. movl 4(%si), %edx
  97. movl 0(%si), %eax
  98. 2: /* Truncate to window end */
  99. cmpl 12(%si), %ecx
  100. jne 1f
  101. cmpl 8(%si), %ebx
  102. 1: jbe 2f
  103. movl 12(%si), %ecx
  104. movl 8(%si), %ebx
  105. 2: /* Convert (start, end) back to (start, len) */
  106. subl %eax, %ebx
  107. sbbl %edx, %ecx
  108. /* If length is <0, set length to 0 */
  109. jae 1f
  110. xorl %ebx, %ebx
  111. xorl %ecx, %ecx
  112. ret
  113. .size window_region, . - window_region
  114. /****************************************************************************
  115. * Patch "memory above 1MB" figure
  116. *
  117. * Parameters:
  118. * %ax Memory above 1MB, in 1kB blocks
  119. * Returns:
  120. * %ax Modified memory above 1M in 1kB blocks
  121. ****************************************************************************
  122. */
  123. .section ".text16", "ax", @progbits
  124. patch_1m:
  125. pushal
  126. /* Convert to (start,len) format and call truncate */
  127. xorl %ecx, %ecx
  128. movzwl %ax, %ebx
  129. shll $10, %ebx
  130. xorl %edx, %edx
  131. movl $0x100000, %eax
  132. movw $ext_memory_window, %si
  133. call window_region
  134. /* Convert back to "memory above 1MB" format and return via %ax */
  135. pushfw
  136. shrl $10, %ebx
  137. popfw
  138. movw %sp, %bp
  139. movw %bx, 28(%bp)
  140. popal
  141. ret
  142. .size patch_1m, . - patch_1m
  143. /****************************************************************************
  144. * Patch "memory above 16MB" figure
  145. *
  146. * Parameters:
  147. * %bx Memory above 16MB, in 64kB blocks
  148. * Returns:
  149. * %bx Modified memory above 16M in 64kB blocks
  150. ****************************************************************************
  151. */
  152. .section ".text16", "ax", @progbits
  153. patch_16m:
  154. pushal
  155. /* Convert to (start,len) format and call truncate */
  156. xorl %ecx, %ecx
  157. shll $16, %ebx
  158. xorl %edx, %edx
  159. movl $0x1000000, %eax
  160. movw $ext_memory_window, %si
  161. call window_region
  162. /* Convert back to "memory above 16MB" format and return via %bx */
  163. pushfw
  164. shrl $16, %ebx
  165. popfw
  166. movw %sp, %bp
  167. movw %bx, 16(%bp)
  168. popal
  169. ret
  170. .size patch_16m, . - patch_16m
  171. /****************************************************************************
  172. * Patch "memory between 1MB and 16MB" and "memory above 16MB" figures
  173. *
  174. * Parameters:
  175. * %ax Memory between 1MB and 16MB, in 1kB blocks
  176. * %bx Memory above 16MB, in 64kB blocks
  177. * Returns:
  178. * %ax Modified memory between 1MB and 16MB, in 1kB blocks
  179. * %bx Modified memory above 16MB, in 64kB blocks
  180. ****************************************************************************
  181. */
  182. .section ".text16", "ax", @progbits
  183. patch_1m_16m:
  184. call patch_1m
  185. call patch_16m
  186. /* If 1M region is no longer full-length, kill off the 16M region */
  187. cmpw $( 15 * 1024 ), %ax
  188. je 1f
  189. xorw %bx, %bx
  190. 1: ret
  191. .size patch_1m_16m, . - patch_1m_16m
  192. /****************************************************************************
  193. * Get underlying e820 memory region to underlying_e820 buffer
  194. *
  195. * Parameters:
  196. * As for INT 15,e820
  197. * Returns:
  198. * As for INT 15,e820
  199. *
  200. * Wraps the underlying INT 15,e820 call so that the continuation
  201. * value (%ebx) is a 16-bit simple sequence counter (with the high 16
  202. * bits ignored), and termination is always via CF=1 rather than
  203. * %ebx=0.
  204. *
  205. ****************************************************************************
  206. */
  207. .section ".text16", "ax", @progbits
  208. get_underlying_e820:
  209. /* If the requested region is in the cache, return it */
  210. cmpw %bx, underlying_e820_index
  211. jne 2f
  212. pushw %di
  213. pushw %si
  214. movw $underlying_e820_cache, %si
  215. cmpl underlying_e820_cache_size, %ecx
  216. jbe 1f
  217. movl underlying_e820_cache_size, %ecx
  218. 1: pushl %ecx
  219. rep movsb
  220. popl %ecx
  221. popw %si
  222. popw %di
  223. incw %bx
  224. movl %edx, %eax
  225. ret
  226. 2:
  227. /* If the requested region is earlier than the cached region,
  228. * invalidate the cache.
  229. */
  230. cmpw %bx, underlying_e820_index
  231. jbe 1f
  232. movw $0xffff, underlying_e820_index
  233. 1:
  234. /* If the cache is invalid, reset the underlying %ebx */
  235. cmpw $0xffff, underlying_e820_index
  236. jne 1f
  237. andl $0, underlying_e820_ebx
  238. 1:
  239. /* If the cache is valid but the continuation value is zero,
  240. * this means that the previous underlying call returned with
  241. * %ebx=0. Return with CF=1 in this case.
  242. */
  243. cmpw $0xffff, underlying_e820_index
  244. je 1f
  245. cmpl $0, underlying_e820_ebx
  246. jne 1f
  247. stc
  248. ret
  249. 1:
  250. /* Get the next region into the cache */
  251. pushl %eax
  252. pushl %ebx
  253. pushl %ecx
  254. pushl %edx
  255. pushl %esi /* Some implementations corrupt %esi, so we */
  256. pushl %edi /* preserve %esi, %edi and %ebp to be paranoid */
  257. pushl %ebp
  258. pushw %es
  259. pushw %ds
  260. popw %es
  261. movw $underlying_e820_cache, %di
  262. cmpl $E820MAXSIZE, %ecx
  263. jbe 1f
  264. movl $E820MAXSIZE, %ecx
  265. 1: movl underlying_e820_ebx, %ebx
  266. stc
  267. pushfw
  268. lcall *%cs:int15_vector
  269. popw %es
  270. popl %ebp
  271. popl %edi
  272. popl %esi
  273. /* Check for error return from underlying e820 call */
  274. jc 2f /* CF set: error */
  275. cmpl $SMAP, %eax
  276. je 3f /* 'SMAP' missing: error */
  277. 2: /* An error occurred: return values returned by underlying e820 call */
  278. stc /* Force CF set if SMAP was missing */
  279. addr32 leal 16(%esp), %esp /* avoid changing other flags */
  280. ret
  281. 3: /* No error occurred */
  282. movl %ebx, underlying_e820_ebx
  283. movl %ecx, underlying_e820_cache_size
  284. popl %edx
  285. popl %ecx
  286. popl %ebx
  287. popl %eax
  288. /* Mark cache as containing this result */
  289. incw underlying_e820_index
  290. /* Loop until found */
  291. jmp get_underlying_e820
  292. .size get_underlying_e820, . - get_underlying_e820
  293. .section ".data16", "aw", @progbits
  294. underlying_e820_index:
  295. .word 0xffff /* Initialise to an invalid value */
  296. .size underlying_e820_index, . - underlying_e820_index
  297. .section ".bss16", "aw", @nobits
  298. underlying_e820_ebx:
  299. .long 0
  300. .size underlying_e820_ebx, . - underlying_e820_ebx
  301. .section ".bss16", "aw", @nobits
  302. underlying_e820_cache:
  303. .space E820MAXSIZE
  304. .size underlying_e820_cache, . - underlying_e820_cache
  305. .section ".bss16", "aw", @nobits
  306. underlying_e820_cache_size:
  307. .long 0
  308. .size underlying_e820_cache_size, . - underlying_e820_cache_size
  309. /****************************************************************************
  310. * Get windowed e820 region, without empty region stripping
  311. *
  312. * Parameters:
  313. * As for INT 15,e820
  314. * Returns:
  315. * As for INT 15,e820
  316. *
  317. * Wraps the underlying INT 15,e820 call so that each underlying
  318. * region is returned N times, windowed to fit within N visible-memory
  319. * windows. Termination is always via CF=1.
  320. *
  321. ****************************************************************************
  322. */
  323. .section ".text16", "ax", @progbits
  324. get_windowed_e820:
  325. /* Preserve registers */
  326. pushl %esi
  327. pushw %bp
  328. /* Split %ebx into %si:%bx, store original %bx in %bp */
  329. pushl %ebx
  330. popw %bp
  331. popw %si
  332. /* %si == 0 => start of memory_windows list */
  333. testw %si, %si
  334. jne 1f
  335. movw $memory_windows, %si
  336. 1:
  337. /* Get (cached) underlying e820 region to buffer */
  338. call get_underlying_e820
  339. jc 99f /* Abort on error */
  340. /* Preserve registers */
  341. pushal
  342. /* start => %edx:%eax, len => %ecx:%ebx */
  343. movl %es:0(%di), %eax
  344. movl %es:4(%di), %edx
  345. movl %es:8(%di), %ebx
  346. movl %es:12(%di), %ecx
  347. /* Truncate region to current window */
  348. call window_region
  349. 1: /* Store modified values in e820 map entry */
  350. movl %eax, %es:0(%di)
  351. movl %edx, %es:4(%di)
  352. movl %ebx, %es:8(%di)
  353. movl %ecx, %es:12(%di)
  354. /* Restore registers */
  355. popal
  356. /* Derive continuation value for next call */
  357. addw $16, %si
  358. cmpw $memory_windows_end, %si
  359. jne 1f
  360. /* End of memory windows: reset %si and allow %bx to continue */
  361. xorw %si, %si
  362. jmp 2f
  363. 1: /* More memory windows to go: restore original %bx */
  364. movw %bp, %bx
  365. 2: /* Construct %ebx from %si:%bx */
  366. pushw %si
  367. pushw %bx
  368. popl %ebx
  369. 98: /* Clear CF */
  370. clc
  371. 99: /* Restore registers and return */
  372. popw %bp
  373. popl %esi
  374. ret
  375. .size get_windowed_e820, . - get_windowed_e820
  376. /****************************************************************************
  377. * Get windowed e820 region, with empty region stripping
  378. *
  379. * Parameters:
  380. * As for INT 15,e820
  381. * Returns:
  382. * As for INT 15,e820
  383. *
  384. * Wraps the underlying INT 15,e820 call so that each underlying
  385. * region is returned up to N times, windowed to fit within N
  386. * visible-memory windows. Empty windows are never returned.
  387. * Termination is always via CF=1.
  388. *
  389. ****************************************************************************
  390. */
  391. .section ".text16", "ax", @progbits
  392. get_nonempty_e820:
  393. /* Record entry parameters */
  394. pushl %eax
  395. pushl %ecx
  396. pushl %edx
  397. /* Get next windowed region */
  398. call get_windowed_e820
  399. jc 99f /* abort on error */
  400. /* If region is non-empty, finish here */
  401. cmpl $0, %es:8(%di)
  402. jne 98f
  403. cmpl $0, %es:12(%di)
  404. jne 98f
  405. /* Region was empty: restore entry parameters and go to next region */
  406. popl %edx
  407. popl %ecx
  408. popl %eax
  409. jmp get_nonempty_e820
  410. 98: /* Clear CF */
  411. clc
  412. 99: /* Return values from underlying call */
  413. addr32 leal 12(%esp), %esp /* avoid changing flags */
  414. ret
  415. .size get_nonempty_e820, . - get_nonempty_e820
  416. /****************************************************************************
  417. * Get mangled e820 region, with empty region stripping
  418. *
  419. * Parameters:
  420. * As for INT 15,e820
  421. * Returns:
  422. * As for INT 15,e820
  423. *
  424. * Wraps the underlying INT 15,e820 call so that underlying regions
  425. * are windowed to the allowed memory regions. Empty regions are
  426. * stripped from the map. Termination is always via %ebx=0.
  427. *
  428. ****************************************************************************
  429. */
  430. .section ".text16", "ax", @progbits
  431. get_mangled_e820:
  432. /* Get a nonempty region */
  433. call get_nonempty_e820
  434. jc 99f /* Abort on error */
  435. /* Peek ahead to see if there are any further nonempty regions */
  436. pushal
  437. pushw %es
  438. movw %sp, %bp
  439. subw %cx, %sp
  440. movl $0xe820, %eax
  441. movl $SMAP, %edx
  442. pushw %ss
  443. popw %es
  444. movw %sp, %di
  445. call get_nonempty_e820
  446. movw %bp, %sp
  447. popw %es
  448. popal
  449. jnc 99f /* There are further nonempty regions */
  450. /* No futher nonempty regions: zero %ebx and clear CF */
  451. xorl %ebx, %ebx
  452. 99: /* Return */
  453. ret
  454. .size get_mangled_e820, . - get_mangled_e820
  455. /****************************************************************************
  456. * Set/clear CF on the stack as appropriate, assumes stack is as it should
  457. * be immediately before IRET
  458. ****************************************************************************
  459. */
  460. patch_cf:
  461. pushw %bp
  462. movw %sp, %bp
  463. setc 8(%bp) /* Set/reset CF; clears PF, AF, ZF, SF */
  464. popw %bp
  465. ret
  466. /****************************************************************************
  467. * INT 15,e820 handler
  468. ****************************************************************************
  469. */
  470. .section ".text16", "ax", @progbits
  471. int15_e820:
  472. pushw %ds
  473. pushw %cs:rm_ds
  474. popw %ds
  475. call get_mangled_e820
  476. popw %ds
  477. call patch_cf
  478. iret
  479. .size int15_e820, . - int15_e820
  480. /****************************************************************************
  481. * INT 15,e801 handler
  482. ****************************************************************************
  483. */
  484. .section ".text16", "ax", @progbits
  485. int15_e801:
  486. /* Call previous handler */
  487. pushfw
  488. lcall *%cs:int15_vector
  489. call patch_cf
  490. /* Edit result */
  491. pushw %ds
  492. pushw %cs:rm_ds
  493. popw %ds
  494. call patch_1m_16m
  495. xchgw %ax, %cx
  496. xchgw %bx, %dx
  497. call patch_1m_16m
  498. xchgw %ax, %cx
  499. xchgw %bx, %dx
  500. popw %ds
  501. iret
  502. .size int15_e801, . - int15_e801
  503. /****************************************************************************
  504. * INT 15,88 handler
  505. ****************************************************************************
  506. */
  507. .section ".text16", "ax", @progbits
  508. int15_88:
  509. /* Call previous handler */
  510. pushfw
  511. lcall *%cs:int15_vector
  512. call patch_cf
  513. /* Edit result */
  514. pushw %ds
  515. pushw %cs:rm_ds
  516. popw %ds
  517. call patch_1m
  518. popw %ds
  519. iret
  520. .size int15_88, . - int15_88
  521. /****************************************************************************
  522. * INT 15 handler
  523. ****************************************************************************
  524. */
  525. .section ".text16", "ax", @progbits
  526. .globl int15
  527. int15:
  528. /* See if we want to intercept this call */
  529. pushfw
  530. cmpw $0xe820, %ax
  531. jne 1f
  532. cmpl $SMAP, %edx
  533. jne 1f
  534. popfw
  535. jmp int15_e820
  536. 1: cmpw $0xe801, %ax
  537. jne 2f
  538. popfw
  539. jmp int15_e801
  540. 2: cmpb $0x88, %ah
  541. jne 3f
  542. popfw
  543. jmp int15_88
  544. 3: popfw
  545. ljmp *%cs:int15_vector
  546. .size int15, . - int15
  547. .section ".text16.data", "aw", @progbits
  548. .globl int15_vector
  549. int15_vector:
  550. .long 0
  551. .size int15_vector, . - int15_vector