get_address.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 "fpu_system.h"
  21. #include "exception.h"
  22. #include "fpu_emu.h"
  23. #define FPU_WRITE_BIT 0x10
  24. static int reg_offset[] = {
  25. offsetof(struct pt_regs, ax),
  26. offsetof(struct pt_regs, cx),
  27. offsetof(struct pt_regs, dx),
  28. offsetof(struct pt_regs, bx),
  29. offsetof(struct pt_regs, sp),
  30. offsetof(struct pt_regs, bp),
  31. offsetof(struct pt_regs, si),
  32. offsetof(struct pt_regs, di)
  33. };
  34. #define REG_(x) (*(long *)(reg_offset[(x)] + (u_char *)FPU_info->regs))
  35. static int reg_offset_vm86[] = {
  36. offsetof(struct pt_regs, cs),
  37. offsetof(struct kernel_vm86_regs, ds),
  38. offsetof(struct kernel_vm86_regs, es),
  39. offsetof(struct kernel_vm86_regs, fs),
  40. offsetof(struct kernel_vm86_regs, gs),
  41. offsetof(struct pt_regs, ss),
  42. offsetof(struct kernel_vm86_regs, ds)
  43. };
  44. #define VM86_REG_(x) (*(unsigned short *) \
  45. (reg_offset_vm86[((unsigned)x)] + (u_char *)FPU_info->regs))
  46. static int reg_offset_pm[] = {
  47. offsetof(struct pt_regs, cs),
  48. offsetof(struct pt_regs, ds),
  49. offsetof(struct pt_regs, es),
  50. offsetof(struct pt_regs, fs),
  51. offsetof(struct pt_regs, ds), /* dummy, not saved on stack */
  52. offsetof(struct pt_regs, ss),
  53. offsetof(struct pt_regs, ds)
  54. };
  55. #define PM_REG_(x) (*(unsigned short *) \
  56. (reg_offset_pm[((unsigned)x)] + (u_char *)FPU_info->regs))
  57. /* Decode the SIB byte. This function assumes mod != 0 */
  58. static int sib(int mod, unsigned long *fpu_eip)
  59. {
  60. u_char ss, index, base;
  61. long offset;
  62. RE_ENTRANT_CHECK_OFF;
  63. FPU_code_access_ok(1);
  64. FPU_get_user(base, (u_char __user *) (*fpu_eip)); /* The SIB byte */
  65. RE_ENTRANT_CHECK_ON;
  66. (*fpu_eip)++;
  67. ss = base >> 6;
  68. index = (base >> 3) & 7;
  69. base &= 7;
  70. if ((mod == 0) && (base == 5))
  71. offset = 0; /* No base register */
  72. else
  73. offset = REG_(base);
  74. if (index == 4) {
  75. /* No index register */
  76. /* A non-zero ss is illegal */
  77. if (ss)
  78. EXCEPTION(EX_Invalid);
  79. } else {
  80. offset += (REG_(index)) << ss;
  81. }
  82. if (mod == 1) {
  83. /* 8 bit signed displacement */
  84. long displacement;
  85. RE_ENTRANT_CHECK_OFF;
  86. FPU_code_access_ok(1);
  87. FPU_get_user(displacement, (signed char __user *)(*fpu_eip));
  88. offset += displacement;
  89. RE_ENTRANT_CHECK_ON;
  90. (*fpu_eip)++;
  91. } else if (mod == 2 || base == 5) { /* The second condition also has mod==0 */
  92. /* 32 bit displacement */
  93. long displacement;
  94. RE_ENTRANT_CHECK_OFF;
  95. FPU_code_access_ok(4);
  96. FPU_get_user(displacement, (long __user *)(*fpu_eip));
  97. offset += displacement;
  98. RE_ENTRANT_CHECK_ON;
  99. (*fpu_eip) += 4;
  100. }
  101. return offset;
  102. }
  103. static unsigned long vm86_segment(u_char segment, struct address *addr)
  104. {
  105. segment--;
  106. #ifdef PARANOID
  107. if (segment > PREFIX_SS_) {
  108. EXCEPTION(EX_INTERNAL | 0x130);
  109. math_abort(FPU_info, SIGSEGV);
  110. }
  111. #endif /* PARANOID */
  112. addr->selector = VM86_REG_(segment);
  113. return (unsigned long)VM86_REG_(segment) << 4;
  114. }
  115. /* This should work for 16 and 32 bit protected mode. */
  116. static long pm_address(u_char FPU_modrm, u_char segment,
  117. struct address *addr, long offset)
  118. {
  119. struct desc_struct descriptor;
  120. unsigned long base_address, limit, address, seg_top;
  121. segment--;
  122. #ifdef PARANOID
  123. /* segment is unsigned, so this also detects if segment was 0: */
  124. if (segment > PREFIX_SS_) {
  125. EXCEPTION(EX_INTERNAL | 0x132);
  126. math_abort(FPU_info, SIGSEGV);
  127. }
  128. #endif /* PARANOID */
  129. switch (segment) {
  130. case PREFIX_GS_ - 1:
  131. /* user gs handling can be lazy, use special accessors */
  132. addr->selector = get_user_gs(FPU_info->regs);
  133. break;
  134. default:
  135. addr->selector = PM_REG_(segment);
  136. }
  137. descriptor = FPU_get_ldt_descriptor(segment);
  138. base_address = SEG_BASE_ADDR(descriptor);
  139. address = base_address + offset;
  140. limit = base_address
  141. + (SEG_LIMIT(descriptor) + 1) * SEG_GRANULARITY(descriptor) - 1;
  142. if (limit < base_address)
  143. limit = 0xffffffff;
  144. if (SEG_EXPAND_DOWN(descriptor)) {
  145. if (SEG_G_BIT(descriptor))
  146. seg_top = 0xffffffff;
  147. else {
  148. seg_top = base_address + (1 << 20);
  149. if (seg_top < base_address)
  150. seg_top = 0xffffffff;
  151. }
  152. access_limit =
  153. (address <= limit) || (address >= seg_top) ? 0 :
  154. ((seg_top - address) >= 255 ? 255 : seg_top - address);
  155. } else {
  156. access_limit =
  157. (address > limit) || (address < base_address) ? 0 :
  158. ((limit - address) >= 254 ? 255 : limit - address + 1);
  159. }
  160. if (SEG_EXECUTE_ONLY(descriptor) ||
  161. (!SEG_WRITE_PERM(descriptor) && (FPU_modrm & FPU_WRITE_BIT))) {
  162. access_limit = 0;
  163. }
  164. return address;
  165. }
  166. /*
  167. MOD R/M byte: MOD == 3 has a special use for the FPU
  168. SIB byte used iff R/M = 100b
  169. 7 6 5 4 3 2 1 0
  170. ..... ......... .........
  171. MOD OPCODE(2) R/M
  172. SIB byte
  173. 7 6 5 4 3 2 1 0
  174. ..... ......... .........
  175. SS INDEX BASE
  176. */
  177. void __user *FPU_get_address(u_char FPU_modrm, unsigned long *fpu_eip,
  178. struct address *addr, fpu_addr_modes addr_modes)
  179. {
  180. u_char mod;
  181. unsigned rm = FPU_modrm & 7;
  182. long *cpu_reg_ptr;
  183. int address = 0; /* Initialized just to stop compiler warnings. */
  184. /* Memory accessed via the cs selector is write protected
  185. in `non-segmented' 32 bit protected mode. */
  186. if (!addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
  187. && (addr_modes.override.segment == PREFIX_CS_)) {
  188. math_abort(FPU_info, SIGSEGV);
  189. }
  190. addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
  191. mod = (FPU_modrm >> 6) & 3;
  192. if (rm == 4 && mod != 3) {
  193. address = sib(mod, fpu_eip);
  194. } else {
  195. cpu_reg_ptr = &REG_(rm);
  196. switch (mod) {
  197. case 0:
  198. if (rm == 5) {
  199. /* Special case: disp32 */
  200. RE_ENTRANT_CHECK_OFF;
  201. FPU_code_access_ok(4);
  202. FPU_get_user(address,
  203. (unsigned long __user
  204. *)(*fpu_eip));
  205. (*fpu_eip) += 4;
  206. RE_ENTRANT_CHECK_ON;
  207. addr->offset = address;
  208. return (void __user *)address;
  209. } else {
  210. address = *cpu_reg_ptr; /* Just return the contents
  211. of the cpu register */
  212. addr->offset = address;
  213. return (void __user *)address;
  214. }
  215. case 1:
  216. /* 8 bit signed displacement */
  217. RE_ENTRANT_CHECK_OFF;
  218. FPU_code_access_ok(1);
  219. FPU_get_user(address, (signed char __user *)(*fpu_eip));
  220. RE_ENTRANT_CHECK_ON;
  221. (*fpu_eip)++;
  222. break;
  223. case 2:
  224. /* 32 bit displacement */
  225. RE_ENTRANT_CHECK_OFF;
  226. FPU_code_access_ok(4);
  227. FPU_get_user(address, (long __user *)(*fpu_eip));
  228. (*fpu_eip) += 4;
  229. RE_ENTRANT_CHECK_ON;
  230. break;
  231. case 3:
  232. /* Not legal for the FPU */
  233. EXCEPTION(EX_Invalid);
  234. }
  235. address += *cpu_reg_ptr;
  236. }
  237. addr->offset = address;
  238. switch (addr_modes.default_mode) {
  239. case 0:
  240. break;
  241. case VM86:
  242. address += vm86_segment(addr_modes.override.segment, addr);
  243. break;
  244. case PM16:
  245. case SEG32:
  246. address = pm_address(FPU_modrm, addr_modes.override.segment,
  247. addr, address);
  248. break;
  249. default:
  250. EXCEPTION(EX_INTERNAL | 0x133);
  251. }
  252. return (void __user *)address;
  253. }
  254. void __user *FPU_get_address_16(u_char FPU_modrm, unsigned long *fpu_eip,
  255. struct address *addr, fpu_addr_modes addr_modes)
  256. {
  257. u_char mod;
  258. unsigned rm = FPU_modrm & 7;
  259. int address = 0; /* Default used for mod == 0 */
  260. /* Memory accessed via the cs selector is write protected
  261. in `non-segmented' 32 bit protected mode. */
  262. if (!addr_modes.default_mode && (FPU_modrm & FPU_WRITE_BIT)
  263. && (addr_modes.override.segment == PREFIX_CS_)) {
  264. math_abort(FPU_info, SIGSEGV);
  265. }
  266. addr->selector = FPU_DS; /* Default, for 32 bit non-segmented mode. */
  267. mod = (FPU_modrm >> 6) & 3;
  268. switch (mod) {
  269. case 0:
  270. if (rm == 6) {
  271. /* Special case: disp16 */
  272. RE_ENTRANT_CHECK_OFF;
  273. FPU_code_access_ok(2);
  274. FPU_get_user(address,
  275. (unsigned short __user *)(*fpu_eip));
  276. (*fpu_eip) += 2;
  277. RE_ENTRANT_CHECK_ON;
  278. goto add_segment;
  279. }
  280. break;
  281. case 1:
  282. /* 8 bit signed displacement */
  283. RE_ENTRANT_CHECK_OFF;
  284. FPU_code_access_ok(1);
  285. FPU_get_user(address, (signed char __user *)(*fpu_eip));
  286. RE_ENTRANT_CHECK_ON;
  287. (*fpu_eip)++;
  288. break;
  289. case 2:
  290. /* 16 bit displacement */
  291. RE_ENTRANT_CHECK_OFF;
  292. FPU_code_access_ok(2);
  293. FPU_get_user(address, (unsigned short __user *)(*fpu_eip));
  294. (*fpu_eip) += 2;
  295. RE_ENTRANT_CHECK_ON;
  296. break;
  297. case 3:
  298. /* Not legal for the FPU */
  299. EXCEPTION(EX_Invalid);
  300. break;
  301. }
  302. switch (rm) {
  303. case 0:
  304. address += FPU_info->regs->bx + FPU_info->regs->si;
  305. break;
  306. case 1:
  307. address += FPU_info->regs->bx + FPU_info->regs->di;
  308. break;
  309. case 2:
  310. address += FPU_info->regs->bp + FPU_info->regs->si;
  311. if (addr_modes.override.segment == PREFIX_DEFAULT)
  312. addr_modes.override.segment = PREFIX_SS_;
  313. break;
  314. case 3:
  315. address += FPU_info->regs->bp + FPU_info->regs->di;
  316. if (addr_modes.override.segment == PREFIX_DEFAULT)
  317. addr_modes.override.segment = PREFIX_SS_;
  318. break;
  319. case 4:
  320. address += FPU_info->regs->si;
  321. break;
  322. case 5:
  323. address += FPU_info->regs->di;
  324. break;
  325. case 6:
  326. address += FPU_info->regs->bp;
  327. if (addr_modes.override.segment == PREFIX_DEFAULT)
  328. addr_modes.override.segment = PREFIX_SS_;
  329. break;
  330. case 7:
  331. address += FPU_info->regs->bx;
  332. break;
  333. }
  334. add_segment:
  335. address &= 0xffff;
  336. addr->offset = address;
  337. switch (addr_modes.default_mode) {
  338. case 0:
  339. break;
  340. case VM86:
  341. address += vm86_segment(addr_modes.override.segment, addr);
  342. break;
  343. case PM16:
  344. case SEG32:
  345. address = pm_address(FPU_modrm, addr_modes.override.segment,
  346. addr, address);
  347. break;
  348. default:
  349. EXCEPTION(EX_INTERNAL | 0x131);
  350. }
  351. return (void __user *)address;
  352. }