load_store.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*---------------------------------------------------------------------------+
  2. | load_store.c |
  3. | |
  4. | This file contains most of the code to interpret the FPU instructions |
  5. | which load and store from user memory. |
  6. | |
  7. | Copyright (C) 1992,1993,1994,1997 |
  8. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
  9. | Australia. E-mail billm@suburbia.net |
  10. | |
  11. | |
  12. +---------------------------------------------------------------------------*/
  13. /*---------------------------------------------------------------------------+
  14. | Note: |
  15. | The file contains code which accesses user memory. |
  16. | Emulator static data may change when user memory is accessed, due to |
  17. | other processes using the emulator while swapping is in progress. |
  18. +---------------------------------------------------------------------------*/
  19. #include <asm/uaccess.h>
  20. #include "fpu_system.h"
  21. #include "exception.h"
  22. #include "fpu_emu.h"
  23. #include "status_w.h"
  24. #include "control_w.h"
  25. #define _NONE_ 0 /* st0_ptr etc not needed */
  26. #define _REG0_ 1 /* Will be storing st(0) */
  27. #define _PUSH_ 3 /* Need to check for space to push onto stack */
  28. #define _null_ 4 /* Function illegal or not implemented */
  29. #define pop_0() { FPU_settag0(TAG_Empty); top++; }
  30. /* index is a 5-bit value: (3-bit FPU_modrm.reg field | opcode[2,1]) */
  31. static u_char const type_table[32] = {
  32. _PUSH_, _PUSH_, _PUSH_, _PUSH_, /* /0: d9:fld f32, db:fild m32, dd:fld f64, df:fild m16 */
  33. _null_, _REG0_, _REG0_, _REG0_, /* /1: d9:undef, db,dd,df:fisttp m32/64/16 */
  34. _REG0_, _REG0_, _REG0_, _REG0_, /* /2: d9:fst f32, db:fist m32, dd:fst f64, df:fist m16 */
  35. _REG0_, _REG0_, _REG0_, _REG0_, /* /3: d9:fstp f32, db:fistp m32, dd:fstp f64, df:fistp m16 */
  36. _NONE_, _null_, _NONE_, _PUSH_,
  37. _NONE_, _PUSH_, _null_, _PUSH_,
  38. _NONE_, _null_, _NONE_, _REG0_,
  39. _NONE_, _REG0_, _NONE_, _REG0_
  40. };
  41. u_char const data_sizes_16[32] = {
  42. 4, 4, 8, 2,
  43. 0, 4, 8, 2, /* /1: d9:undef, db,dd,df:fisttp */
  44. 4, 4, 8, 2,
  45. 4, 4, 8, 2,
  46. 14, 0, 94, 10, 2, 10, 0, 8,
  47. 14, 0, 94, 10, 2, 10, 2, 8
  48. };
  49. static u_char const data_sizes_32[32] = {
  50. 4, 4, 8, 2,
  51. 0, 4, 8, 2, /* /1: d9:undef, db,dd,df:fisttp */
  52. 4, 4, 8, 2,
  53. 4, 4, 8, 2,
  54. 28, 0, 108, 10, 2, 10, 0, 8,
  55. 28, 0, 108, 10, 2, 10, 2, 8
  56. };
  57. int FPU_load_store(u_char type, fpu_addr_modes addr_modes,
  58. void __user * data_address)
  59. {
  60. FPU_REG loaded_data;
  61. FPU_REG *st0_ptr;
  62. u_char st0_tag = TAG_Empty; /* This is just to stop a gcc warning. */
  63. u_char loaded_tag;
  64. int sv_cw;
  65. st0_ptr = NULL; /* Initialized just to stop compiler warnings. */
  66. if (addr_modes.default_mode & PROTECTED) {
  67. if (addr_modes.default_mode == SEG32) {
  68. if (access_limit < data_sizes_32[type])
  69. math_abort(FPU_info, SIGSEGV);
  70. } else if (addr_modes.default_mode == PM16) {
  71. if (access_limit < data_sizes_16[type])
  72. math_abort(FPU_info, SIGSEGV);
  73. }
  74. #ifdef PARANOID
  75. else
  76. EXCEPTION(EX_INTERNAL | 0x140);
  77. #endif /* PARANOID */
  78. }
  79. switch (type_table[type]) {
  80. case _NONE_:
  81. break;
  82. case _REG0_:
  83. st0_ptr = &st(0); /* Some of these instructions pop after
  84. storing */
  85. st0_tag = FPU_gettag0();
  86. break;
  87. case _PUSH_:
  88. {
  89. if (FPU_gettagi(-1) != TAG_Empty) {
  90. FPU_stack_overflow();
  91. return 0;
  92. }
  93. top--;
  94. st0_ptr = &st(0);
  95. }
  96. break;
  97. case _null_:
  98. FPU_illegal();
  99. return 0;
  100. #ifdef PARANOID
  101. default:
  102. EXCEPTION(EX_INTERNAL | 0x141);
  103. return 0;
  104. #endif /* PARANOID */
  105. }
  106. switch (type) {
  107. /* type is a 5-bit value: (3-bit FPU_modrm.reg field | opcode[2,1]) */
  108. case 000: /* fld m32real (d9 /0) */
  109. clear_C1();
  110. loaded_tag =
  111. FPU_load_single((float __user *)data_address, &loaded_data);
  112. if ((loaded_tag == TAG_Special)
  113. && isNaN(&loaded_data)
  114. && (real_1op_NaN(&loaded_data) < 0)) {
  115. top++;
  116. break;
  117. }
  118. FPU_copy_to_reg0(&loaded_data, loaded_tag);
  119. break;
  120. case 001: /* fild m32int (db /0) */
  121. clear_C1();
  122. loaded_tag =
  123. FPU_load_int32((long __user *)data_address, &loaded_data);
  124. FPU_copy_to_reg0(&loaded_data, loaded_tag);
  125. break;
  126. case 002: /* fld m64real (dd /0) */
  127. clear_C1();
  128. loaded_tag =
  129. FPU_load_double((double __user *)data_address,
  130. &loaded_data);
  131. if ((loaded_tag == TAG_Special)
  132. && isNaN(&loaded_data)
  133. && (real_1op_NaN(&loaded_data) < 0)) {
  134. top++;
  135. break;
  136. }
  137. FPU_copy_to_reg0(&loaded_data, loaded_tag);
  138. break;
  139. case 003: /* fild m16int (df /0) */
  140. clear_C1();
  141. loaded_tag =
  142. FPU_load_int16((short __user *)data_address, &loaded_data);
  143. FPU_copy_to_reg0(&loaded_data, loaded_tag);
  144. break;
  145. /* case 004: undefined (d9 /1) */
  146. /* fisttp are enabled if CPUID(1).ECX(0) "sse3" is set */
  147. case 005: /* fisttp m32int (db /1) */
  148. clear_C1();
  149. sv_cw = control_word;
  150. control_word |= RC_CHOP;
  151. if (FPU_store_int32
  152. (st0_ptr, st0_tag, (long __user *)data_address))
  153. pop_0(); /* pop only if the number was actually stored
  154. (see the 80486 manual p16-28) */
  155. control_word = sv_cw;
  156. break;
  157. case 006: /* fisttp m64int (dd /1) */
  158. clear_C1();
  159. sv_cw = control_word;
  160. control_word |= RC_CHOP;
  161. if (FPU_store_int64
  162. (st0_ptr, st0_tag, (long long __user *)data_address))
  163. pop_0(); /* pop only if the number was actually stored
  164. (see the 80486 manual p16-28) */
  165. control_word = sv_cw;
  166. break;
  167. case 007: /* fisttp m16int (df /1) */
  168. clear_C1();
  169. sv_cw = control_word;
  170. control_word |= RC_CHOP;
  171. if (FPU_store_int16
  172. (st0_ptr, st0_tag, (short __user *)data_address))
  173. pop_0(); /* pop only if the number was actually stored
  174. (see the 80486 manual p16-28) */
  175. control_word = sv_cw;
  176. break;
  177. case 010: /* fst m32real */
  178. clear_C1();
  179. FPU_store_single(st0_ptr, st0_tag,
  180. (float __user *)data_address);
  181. break;
  182. case 011: /* fist m32int */
  183. clear_C1();
  184. FPU_store_int32(st0_ptr, st0_tag, (long __user *)data_address);
  185. break;
  186. case 012: /* fst m64real */
  187. clear_C1();
  188. FPU_store_double(st0_ptr, st0_tag,
  189. (double __user *)data_address);
  190. break;
  191. case 013: /* fist m16int */
  192. clear_C1();
  193. FPU_store_int16(st0_ptr, st0_tag, (short __user *)data_address);
  194. break;
  195. case 014: /* fstp m32real */
  196. clear_C1();
  197. if (FPU_store_single
  198. (st0_ptr, st0_tag, (float __user *)data_address))
  199. pop_0(); /* pop only if the number was actually stored
  200. (see the 80486 manual p16-28) */
  201. break;
  202. case 015: /* fistp m32int */
  203. clear_C1();
  204. if (FPU_store_int32
  205. (st0_ptr, st0_tag, (long __user *)data_address))
  206. pop_0(); /* pop only if the number was actually stored
  207. (see the 80486 manual p16-28) */
  208. break;
  209. case 016: /* fstp m64real */
  210. clear_C1();
  211. if (FPU_store_double
  212. (st0_ptr, st0_tag, (double __user *)data_address))
  213. pop_0(); /* pop only if the number was actually stored
  214. (see the 80486 manual p16-28) */
  215. break;
  216. case 017: /* fistp m16int */
  217. clear_C1();
  218. if (FPU_store_int16
  219. (st0_ptr, st0_tag, (short __user *)data_address))
  220. pop_0(); /* pop only if the number was actually stored
  221. (see the 80486 manual p16-28) */
  222. break;
  223. case 020: /* fldenv m14/28byte */
  224. fldenv(addr_modes, (u_char __user *) data_address);
  225. /* Ensure that the values just loaded are not changed by
  226. fix-up operations. */
  227. return 1;
  228. case 022: /* frstor m94/108byte */
  229. frstor(addr_modes, (u_char __user *) data_address);
  230. /* Ensure that the values just loaded are not changed by
  231. fix-up operations. */
  232. return 1;
  233. case 023: /* fbld m80dec */
  234. clear_C1();
  235. loaded_tag = FPU_load_bcd((u_char __user *) data_address);
  236. FPU_settag0(loaded_tag);
  237. break;
  238. case 024: /* fldcw */
  239. RE_ENTRANT_CHECK_OFF;
  240. FPU_access_ok(VERIFY_READ, data_address, 2);
  241. FPU_get_user(control_word,
  242. (unsigned short __user *)data_address);
  243. RE_ENTRANT_CHECK_ON;
  244. if (partial_status & ~control_word & CW_Exceptions)
  245. partial_status |= (SW_Summary | SW_Backward);
  246. else
  247. partial_status &= ~(SW_Summary | SW_Backward);
  248. #ifdef PECULIAR_486
  249. control_word |= 0x40; /* An 80486 appears to always set this bit */
  250. #endif /* PECULIAR_486 */
  251. return 1;
  252. case 025: /* fld m80real */
  253. clear_C1();
  254. loaded_tag =
  255. FPU_load_extended((long double __user *)data_address, 0);
  256. FPU_settag0(loaded_tag);
  257. break;
  258. case 027: /* fild m64int */
  259. clear_C1();
  260. loaded_tag = FPU_load_int64((long long __user *)data_address);
  261. if (loaded_tag == TAG_Error)
  262. return 0;
  263. FPU_settag0(loaded_tag);
  264. break;
  265. case 030: /* fstenv m14/28byte */
  266. fstenv(addr_modes, (u_char __user *) data_address);
  267. return 1;
  268. case 032: /* fsave */
  269. fsave(addr_modes, (u_char __user *) data_address);
  270. return 1;
  271. case 033: /* fbstp m80dec */
  272. clear_C1();
  273. if (FPU_store_bcd
  274. (st0_ptr, st0_tag, (u_char __user *) data_address))
  275. pop_0(); /* pop only if the number was actually stored
  276. (see the 80486 manual p16-28) */
  277. break;
  278. case 034: /* fstcw m16int */
  279. RE_ENTRANT_CHECK_OFF;
  280. FPU_access_ok(VERIFY_WRITE, data_address, 2);
  281. FPU_put_user(control_word,
  282. (unsigned short __user *)data_address);
  283. RE_ENTRANT_CHECK_ON;
  284. return 1;
  285. case 035: /* fstp m80real */
  286. clear_C1();
  287. if (FPU_store_extended
  288. (st0_ptr, st0_tag, (long double __user *)data_address))
  289. pop_0(); /* pop only if the number was actually stored
  290. (see the 80486 manual p16-28) */
  291. break;
  292. case 036: /* fstsw m2byte */
  293. RE_ENTRANT_CHECK_OFF;
  294. FPU_access_ok(VERIFY_WRITE, data_address, 2);
  295. FPU_put_user(status_word(),
  296. (unsigned short __user *)data_address);
  297. RE_ENTRANT_CHECK_ON;
  298. return 1;
  299. case 037: /* fistp m64int */
  300. clear_C1();
  301. if (FPU_store_int64
  302. (st0_ptr, st0_tag, (long long __user *)data_address))
  303. pop_0(); /* pop only if the number was actually stored
  304. (see the 80486 manual p16-28) */
  305. break;
  306. }
  307. return 0;
  308. }