get_address.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*---------------------------------------------------------------------------+
  2. | get_address.c |
  3. | |
  4. | Get the effective address from an FPU instruction. |
  5. | |
  6. | Copyright (C) 1992,1993,1994,1997 |
  7. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
  8. | Australia. E-mail billm@suburbia.net |
  9. | |
  10. | |
  11. +---------------------------------------------------------------------------*/
  12. /*---------------------------------------------------------------------------+
  13. | Note: |
  14. | The file contains code which accesses user memory. |
  15. | Emulator static data may change when user memory is accessed, due to |
  16. | other processes using the emulator while swapping is in progress. |
  17. +---------------------------------------------------------------------------*/
  18. #include <linux/stddef.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/desc.h>
  21. #include "fpu_system.h"
  22. #include "exception.h"
  23. #include "fpu_emu.h"
  24. #define FPU_WRITE_BIT 0x10
  25. static int reg_offset[] = {
  26. offsetof(struct info,___eax),
  27. offsetof(struct info,___ecx),
  28. offsetof(struct info,___edx),
  29. offsetof(struct info,___ebx),
  30. offsetof(struct info,___esp),
  31. offsetof(struct info,___ebp),
  32. offsetof(struct info,___esi),
  33. offsetof(struct info,___edi)
  34. };
  35. #define REG_(x) (*(long *)(reg_offset[(x)]+(u_char *) FPU_info))
  36. static int reg_offset_vm86[] = {
  37. offsetof(struct info,___cs),
  38. offsetof(struct info,___vm86_ds),
  39. offsetof(struct info,___vm86_es),
  40. offsetof(struct info,___vm86_fs),
  41. offsetof(struct info,___vm86_gs),
  42. offsetof(struct info,___ss),
  43. offsetof(struct info,___vm86_ds)
  44. };
  45. #define VM86_REG_(x) (*(unsigned short *) \
  46. (reg_offset_vm86[((unsigned)x)]+(u_char *) FPU_info))
  47. /* These are dummy, fs and gs are not saved on the stack. */
  48. #define ___FS ___ds
  49. #define ___GS ___ds
  50. static int reg_offset_pm[] = {
  51. offsetof(struct info,___cs),
  52. offsetof(struct info,___ds),
  53. offsetof(struct info,___es),
  54. offsetof(struct info,___FS),
  55. offsetof(struct info,___GS),
  56. offsetof(struct info,___ss),
  57. offsetof(struct info,___ds)
  58. };
  59. #define PM_REG_(x) (*(unsigned short *) \
  60. (reg_offset_pm[((unsigned)x)]+(u_char *) FPU_info))
  61. /* Decode the SIB byte. This function assumes mod != 0 */
  62. static int sib(int mod, unsigned long *fpu_eip)
  63. {
  64. u_char ss,index,base;
  65. long offset;
  66. RE_ENTRANT_CHECK_OFF;
  67. FPU_code_access_ok(1);
  68. FPU_get_user(base, (u_char __user *) (*fpu_eip)); /* The SIB byte */
  69. RE_ENTRANT_CHECK_ON;
  70. (*fpu_eip)++;
  71. ss = base >> 6;
  72. index = (base >> 3) & 7;
  73. base &= 7;
  74. if ((mod == 0) && (base == 5))
  75. offset = 0; /* No base register */
  76. else
  77. offset = REG_(base);
  78. if (index == 4)
  79. {
  80. /* No index register */
  81. /* A non-zero ss is illegal */
  82. if ( ss )
  83. EXCEPTION(EX_Invalid);
  84. }
  85. else
  86. {
  87. offset += (REG_(index)) << ss;
  88. }
  89. if (mod == 1)
  90. {
  91. /* 8 bit signed displacement */
  92. long displacement;
  93. RE_ENTRANT_CHECK_OFF;
  94. FPU_code_access_ok(1);
  95. FPU_get_user(displacement, (signed char __user *) (*fpu_eip));
  96. offset += displacement;
  97. RE_ENTRANT_CHECK_ON;
  98. (*fpu_eip)++;
  99. }
  100. else if (mod == 2 || base == 5) /* The second condition also has mod==0 */
  101. {
  102. /* 32 bit displacement */
  103. long displacement;
  104. RE_ENTRANT_CHECK_OFF;
  105. FPU_code_access_ok(4);
  106. FPU_get_user(displacement, (long __user *) (*fpu_eip));
  107. offset += displacement;
  108. RE_ENTRANT_CHECK_ON;
  109. (*fpu_eip) += 4;
  110. }
  111. return offset;
  112. }
  113. static unsigned long vm86_segment(u_char segment,
  114. struct address *addr)
  115. {
  116. segment--;
  117. #ifdef PARANOID
  118. if ( segment > PREFIX_SS_ )
  119. {
  120. EXCEPTION(EX_INTERNAL|0x130);
  121. math_abort(FPU_info,SIGSEGV);
  122. }
  123. #endif /* PARANOID */
  124. addr->selector = VM86_REG_(segment);
  125. return (unsigned long)VM86_REG_(segment) << 4;
  126. }
  127. /* This should work for 16 and 32 bit protected mode. */
  128. static long pm_address(u_char FPU_modrm, u_char segment,
  129. struct address *addr, long offset)
  130. {
  131. struct desc_struct descriptor;
  132. unsigned long base_address, limit, address, seg_top;
  133. unsigned short selector;
  134. segment--;
  135. #ifdef PARANOID
  136. /* segment is unsigned, so this also detects if segment was 0: */
  137. if ( segment > PREFIX_SS_ )
  138. {
  139. EXCEPTION(EX_INTERNAL|0x132);
  140. math_abort(FPU_info,SIGSEGV);
  141. }
  142. #endif /* PARANOID */
  143. switch ( segment )
  144. {
  145. /* fs and gs aren't used by the kernel, so they still have their
  146. user-space values. */
  147. case PREFIX_FS_-1:
  148. /* The cast is needed here to get gcc 2.8.0 to use a 16 bit register
  149. in the assembler statement. */
  150. __asm__("mov %%fs,%0":"=r" (selector));
  151. addr->selector = selector;
  152. break;
  153. case PREFIX_GS_-1:
  154. /* The cast is needed here to get gcc 2.8.0 to use a 16 bit register
  155. in the assembler statement. */
  156. __asm__("mov %%gs,%0":"=r" (selector));
  157. addr->selector = selector;
  158. break;
  159. default:
  160. addr->selector = PM_REG_(segment);
  161. }
  162. descriptor = LDT_DESCRIPTOR(PM_REG_(segment));
  163. base_address = SEG_BASE_ADDR(descriptor);
  164. address = base_address + offset;
  165. limit = base_address
  166. + (SEG_LIMIT(descriptor)+1) * SEG_GRANULARITY(descriptor) - 1;
  167. if ( limit < base_address ) limit = 0xffffffff;
  168. if ( SEG_EXPAND_DOWN(descriptor) )
  169. {
  170. if ( SEG_G_BIT(descriptor) )
  171. seg_top = 0xffffffff;
  172. else
  173. {
  174. seg_top = base_address + (1 << 20);
  175. if ( seg_top < base_address ) seg_top = 0xffffffff;
  176. }
  177. access_limit =
  178. (address <= limit) || (address >= seg_top) ? 0 :
  179. ((seg_top-address) >= 255 ? 255 : seg_top-address);
  180. }
  181. else
  182. {
  183. access_limit =
  184. (address > limit) || (address < base_address) ? 0 :
  185. ((limit-address) >= 254 ? 255 : limit-address+1);
  186. }
  187. if ( SEG_EXECUTE_ONLY(descriptor) ||
  188. (!SEG_WRITE_PERM(descriptor) && (FPU_modrm & FPU_WRITE_BIT)) )
  189. {
  190. access_limit = 0;
  191. }
  192. return address;
  193. }
  194. /*
  195. MOD R/M byte: MOD == 3 has a special use for the FPU
  196. SIB byte used iff R/M = 100b
  197. 7 6 5 4 3 2 1 0
  198. ..... ......... .........
  199. MOD OPCODE(2) R/M
  200. SIB byte
  201. 7 6 5 4 3 2 1 0
  202. ..... ......... .........
  203. SS INDEX BASE
  204. */
  205. void __user *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
  206. struct address *addr,
  207. fpu_addr_modes addr_modes)
  208. {
  209. u_char mod;
  210. unsigned rm = FPU_modrm & 7;
  211. long *cpu_reg_ptr;
  212. int address = 0; /* Initialized just to stop compiler warnings. */
  213. /* Memory accessed via the cs selector is write protected
  214. in `non-segmented' 32 bit protected mode. */
  215. if ( !addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
  216. && (addr_modes.override.segment == PREFIX_CS_) )
  217. {
  218. math_abort(FPU_info,SIGSEGV);
  219. }
  220. addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
  221. mod = (FPU_modrm >> 6) & 3;
  222. if (rm == 4 && mod != 3)
  223. {
  224. address = sib(mod, fpu_eip);
  225. }
  226. else
  227. {
  228. cpu_reg_ptr = & REG_(rm);
  229. switch (mod)
  230. {
  231. case 0:
  232. if (rm == 5)
  233. {
  234. /* Special case: disp32 */
  235. RE_ENTRANT_CHECK_OFF;
  236. FPU_code_access_ok(4);
  237. FPU_get_user(address, (unsigned long __user *) (*fpu_eip));
  238. (*fpu_eip) += 4;
  239. RE_ENTRANT_CHECK_ON;
  240. addr->offset = address;
  241. return (void __user *) address;
  242. }
  243. else
  244. {
  245. address = *cpu_reg_ptr; /* Just return the contents
  246. of the cpu register */
  247. addr->offset = address;
  248. return (void __user *) address;
  249. }
  250. case 1:
  251. /* 8 bit signed displacement */
  252. RE_ENTRANT_CHECK_OFF;
  253. FPU_code_access_ok(1);
  254. FPU_get_user(address, (signed char __user *) (*fpu_eip));
  255. RE_ENTRANT_CHECK_ON;
  256. (*fpu_eip)++;
  257. break;
  258. case 2:
  259. /* 32 bit displacement */
  260. RE_ENTRANT_CHECK_OFF;
  261. FPU_code_access_ok(4);
  262. FPU_get_user(address, (long __user *) (*fpu_eip));
  263. (*fpu_eip) += 4;
  264. RE_ENTRANT_CHECK_ON;
  265. break;
  266. case 3:
  267. /* Not legal for the FPU */
  268. EXCEPTION(EX_Invalid);
  269. }
  270. address += *cpu_reg_ptr;
  271. }
  272. addr->offset = address;
  273. switch ( addr_modes.default_mode )
  274. {
  275. case 0:
  276. break;
  277. case VM86:
  278. address += vm86_segment(addr_modes.override.segment, addr);
  279. break;
  280. case PM16:
  281. case SEG32:
  282. address = pm_address(FPU_modrm, addr_modes.override.segment,
  283. addr, address);
  284. break;
  285. default:
  286. EXCEPTION(EX_INTERNAL|0x133);
  287. }
  288. return (void __user *)address;
  289. }
  290. void __user *FPU_get_address_16(u_char FPU_modrm, unsigned long *fpu_eip,
  291. struct address *addr,
  292. fpu_addr_modes addr_modes)
  293. {
  294. u_char mod;
  295. unsigned rm = FPU_modrm & 7;
  296. int address = 0; /* Default used for mod == 0 */
  297. /* Memory accessed via the cs selector is write protected
  298. in `non-segmented' 32 bit protected mode. */
  299. if ( !addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
  300. && (addr_modes.override.segment == PREFIX_CS_) )
  301. {
  302. math_abort(FPU_info,SIGSEGV);
  303. }
  304. addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
  305. mod = (FPU_modrm >> 6) & 3;
  306. switch (mod)
  307. {
  308. case 0:
  309. if (rm == 6)
  310. {
  311. /* Special case: disp16 */
  312. RE_ENTRANT_CHECK_OFF;
  313. FPU_code_access_ok(2);
  314. FPU_get_user(address, (unsigned short __user *) (*fpu_eip));
  315. (*fpu_eip) += 2;
  316. RE_ENTRANT_CHECK_ON;
  317. goto add_segment;
  318. }
  319. break;
  320. case 1:
  321. /* 8 bit signed displacement */
  322. RE_ENTRANT_CHECK_OFF;
  323. FPU_code_access_ok(1);
  324. FPU_get_user(address, (signed char __user *) (*fpu_eip));
  325. RE_ENTRANT_CHECK_ON;
  326. (*fpu_eip)++;
  327. break;
  328. case 2:
  329. /* 16 bit displacement */
  330. RE_ENTRANT_CHECK_OFF;
  331. FPU_code_access_ok(2);
  332. FPU_get_user(address, (unsigned short __user *) (*fpu_eip));
  333. (*fpu_eip) += 2;
  334. RE_ENTRANT_CHECK_ON;
  335. break;
  336. case 3:
  337. /* Not legal for the FPU */
  338. EXCEPTION(EX_Invalid);
  339. break;
  340. }
  341. switch ( rm )
  342. {
  343. case 0:
  344. address += FPU_info->___ebx + FPU_info->___esi;
  345. break;
  346. case 1:
  347. address += FPU_info->___ebx + FPU_info->___edi;
  348. break;
  349. case 2:
  350. address += FPU_info->___ebp + FPU_info->___esi;
  351. if ( addr_modes.override.segment == PREFIX_DEFAULT )
  352. addr_modes.override.segment = PREFIX_SS_;
  353. break;
  354. case 3:
  355. address += FPU_info->___ebp + FPU_info->___edi;
  356. if ( addr_modes.override.segment == PREFIX_DEFAULT )
  357. addr_modes.override.segment = PREFIX_SS_;
  358. break;
  359. case 4:
  360. address += FPU_info->___esi;
  361. break;
  362. case 5:
  363. address += FPU_info->___edi;
  364. break;
  365. case 6:
  366. address += FPU_info->___ebp;
  367. if ( addr_modes.override.segment == PREFIX_DEFAULT )
  368. addr_modes.override.segment = PREFIX_SS_;
  369. break;
  370. case 7:
  371. address += FPU_info->___ebx;
  372. break;
  373. }
  374. add_segment:
  375. address &= 0xffff;
  376. addr->offset = address;
  377. switch ( addr_modes.default_mode )
  378. {
  379. case 0:
  380. break;
  381. case VM86:
  382. address += vm86_segment(addr_modes.override.segment, addr);
  383. break;
  384. case PM16:
  385. case SEG32:
  386. address = pm_address(FPU_modrm, addr_modes.override.segment,
  387. addr, address);
  388. break;
  389. default:
  390. EXCEPTION(EX_INTERNAL|0x131);
  391. }
  392. return (void __user *)address ;
  393. }