您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

e820mangler.S 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. .text
  19. .arch i386
  20. .section ".text16", "ax", @progbits
  21. .section ".data16", "aw", @progbits
  22. .section ".text16.data", "aw", @progbits
  23. .code16
  24. #define SMAP 0x534d4150
  25. /****************************************************************************
  26. *
  27. * Allowed memory windows
  28. *
  29. * There are two ways to view this list. The first is as a list of
  30. * (non-overlapping) allowed memory regions, sorted by increasing
  31. * address. The second is as a list of (non-overlapping) hidden
  32. * memory regions, again sorted by increasing address. The second
  33. * view is offset by half an entry from the first: think about this
  34. * for a moment and it should make sense.
  35. *
  36. * xxx_memory_window is used to indicate an "allowed region"
  37. * structure, hidden_xxx_memory is used to indicate a "hidden region"
  38. * structure. Each structure is 16 bytes in length.
  39. *
  40. ****************************************************************************
  41. */
  42. .section ".data16"
  43. .align 16
  44. .globl hidemem_base
  45. .globl hidemem_umalloc
  46. .globl hidemem_text
  47. memory_windows:
  48. base_memory_window: .long 0x00000000, 0x00000000 /* Start of memory */
  49. hidemem_base: .long 0x000a0000, 0x00000000 /* Changes at runtime */
  50. ext_memory_window: .long 0x000a0000, 0x00000000 /* 640kB mark */
  51. hidemem_umalloc: .long 0xffffffff, 0xffffffff /* Changes at runtime */
  52. .long 0xffffffff, 0xffffffff /* Changes at runtime */
  53. hidemem_text: .long 0xffffffff, 0xffffffff /* Changes at runtime */
  54. .long 0xffffffff, 0xffffffff /* Changes at runtime */
  55. .long 0xffffffff, 0xffffffff /* End of memory */
  56. memory_windows_end:
  57. /****************************************************************************
  58. * Truncate region to memory window
  59. *
  60. * Parameters:
  61. * %edx:%eax Start of region
  62. * %ecx:%ebx Length of region
  63. * %si Memory window
  64. * Returns:
  65. * %edx:%eax Start of windowed region
  66. * %ecx:%ebx Length of windowed region
  67. ****************************************************************************
  68. */
  69. .section ".text16"
  70. window_region:
  71. /* Convert (start,len) to (start, end) */
  72. addl %eax, %ebx
  73. adcl %edx, %ecx
  74. /* Truncate to window start */
  75. cmpl 4(%si), %edx
  76. jne 1f
  77. cmpl 0(%si), %eax
  78. 1: jae 2f
  79. movl 4(%si), %edx
  80. movl 0(%si), %eax
  81. 2: /* Truncate to window end */
  82. cmpl 12(%si), %ecx
  83. jne 1f
  84. cmpl 8(%si), %ebx
  85. 1: jbe 2f
  86. movl 12(%si), %ecx
  87. movl 8(%si), %ebx
  88. 2: /* Convert (start, end) back to (start, len) */
  89. subl %eax, %ebx
  90. sbbl %edx, %ecx
  91. /* If length is <0, set length to 0 */
  92. jae 1f
  93. xorl %ebx, %ebx
  94. xorl %ecx, %ecx
  95. ret
  96. .size window_region, . - window_region
  97. /****************************************************************************
  98. * Patch "memory above 1MB" figure
  99. *
  100. * Parameters:
  101. * %ax Memory above 1MB, in 1kB blocks
  102. * Returns:
  103. * %ax Modified memory above 1M in 1kB blocks
  104. ****************************************************************************
  105. */
  106. .section ".text16"
  107. patch_1m:
  108. pushal
  109. /* Convert to (start,len) format and call truncate */
  110. xorl %ecx, %ecx
  111. movzwl %ax, %ebx
  112. shll $10, %ebx
  113. xorl %edx, %edx
  114. movl $0x100000, %eax
  115. movw $ext_memory_window, %si
  116. call window_region
  117. /* Convert back to "memory above 1MB" format and return via %ax */
  118. pushfw
  119. shrl $10, %ebx
  120. popfw
  121. movw %sp, %bp
  122. movw %bx, 28(%bp)
  123. popal
  124. ret
  125. .size patch_1m, . - patch_1m
  126. /****************************************************************************
  127. * Patch "memory above 16MB" figure
  128. *
  129. * Parameters:
  130. * %bx Memory above 16MB, in 64kB blocks
  131. * Returns:
  132. * %bx Modified memory above 16M in 64kB blocks
  133. ****************************************************************************
  134. */
  135. .section ".text16"
  136. patch_16m:
  137. pushal
  138. /* Convert to (start,len) format and call truncate */
  139. xorl %ecx, %ecx
  140. shll $16, %ebx
  141. xorl %edx, %edx
  142. movl $0x1000000, %eax
  143. movw $ext_memory_window, %si
  144. call window_region
  145. /* Convert back to "memory above 16MB" format and return via %bx */
  146. pushfw
  147. shrl $16, %ebx
  148. popfw
  149. movw %sp, %bp
  150. movw %bx, 16(%bp)
  151. popal
  152. ret
  153. .size patch_16m, . - patch_16m
  154. /****************************************************************************
  155. * Patch "memory between 1MB and 16MB" and "memory above 16MB" figures
  156. *
  157. * Parameters:
  158. * %ax Memory between 1MB and 16MB, in 1kB blocks
  159. * %bx Memory above 16MB, in 64kB blocks
  160. * Returns:
  161. * %ax Modified memory between 1MB and 16MB, in 1kB blocks
  162. * %bx Modified memory above 16MB, in 64kB blocks
  163. ****************************************************************************
  164. */
  165. .section ".text16"
  166. patch_1m_16m:
  167. call patch_1m
  168. call patch_16m
  169. /* If 1M region is no longer full-length, kill off the 16M region */
  170. cmpw $( 15 * 1024 ), %ax
  171. je 1f
  172. xorw %bx, %bx
  173. 1: ret
  174. .size patch_1m_16m, . - patch_1m_16m
  175. /****************************************************************************
  176. * Get underlying e820 memory region to underlying_e820 buffer
  177. *
  178. * Parameters:
  179. * As for INT 15,e820
  180. * Returns:
  181. * As for INT 15,e820
  182. *
  183. * Wraps the underlying INT 15,e820 call so that the continuation
  184. * value (%ebx) is a 16-bit simple sequence counter (with the high 16
  185. * bits ignored), and termination is always via CF=1 rather than
  186. * %ebx=0.
  187. *
  188. ****************************************************************************
  189. */
  190. .section ".text16"
  191. get_underlying_e820:
  192. /* If the requested region is in the cache, return it */
  193. cmpw %bx, underlying_e820_index
  194. jne 1f
  195. pushw %di
  196. pushw %si
  197. movw $underlying_e820_cache, %si
  198. movw $20, %cx
  199. rep movsb
  200. popw %si
  201. popw %di
  202. movw $20, %cx
  203. incw %bx
  204. movl %edx, %eax
  205. ret
  206. 1:
  207. /* If the requested region is earlier than the cached region,
  208. * invalidate the cache.
  209. */
  210. cmpw %bx, underlying_e820_index
  211. jbe 1f
  212. movw $0xffff, underlying_e820_index
  213. 1:
  214. /* If the cache is invalid, reset the underlying %ebx */
  215. cmpw $0xffff, underlying_e820_index
  216. jne 1f
  217. andl $0, underlying_e820_ebx
  218. 1:
  219. /* If the cache is valid but the continuation value is zero,
  220. * this means that the previous underlying call returned with
  221. * %ebx=0. Return with CF=1 in this case.
  222. */
  223. cmpw $0xffff, underlying_e820_index
  224. je 1f
  225. cmpl $0, underlying_e820_ebx
  226. jne 1f
  227. stc
  228. ret
  229. 1:
  230. /* Get the next region into the cache */
  231. pushl %eax
  232. pushl %ebx
  233. pushl %ecx
  234. pushl %edx
  235. pushw %es
  236. pushw %di
  237. pushw %ds
  238. popw %es
  239. movw $underlying_e820_cache, %di
  240. movl $20, %ecx
  241. movl underlying_e820_ebx, %ebx
  242. stc
  243. pushfw
  244. lcall *%cs:int15_vector
  245. popw %di
  246. popw %es
  247. /* Check for error return from underlying e820 call */
  248. jc 1f /* CF set: error */
  249. cmpl $SMAP, %eax
  250. je 2f /* 'SMAP' missing: error */
  251. 1: /* An error occurred: return values returned by underlying e820 call */
  252. stc /* Force CF set if SMAP was missing */
  253. addr32 leal 16(%esp), %esp /* avoid changing other flags */
  254. ret
  255. 2: /* No error occurred */
  256. movl %ebx, underlying_e820_ebx
  257. popl %edx
  258. popl %ecx
  259. popl %ebx
  260. popl %eax
  261. /* Mark cache as containing this result */
  262. incw underlying_e820_index
  263. /* Loop until found */
  264. jmp get_underlying_e820
  265. .size get_underlying_e820, . - get_underlying_e820
  266. .section ".data16"
  267. underlying_e820_index:
  268. .word 0xffff /* Initialise to an invalid value */
  269. .size underlying_e820_index, . - underlying_e820_index
  270. .section ".bss16"
  271. underlying_e820_ebx:
  272. .long 0
  273. .size underlying_e820_ebx, . - underlying_e820_ebx
  274. .section ".bss16"
  275. underlying_e820_cache:
  276. .space 20
  277. .size underlying_e820_cache, . - underlying_e820_cache
  278. /****************************************************************************
  279. * Get windowed e820 region, without empty region stripping
  280. *
  281. * Parameters:
  282. * As for INT 15,e820
  283. * Returns:
  284. * As for INT 15,e820
  285. *
  286. * Wraps the underlying INT 15,e820 call so that each underlying
  287. * region is returned N times, windowed to fit within N visible-memory
  288. * windows. Termination is always via CF=1.
  289. *
  290. ****************************************************************************
  291. */
  292. .section ".text16"
  293. get_windowed_e820:
  294. /* Preserve registers */
  295. pushl %esi
  296. pushw %bp
  297. /* Split %ebx into %si:%bx, store original %bx in %bp */
  298. pushl %ebx
  299. popw %bp
  300. popw %si
  301. /* %si == 0 => start of memory_windows list */
  302. testw %si, %si
  303. jne 1f
  304. movw $memory_windows, %si
  305. 1:
  306. /* Get (cached) underlying e820 region to buffer */
  307. call get_underlying_e820
  308. jc 99f /* Abort on error */
  309. /* Preserve registers */
  310. pushal
  311. /* start => %edx:%eax, len => %ecx:%ebx */
  312. movl %es:0(%di), %eax
  313. movl %es:4(%di), %edx
  314. movl %es:8(%di), %ebx
  315. movl %es:12(%di), %ecx
  316. /* Truncate region to current window */
  317. call window_region
  318. 1: /* Store modified values in e820 map entry */
  319. movl %eax, %es:0(%di)
  320. movl %edx, %es:4(%di)
  321. movl %ebx, %es:8(%di)
  322. movl %ecx, %es:12(%di)
  323. /* Restore registers */
  324. popal
  325. /* Derive continuation value for next call */
  326. addw $16, %si
  327. cmpw $memory_windows_end, %si
  328. jne 1f
  329. /* End of memory windows: reset %si and allow %bx to continue */
  330. xorw %si, %si
  331. jmp 2f
  332. 1: /* More memory windows to go: restore original %bx */
  333. movw %bp, %bx
  334. 2: /* Construct %ebx from %si:%bx */
  335. pushw %si
  336. pushw %bx
  337. popl %ebx
  338. 98: /* Clear CF */
  339. clc
  340. 99: /* Restore registers and return */
  341. popw %bp
  342. popl %esi
  343. ret
  344. .size get_windowed_e820, . - get_windowed_e820
  345. /****************************************************************************
  346. * Get windowed e820 region, with empty region stripping
  347. *
  348. * Parameters:
  349. * As for INT 15,e820
  350. * Returns:
  351. * As for INT 15,e820
  352. *
  353. * Wraps the underlying INT 15,e820 call so that each underlying
  354. * region is returned up to N times, windowed to fit within N
  355. * visible-memory windows. Empty windows are never returned.
  356. * Termination is always via CF=1.
  357. *
  358. ****************************************************************************
  359. */
  360. .section ".text16"
  361. get_nonempty_e820:
  362. /* Record entry parameters */
  363. pushl %eax
  364. pushl %ecx
  365. pushl %edx
  366. /* Get next windowed region */
  367. call get_windowed_e820
  368. jc 99f /* abort on error */
  369. /* If region is non-empty, finish here */
  370. cmpl $0, %es:8(%di)
  371. jne 98f
  372. cmpl $0, %es:12(%di)
  373. jne 98f
  374. /* Region was empty: restore entry parameters and go to next region */
  375. popl %edx
  376. popl %ecx
  377. popl %eax
  378. jmp get_nonempty_e820
  379. 98: /* Clear CF */
  380. clc
  381. 99: /* Return values from underlying call */
  382. addr32 leal 12(%esp), %esp /* avoid changing flags */
  383. ret
  384. .size get_nonempty_e820, . - get_nonempty_e820
  385. /****************************************************************************
  386. * Get mangled e820 region, with empty region stripping
  387. *
  388. * Parameters:
  389. * As for INT 15,e820
  390. * Returns:
  391. * As for INT 15,e820
  392. *
  393. * Wraps the underlying INT 15,e820 call so that underlying regions
  394. * are windowed to the allowed memory regions. Empty regions are
  395. * stripped from the map. Termination is always via %ebx=0.
  396. *
  397. ****************************************************************************
  398. */
  399. .section ".text16"
  400. get_mangled_e820:
  401. /* Get a nonempty region */
  402. call get_nonempty_e820
  403. jc 99f /* Abort on error */
  404. /* Peek ahead to see if there are any further nonempty regions */
  405. pushal
  406. subw $20, %sp
  407. movl $0xe820, %eax
  408. movl $SMAP, %edx
  409. movl $20, %ecx
  410. pushw %ss
  411. popw %es
  412. movw %sp, %di
  413. call get_nonempty_e820
  414. addr32 leal 20(%esp), %esp /* avoid changing flags */
  415. popal
  416. jnc 99f /* There are further nonempty regions */
  417. /* No futher nonempty regions: zero %ebx and clear CF */
  418. xorl %ebx, %ebx
  419. 99: /* Return */
  420. ret
  421. .size get_mangled_e820, . - get_mangled_e820
  422. /****************************************************************************
  423. * INT 15,e820 handler
  424. ****************************************************************************
  425. */
  426. .section ".text16"
  427. int15_e820:
  428. pushw %ds
  429. pushw %cs:rm_ds
  430. popw %ds
  431. call get_mangled_e820
  432. popw %ds
  433. lret $2
  434. .size int15_e820, . - int15_e820
  435. /****************************************************************************
  436. * INT 15,e801 handler
  437. ****************************************************************************
  438. */
  439. .section ".text16"
  440. int15_e801:
  441. /* Call previous handler */
  442. pushfw
  443. lcall *%cs:int15_vector
  444. pushfw
  445. /* Edit result */
  446. pushw %ds
  447. pushw %cs:rm_ds
  448. popw %ds
  449. call patch_1m_16m
  450. xchgw %ax, %cx
  451. xchgw %bx, %dx
  452. call patch_1m_16m
  453. xchgw %ax, %cx
  454. xchgw %bx, %dx
  455. popw %ds
  456. /* Restore flags returned by previous handler and return */
  457. popfw
  458. lret $2
  459. .size int15_e801, . - int15_e801
  460. /****************************************************************************
  461. * INT 15,88 handler
  462. ****************************************************************************
  463. */
  464. .section ".text16"
  465. int15_88:
  466. /* Call previous handler */
  467. pushfw
  468. lcall *%cs:int15_vector
  469. pushfw
  470. /* Edit result */
  471. pushw %ds
  472. pushw %cs:rm_ds
  473. popw %ds
  474. call patch_1m
  475. popw %ds
  476. /* Restore flags returned by previous handler and return */
  477. popfw
  478. lret $2
  479. .size int15_88, . - int15_88
  480. /****************************************************************************
  481. * INT 15 handler
  482. ****************************************************************************
  483. */
  484. .section ".text16"
  485. .globl int15
  486. int15:
  487. /* See if we want to intercept this call */
  488. pushfw
  489. cmpw $0xe820, %ax
  490. jne 1f
  491. cmpl $SMAP, %edx
  492. jne 1f
  493. popfw
  494. jmp int15_e820
  495. 1: cmpw $0xe801, %ax
  496. jne 2f
  497. popfw
  498. jmp int15_e801
  499. 2: cmpb $0x88, %ah
  500. jne 3f
  501. popfw
  502. jmp int15_88
  503. 3: popfw
  504. ljmp *%cs:int15_vector
  505. .size int15, . - int15
  506. .section ".text16.data"
  507. .globl int15_vector
  508. int15_vector:
  509. .long 0
  510. .size int15_vector, . - int15_vector