reg_ld_str.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*---------------------------------------------------------------------------+
  3. | reg_ld_str.c |
  4. | |
  5. | All of the functions which transfer data between user memory and FPU_REGs.|
  6. | |
  7. | Copyright (C) 1992,1993,1994,1996,1997 |
  8. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  9. | 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 "fpu_emu.h"
  20. #include <linux/uaccess.h>
  21. #include "fpu_system.h"
  22. #include "exception.h"
  23. #include "reg_constant.h"
  24. #include "control_w.h"
  25. #include "status_w.h"
  26. #define DOUBLE_Emax 1023 /* largest valid exponent */
  27. #define DOUBLE_Ebias 1023
  28. #define DOUBLE_Emin (-1022) /* smallest valid exponent */
  29. #define SINGLE_Emax 127 /* largest valid exponent */
  30. #define SINGLE_Ebias 127
  31. #define SINGLE_Emin (-126) /* smallest valid exponent */
  32. static u_char normalize_no_excep(FPU_REG *r, int exp, int sign)
  33. {
  34. u_char tag;
  35. setexponent16(r, exp);
  36. tag = FPU_normalize_nuo(r);
  37. stdexp(r);
  38. if (sign)
  39. setnegative(r);
  40. return tag;
  41. }
  42. int FPU_tagof(FPU_REG *ptr)
  43. {
  44. int exp;
  45. exp = exponent16(ptr) & 0x7fff;
  46. if (exp == 0) {
  47. if (!(ptr->sigh | ptr->sigl)) {
  48. return TAG_Zero;
  49. }
  50. /* The number is a de-normal or pseudodenormal. */
  51. return TAG_Special;
  52. }
  53. if (exp == 0x7fff) {
  54. /* Is an Infinity, a NaN, or an unsupported data type. */
  55. return TAG_Special;
  56. }
  57. if (!(ptr->sigh & 0x80000000)) {
  58. /* Unsupported data type. */
  59. /* Valid numbers have the ms bit set to 1. */
  60. /* Unnormal. */
  61. return TAG_Special;
  62. }
  63. return TAG_Valid;
  64. }
  65. /* Get a long double from user memory */
  66. int FPU_load_extended(long double __user *s, int stnr)
  67. {
  68. FPU_REG *sti_ptr = &st(stnr);
  69. RE_ENTRANT_CHECK_OFF;
  70. FPU_access_ok(VERIFY_READ, s, 10);
  71. __copy_from_user(sti_ptr, s, 10);
  72. RE_ENTRANT_CHECK_ON;
  73. return FPU_tagof(sti_ptr);
  74. }
  75. /* Get a double from user memory */
  76. int FPU_load_double(double __user *dfloat, FPU_REG *loaded_data)
  77. {
  78. int exp, tag, negative;
  79. unsigned m64, l64;
  80. RE_ENTRANT_CHECK_OFF;
  81. FPU_access_ok(VERIFY_READ, dfloat, 8);
  82. FPU_get_user(m64, 1 + (unsigned long __user *)dfloat);
  83. FPU_get_user(l64, (unsigned long __user *)dfloat);
  84. RE_ENTRANT_CHECK_ON;
  85. negative = (m64 & 0x80000000) ? SIGN_Negative : SIGN_Positive;
  86. exp = ((m64 & 0x7ff00000) >> 20) - DOUBLE_Ebias + EXTENDED_Ebias;
  87. m64 &= 0xfffff;
  88. if (exp > DOUBLE_Emax + EXTENDED_Ebias) {
  89. /* Infinity or NaN */
  90. if ((m64 == 0) && (l64 == 0)) {
  91. /* +- infinity */
  92. loaded_data->sigh = 0x80000000;
  93. loaded_data->sigl = 0x00000000;
  94. exp = EXP_Infinity + EXTENDED_Ebias;
  95. tag = TAG_Special;
  96. } else {
  97. /* Must be a signaling or quiet NaN */
  98. exp = EXP_NaN + EXTENDED_Ebias;
  99. loaded_data->sigh = (m64 << 11) | 0x80000000;
  100. loaded_data->sigh |= l64 >> 21;
  101. loaded_data->sigl = l64 << 11;
  102. tag = TAG_Special; /* The calling function must look for NaNs */
  103. }
  104. } else if (exp < DOUBLE_Emin + EXTENDED_Ebias) {
  105. /* Zero or de-normal */
  106. if ((m64 == 0) && (l64 == 0)) {
  107. /* Zero */
  108. reg_copy(&CONST_Z, loaded_data);
  109. exp = 0;
  110. tag = TAG_Zero;
  111. } else {
  112. /* De-normal */
  113. loaded_data->sigh = m64 << 11;
  114. loaded_data->sigh |= l64 >> 21;
  115. loaded_data->sigl = l64 << 11;
  116. return normalize_no_excep(loaded_data, DOUBLE_Emin,
  117. negative)
  118. | (denormal_operand() < 0 ? FPU_Exception : 0);
  119. }
  120. } else {
  121. loaded_data->sigh = (m64 << 11) | 0x80000000;
  122. loaded_data->sigh |= l64 >> 21;
  123. loaded_data->sigl = l64 << 11;
  124. tag = TAG_Valid;
  125. }
  126. setexponent16(loaded_data, exp | negative);
  127. return tag;
  128. }
  129. /* Get a float from user memory */
  130. int FPU_load_single(float __user *single, FPU_REG *loaded_data)
  131. {
  132. unsigned m32;
  133. int exp, tag, negative;
  134. RE_ENTRANT_CHECK_OFF;
  135. FPU_access_ok(VERIFY_READ, single, 4);
  136. FPU_get_user(m32, (unsigned long __user *)single);
  137. RE_ENTRANT_CHECK_ON;
  138. negative = (m32 & 0x80000000) ? SIGN_Negative : SIGN_Positive;
  139. if (!(m32 & 0x7fffffff)) {
  140. /* Zero */
  141. reg_copy(&CONST_Z, loaded_data);
  142. addexponent(loaded_data, negative);
  143. return TAG_Zero;
  144. }
  145. exp = ((m32 & 0x7f800000) >> 23) - SINGLE_Ebias + EXTENDED_Ebias;
  146. m32 = (m32 & 0x7fffff) << 8;
  147. if (exp < SINGLE_Emin + EXTENDED_Ebias) {
  148. /* De-normals */
  149. loaded_data->sigh = m32;
  150. loaded_data->sigl = 0;
  151. return normalize_no_excep(loaded_data, SINGLE_Emin, negative)
  152. | (denormal_operand() < 0 ? FPU_Exception : 0);
  153. } else if (exp > SINGLE_Emax + EXTENDED_Ebias) {
  154. /* Infinity or NaN */
  155. if (m32 == 0) {
  156. /* +- infinity */
  157. loaded_data->sigh = 0x80000000;
  158. loaded_data->sigl = 0x00000000;
  159. exp = EXP_Infinity + EXTENDED_Ebias;
  160. tag = TAG_Special;
  161. } else {
  162. /* Must be a signaling or quiet NaN */
  163. exp = EXP_NaN + EXTENDED_Ebias;
  164. loaded_data->sigh = m32 | 0x80000000;
  165. loaded_data->sigl = 0;
  166. tag = TAG_Special; /* The calling function must look for NaNs */
  167. }
  168. } else {
  169. loaded_data->sigh = m32 | 0x80000000;
  170. loaded_data->sigl = 0;
  171. tag = TAG_Valid;
  172. }
  173. setexponent16(loaded_data, exp | negative); /* Set the sign. */
  174. return tag;
  175. }
  176. /* Get a long long from user memory */
  177. int FPU_load_int64(long long __user *_s)
  178. {
  179. long long s;
  180. int sign;
  181. FPU_REG *st0_ptr = &st(0);
  182. RE_ENTRANT_CHECK_OFF;
  183. FPU_access_ok(VERIFY_READ, _s, 8);
  184. if (copy_from_user(&s, _s, 8))
  185. FPU_abort;
  186. RE_ENTRANT_CHECK_ON;
  187. if (s == 0) {
  188. reg_copy(&CONST_Z, st0_ptr);
  189. return TAG_Zero;
  190. }
  191. if (s > 0)
  192. sign = SIGN_Positive;
  193. else {
  194. s = -s;
  195. sign = SIGN_Negative;
  196. }
  197. significand(st0_ptr) = s;
  198. return normalize_no_excep(st0_ptr, 63, sign);
  199. }
  200. /* Get a long from user memory */
  201. int FPU_load_int32(long __user *_s, FPU_REG *loaded_data)
  202. {
  203. long s;
  204. int negative;
  205. RE_ENTRANT_CHECK_OFF;
  206. FPU_access_ok(VERIFY_READ, _s, 4);
  207. FPU_get_user(s, _s);
  208. RE_ENTRANT_CHECK_ON;
  209. if (s == 0) {
  210. reg_copy(&CONST_Z, loaded_data);
  211. return TAG_Zero;
  212. }
  213. if (s > 0)
  214. negative = SIGN_Positive;
  215. else {
  216. s = -s;
  217. negative = SIGN_Negative;
  218. }
  219. loaded_data->sigh = s;
  220. loaded_data->sigl = 0;
  221. return normalize_no_excep(loaded_data, 31, negative);
  222. }
  223. /* Get a short from user memory */
  224. int FPU_load_int16(short __user *_s, FPU_REG *loaded_data)
  225. {
  226. int s, negative;
  227. RE_ENTRANT_CHECK_OFF;
  228. FPU_access_ok(VERIFY_READ, _s, 2);
  229. /* Cast as short to get the sign extended. */
  230. FPU_get_user(s, _s);
  231. RE_ENTRANT_CHECK_ON;
  232. if (s == 0) {
  233. reg_copy(&CONST_Z, loaded_data);
  234. return TAG_Zero;
  235. }
  236. if (s > 0)
  237. negative = SIGN_Positive;
  238. else {
  239. s = -s;
  240. negative = SIGN_Negative;
  241. }
  242. loaded_data->sigh = s << 16;
  243. loaded_data->sigl = 0;
  244. return normalize_no_excep(loaded_data, 15, negative);
  245. }
  246. /* Get a packed bcd array from user memory */
  247. int FPU_load_bcd(u_char __user *s)
  248. {
  249. FPU_REG *st0_ptr = &st(0);
  250. int pos;
  251. u_char bcd;
  252. long long l = 0;
  253. int sign;
  254. RE_ENTRANT_CHECK_OFF;
  255. FPU_access_ok(VERIFY_READ, s, 10);
  256. RE_ENTRANT_CHECK_ON;
  257. for (pos = 8; pos >= 0; pos--) {
  258. l *= 10;
  259. RE_ENTRANT_CHECK_OFF;
  260. FPU_get_user(bcd, s + pos);
  261. RE_ENTRANT_CHECK_ON;
  262. l += bcd >> 4;
  263. l *= 10;
  264. l += bcd & 0x0f;
  265. }
  266. RE_ENTRANT_CHECK_OFF;
  267. FPU_get_user(sign, s + 9);
  268. sign = sign & 0x80 ? SIGN_Negative : SIGN_Positive;
  269. RE_ENTRANT_CHECK_ON;
  270. if (l == 0) {
  271. reg_copy(&CONST_Z, st0_ptr);
  272. addexponent(st0_ptr, sign); /* Set the sign. */
  273. return TAG_Zero;
  274. } else {
  275. significand(st0_ptr) = l;
  276. return normalize_no_excep(st0_ptr, 63, sign);
  277. }
  278. }
  279. /*===========================================================================*/
  280. /* Put a long double into user memory */
  281. int FPU_store_extended(FPU_REG *st0_ptr, u_char st0_tag,
  282. long double __user * d)
  283. {
  284. /*
  285. The only exception raised by an attempt to store to an
  286. extended format is the Invalid Stack exception, i.e.
  287. attempting to store from an empty register.
  288. */
  289. if (st0_tag != TAG_Empty) {
  290. RE_ENTRANT_CHECK_OFF;
  291. FPU_access_ok(VERIFY_WRITE, d, 10);
  292. FPU_put_user(st0_ptr->sigl, (unsigned long __user *)d);
  293. FPU_put_user(st0_ptr->sigh,
  294. (unsigned long __user *)((u_char __user *) d + 4));
  295. FPU_put_user(exponent16(st0_ptr),
  296. (unsigned short __user *)((u_char __user *) d +
  297. 8));
  298. RE_ENTRANT_CHECK_ON;
  299. return 1;
  300. }
  301. /* Empty register (stack underflow) */
  302. EXCEPTION(EX_StackUnder);
  303. if (control_word & CW_Invalid) {
  304. /* The masked response */
  305. /* Put out the QNaN indefinite */
  306. RE_ENTRANT_CHECK_OFF;
  307. FPU_access_ok(VERIFY_WRITE, d, 10);
  308. FPU_put_user(0, (unsigned long __user *)d);
  309. FPU_put_user(0xc0000000, 1 + (unsigned long __user *)d);
  310. FPU_put_user(0xffff, 4 + (short __user *)d);
  311. RE_ENTRANT_CHECK_ON;
  312. return 1;
  313. } else
  314. return 0;
  315. }
  316. /* Put a double into user memory */
  317. int FPU_store_double(FPU_REG *st0_ptr, u_char st0_tag, double __user *dfloat)
  318. {
  319. unsigned long l[2];
  320. unsigned long increment = 0; /* avoid gcc warnings */
  321. int precision_loss;
  322. int exp;
  323. FPU_REG tmp;
  324. l[0] = 0;
  325. l[1] = 0;
  326. if (st0_tag == TAG_Valid) {
  327. reg_copy(st0_ptr, &tmp);
  328. exp = exponent(&tmp);
  329. if (exp < DOUBLE_Emin) { /* It may be a denormal */
  330. addexponent(&tmp, -DOUBLE_Emin + 52); /* largest exp to be 51 */
  331. denormal_arg:
  332. if ((precision_loss = FPU_round_to_int(&tmp, st0_tag))) {
  333. #ifdef PECULIAR_486
  334. /* Did it round to a non-denormal ? */
  335. /* This behaviour might be regarded as peculiar, it appears
  336. that the 80486 rounds to the dest precision, then
  337. converts to decide underflow. */
  338. if (!
  339. ((tmp.sigh == 0x00100000) && (tmp.sigl == 0)
  340. && (st0_ptr->sigl & 0x000007ff)))
  341. #endif /* PECULIAR_486 */
  342. {
  343. EXCEPTION(EX_Underflow);
  344. /* This is a special case: see sec 16.2.5.1 of
  345. the 80486 book */
  346. if (!(control_word & CW_Underflow))
  347. return 0;
  348. }
  349. EXCEPTION(precision_loss);
  350. if (!(control_word & CW_Precision))
  351. return 0;
  352. }
  353. l[0] = tmp.sigl;
  354. l[1] = tmp.sigh;
  355. } else {
  356. if (tmp.sigl & 0x000007ff) {
  357. precision_loss = 1;
  358. switch (control_word & CW_RC) {
  359. case RC_RND:
  360. /* Rounding can get a little messy.. */
  361. increment = ((tmp.sigl & 0x7ff) > 0x400) | /* nearest */
  362. ((tmp.sigl & 0xc00) == 0xc00); /* odd -> even */
  363. break;
  364. case RC_DOWN: /* towards -infinity */
  365. increment =
  366. signpositive(&tmp) ? 0 : tmp.
  367. sigl & 0x7ff;
  368. break;
  369. case RC_UP: /* towards +infinity */
  370. increment =
  371. signpositive(&tmp) ? tmp.
  372. sigl & 0x7ff : 0;
  373. break;
  374. case RC_CHOP:
  375. increment = 0;
  376. break;
  377. }
  378. /* Truncate the mantissa */
  379. tmp.sigl &= 0xfffff800;
  380. if (increment) {
  381. if (tmp.sigl >= 0xfffff800) {
  382. /* the sigl part overflows */
  383. if (tmp.sigh == 0xffffffff) {
  384. /* The sigh part overflows */
  385. tmp.sigh = 0x80000000;
  386. exp++;
  387. if (exp >= EXP_OVER)
  388. goto overflow;
  389. } else {
  390. tmp.sigh++;
  391. }
  392. tmp.sigl = 0x00000000;
  393. } else {
  394. /* We only need to increment sigl */
  395. tmp.sigl += 0x00000800;
  396. }
  397. }
  398. } else
  399. precision_loss = 0;
  400. l[0] = (tmp.sigl >> 11) | (tmp.sigh << 21);
  401. l[1] = ((tmp.sigh >> 11) & 0xfffff);
  402. if (exp > DOUBLE_Emax) {
  403. overflow:
  404. EXCEPTION(EX_Overflow);
  405. if (!(control_word & CW_Overflow))
  406. return 0;
  407. set_precision_flag_up();
  408. if (!(control_word & CW_Precision))
  409. return 0;
  410. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  411. /* Overflow to infinity */
  412. l[1] = 0x7ff00000; /* Set to + INF */
  413. } else {
  414. if (precision_loss) {
  415. if (increment)
  416. set_precision_flag_up();
  417. else
  418. set_precision_flag_down();
  419. }
  420. /* Add the exponent */
  421. l[1] |= (((exp + DOUBLE_Ebias) & 0x7ff) << 20);
  422. }
  423. }
  424. } else if (st0_tag == TAG_Zero) {
  425. /* Number is zero */
  426. } else if (st0_tag == TAG_Special) {
  427. st0_tag = FPU_Special(st0_ptr);
  428. if (st0_tag == TW_Denormal) {
  429. /* A denormal will always underflow. */
  430. #ifndef PECULIAR_486
  431. /* An 80486 is supposed to be able to generate
  432. a denormal exception here, but... */
  433. /* Underflow has priority. */
  434. if (control_word & CW_Underflow)
  435. denormal_operand();
  436. #endif /* PECULIAR_486 */
  437. reg_copy(st0_ptr, &tmp);
  438. goto denormal_arg;
  439. } else if (st0_tag == TW_Infinity) {
  440. l[1] = 0x7ff00000;
  441. } else if (st0_tag == TW_NaN) {
  442. /* Is it really a NaN ? */
  443. if ((exponent(st0_ptr) == EXP_OVER)
  444. && (st0_ptr->sigh & 0x80000000)) {
  445. /* See if we can get a valid NaN from the FPU_REG */
  446. l[0] =
  447. (st0_ptr->sigl >> 11) | (st0_ptr->
  448. sigh << 21);
  449. l[1] = ((st0_ptr->sigh >> 11) & 0xfffff);
  450. if (!(st0_ptr->sigh & 0x40000000)) {
  451. /* It is a signalling NaN */
  452. EXCEPTION(EX_Invalid);
  453. if (!(control_word & CW_Invalid))
  454. return 0;
  455. l[1] |= (0x40000000 >> 11);
  456. }
  457. l[1] |= 0x7ff00000;
  458. } else {
  459. /* It is an unsupported data type */
  460. EXCEPTION(EX_Invalid);
  461. if (!(control_word & CW_Invalid))
  462. return 0;
  463. l[1] = 0xfff80000;
  464. }
  465. }
  466. } else if (st0_tag == TAG_Empty) {
  467. /* Empty register (stack underflow) */
  468. EXCEPTION(EX_StackUnder);
  469. if (control_word & CW_Invalid) {
  470. /* The masked response */
  471. /* Put out the QNaN indefinite */
  472. RE_ENTRANT_CHECK_OFF;
  473. FPU_access_ok(VERIFY_WRITE, dfloat, 8);
  474. FPU_put_user(0, (unsigned long __user *)dfloat);
  475. FPU_put_user(0xfff80000,
  476. 1 + (unsigned long __user *)dfloat);
  477. RE_ENTRANT_CHECK_ON;
  478. return 1;
  479. } else
  480. return 0;
  481. }
  482. if (getsign(st0_ptr))
  483. l[1] |= 0x80000000;
  484. RE_ENTRANT_CHECK_OFF;
  485. FPU_access_ok(VERIFY_WRITE, dfloat, 8);
  486. FPU_put_user(l[0], (unsigned long __user *)dfloat);
  487. FPU_put_user(l[1], 1 + (unsigned long __user *)dfloat);
  488. RE_ENTRANT_CHECK_ON;
  489. return 1;
  490. }
  491. /* Put a float into user memory */
  492. int FPU_store_single(FPU_REG *st0_ptr, u_char st0_tag, float __user *single)
  493. {
  494. long templ = 0;
  495. unsigned long increment = 0; /* avoid gcc warnings */
  496. int precision_loss;
  497. int exp;
  498. FPU_REG tmp;
  499. if (st0_tag == TAG_Valid) {
  500. reg_copy(st0_ptr, &tmp);
  501. exp = exponent(&tmp);
  502. if (exp < SINGLE_Emin) {
  503. addexponent(&tmp, -SINGLE_Emin + 23); /* largest exp to be 22 */
  504. denormal_arg:
  505. if ((precision_loss = FPU_round_to_int(&tmp, st0_tag))) {
  506. #ifdef PECULIAR_486
  507. /* Did it round to a non-denormal ? */
  508. /* This behaviour might be regarded as peculiar, it appears
  509. that the 80486 rounds to the dest precision, then
  510. converts to decide underflow. */
  511. if (!((tmp.sigl == 0x00800000) &&
  512. ((st0_ptr->sigh & 0x000000ff)
  513. || st0_ptr->sigl)))
  514. #endif /* PECULIAR_486 */
  515. {
  516. EXCEPTION(EX_Underflow);
  517. /* This is a special case: see sec 16.2.5.1 of
  518. the 80486 book */
  519. if (!(control_word & CW_Underflow))
  520. return 0;
  521. }
  522. EXCEPTION(precision_loss);
  523. if (!(control_word & CW_Precision))
  524. return 0;
  525. }
  526. templ = tmp.sigl;
  527. } else {
  528. if (tmp.sigl | (tmp.sigh & 0x000000ff)) {
  529. unsigned long sigh = tmp.sigh;
  530. unsigned long sigl = tmp.sigl;
  531. precision_loss = 1;
  532. switch (control_word & CW_RC) {
  533. case RC_RND:
  534. increment = ((sigh & 0xff) > 0x80) /* more than half */
  535. ||(((sigh & 0xff) == 0x80) && sigl) /* more than half */
  536. ||((sigh & 0x180) == 0x180); /* round to even */
  537. break;
  538. case RC_DOWN: /* towards -infinity */
  539. increment = signpositive(&tmp)
  540. ? 0 : (sigl | (sigh & 0xff));
  541. break;
  542. case RC_UP: /* towards +infinity */
  543. increment = signpositive(&tmp)
  544. ? (sigl | (sigh & 0xff)) : 0;
  545. break;
  546. case RC_CHOP:
  547. increment = 0;
  548. break;
  549. }
  550. /* Truncate part of the mantissa */
  551. tmp.sigl = 0;
  552. if (increment) {
  553. if (sigh >= 0xffffff00) {
  554. /* The sigh part overflows */
  555. tmp.sigh = 0x80000000;
  556. exp++;
  557. if (exp >= EXP_OVER)
  558. goto overflow;
  559. } else {
  560. tmp.sigh &= 0xffffff00;
  561. tmp.sigh += 0x100;
  562. }
  563. } else {
  564. tmp.sigh &= 0xffffff00; /* Finish the truncation */
  565. }
  566. } else
  567. precision_loss = 0;
  568. templ = (tmp.sigh >> 8) & 0x007fffff;
  569. if (exp > SINGLE_Emax) {
  570. overflow:
  571. EXCEPTION(EX_Overflow);
  572. if (!(control_word & CW_Overflow))
  573. return 0;
  574. set_precision_flag_up();
  575. if (!(control_word & CW_Precision))
  576. return 0;
  577. /* This is a special case: see sec 16.2.5.1 of the 80486 book. */
  578. /* Masked response is overflow to infinity. */
  579. templ = 0x7f800000;
  580. } else {
  581. if (precision_loss) {
  582. if (increment)
  583. set_precision_flag_up();
  584. else
  585. set_precision_flag_down();
  586. }
  587. /* Add the exponent */
  588. templ |= ((exp + SINGLE_Ebias) & 0xff) << 23;
  589. }
  590. }
  591. } else if (st0_tag == TAG_Zero) {
  592. templ = 0;
  593. } else if (st0_tag == TAG_Special) {
  594. st0_tag = FPU_Special(st0_ptr);
  595. if (st0_tag == TW_Denormal) {
  596. reg_copy(st0_ptr, &tmp);
  597. /* A denormal will always underflow. */
  598. #ifndef PECULIAR_486
  599. /* An 80486 is supposed to be able to generate
  600. a denormal exception here, but... */
  601. /* Underflow has priority. */
  602. if (control_word & CW_Underflow)
  603. denormal_operand();
  604. #endif /* PECULIAR_486 */
  605. goto denormal_arg;
  606. } else if (st0_tag == TW_Infinity) {
  607. templ = 0x7f800000;
  608. } else if (st0_tag == TW_NaN) {
  609. /* Is it really a NaN ? */
  610. if ((exponent(st0_ptr) == EXP_OVER)
  611. && (st0_ptr->sigh & 0x80000000)) {
  612. /* See if we can get a valid NaN from the FPU_REG */
  613. templ = st0_ptr->sigh >> 8;
  614. if (!(st0_ptr->sigh & 0x40000000)) {
  615. /* It is a signalling NaN */
  616. EXCEPTION(EX_Invalid);
  617. if (!(control_word & CW_Invalid))
  618. return 0;
  619. templ |= (0x40000000 >> 8);
  620. }
  621. templ |= 0x7f800000;
  622. } else {
  623. /* It is an unsupported data type */
  624. EXCEPTION(EX_Invalid);
  625. if (!(control_word & CW_Invalid))
  626. return 0;
  627. templ = 0xffc00000;
  628. }
  629. }
  630. #ifdef PARANOID
  631. else {
  632. EXCEPTION(EX_INTERNAL | 0x164);
  633. return 0;
  634. }
  635. #endif
  636. } else if (st0_tag == TAG_Empty) {
  637. /* Empty register (stack underflow) */
  638. EXCEPTION(EX_StackUnder);
  639. if (control_word & EX_Invalid) {
  640. /* The masked response */
  641. /* Put out the QNaN indefinite */
  642. RE_ENTRANT_CHECK_OFF;
  643. FPU_access_ok(VERIFY_WRITE, single, 4);
  644. FPU_put_user(0xffc00000,
  645. (unsigned long __user *)single);
  646. RE_ENTRANT_CHECK_ON;
  647. return 1;
  648. } else
  649. return 0;
  650. }
  651. #ifdef PARANOID
  652. else {
  653. EXCEPTION(EX_INTERNAL | 0x163);
  654. return 0;
  655. }
  656. #endif
  657. if (getsign(st0_ptr))
  658. templ |= 0x80000000;
  659. RE_ENTRANT_CHECK_OFF;
  660. FPU_access_ok(VERIFY_WRITE, single, 4);
  661. FPU_put_user(templ, (unsigned long __user *)single);
  662. RE_ENTRANT_CHECK_ON;
  663. return 1;
  664. }
  665. /* Put a long long into user memory */
  666. int FPU_store_int64(FPU_REG *st0_ptr, u_char st0_tag, long long __user *d)
  667. {
  668. FPU_REG t;
  669. long long tll;
  670. int precision_loss;
  671. if (st0_tag == TAG_Empty) {
  672. /* Empty register (stack underflow) */
  673. EXCEPTION(EX_StackUnder);
  674. goto invalid_operand;
  675. } else if (st0_tag == TAG_Special) {
  676. st0_tag = FPU_Special(st0_ptr);
  677. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  678. EXCEPTION(EX_Invalid);
  679. goto invalid_operand;
  680. }
  681. }
  682. reg_copy(st0_ptr, &t);
  683. precision_loss = FPU_round_to_int(&t, st0_tag);
  684. ((long *)&tll)[0] = t.sigl;
  685. ((long *)&tll)[1] = t.sigh;
  686. if ((precision_loss == 1) ||
  687. ((t.sigh & 0x80000000) &&
  688. !((t.sigh == 0x80000000) && (t.sigl == 0) && signnegative(&t)))) {
  689. EXCEPTION(EX_Invalid);
  690. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  691. invalid_operand:
  692. if (control_word & EX_Invalid) {
  693. /* Produce something like QNaN "indefinite" */
  694. tll = 0x8000000000000000LL;
  695. } else
  696. return 0;
  697. } else {
  698. if (precision_loss)
  699. set_precision_flag(precision_loss);
  700. if (signnegative(&t))
  701. tll = -tll;
  702. }
  703. RE_ENTRANT_CHECK_OFF;
  704. FPU_access_ok(VERIFY_WRITE, d, 8);
  705. if (copy_to_user(d, &tll, 8))
  706. FPU_abort;
  707. RE_ENTRANT_CHECK_ON;
  708. return 1;
  709. }
  710. /* Put a long into user memory */
  711. int FPU_store_int32(FPU_REG *st0_ptr, u_char st0_tag, long __user *d)
  712. {
  713. FPU_REG t;
  714. int precision_loss;
  715. if (st0_tag == TAG_Empty) {
  716. /* Empty register (stack underflow) */
  717. EXCEPTION(EX_StackUnder);
  718. goto invalid_operand;
  719. } else if (st0_tag == TAG_Special) {
  720. st0_tag = FPU_Special(st0_ptr);
  721. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  722. EXCEPTION(EX_Invalid);
  723. goto invalid_operand;
  724. }
  725. }
  726. reg_copy(st0_ptr, &t);
  727. precision_loss = FPU_round_to_int(&t, st0_tag);
  728. if (t.sigh ||
  729. ((t.sigl & 0x80000000) &&
  730. !((t.sigl == 0x80000000) && signnegative(&t)))) {
  731. EXCEPTION(EX_Invalid);
  732. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  733. invalid_operand:
  734. if (control_word & EX_Invalid) {
  735. /* Produce something like QNaN "indefinite" */
  736. t.sigl = 0x80000000;
  737. } else
  738. return 0;
  739. } else {
  740. if (precision_loss)
  741. set_precision_flag(precision_loss);
  742. if (signnegative(&t))
  743. t.sigl = -(long)t.sigl;
  744. }
  745. RE_ENTRANT_CHECK_OFF;
  746. FPU_access_ok(VERIFY_WRITE, d, 4);
  747. FPU_put_user(t.sigl, (unsigned long __user *)d);
  748. RE_ENTRANT_CHECK_ON;
  749. return 1;
  750. }
  751. /* Put a short into user memory */
  752. int FPU_store_int16(FPU_REG *st0_ptr, u_char st0_tag, short __user *d)
  753. {
  754. FPU_REG t;
  755. int precision_loss;
  756. if (st0_tag == TAG_Empty) {
  757. /* Empty register (stack underflow) */
  758. EXCEPTION(EX_StackUnder);
  759. goto invalid_operand;
  760. } else if (st0_tag == TAG_Special) {
  761. st0_tag = FPU_Special(st0_ptr);
  762. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  763. EXCEPTION(EX_Invalid);
  764. goto invalid_operand;
  765. }
  766. }
  767. reg_copy(st0_ptr, &t);
  768. precision_loss = FPU_round_to_int(&t, st0_tag);
  769. if (t.sigh ||
  770. ((t.sigl & 0xffff8000) &&
  771. !((t.sigl == 0x8000) && signnegative(&t)))) {
  772. EXCEPTION(EX_Invalid);
  773. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  774. invalid_operand:
  775. if (control_word & EX_Invalid) {
  776. /* Produce something like QNaN "indefinite" */
  777. t.sigl = 0x8000;
  778. } else
  779. return 0;
  780. } else {
  781. if (precision_loss)
  782. set_precision_flag(precision_loss);
  783. if (signnegative(&t))
  784. t.sigl = -t.sigl;
  785. }
  786. RE_ENTRANT_CHECK_OFF;
  787. FPU_access_ok(VERIFY_WRITE, d, 2);
  788. FPU_put_user((short)t.sigl, d);
  789. RE_ENTRANT_CHECK_ON;
  790. return 1;
  791. }
  792. /* Put a packed bcd array into user memory */
  793. int FPU_store_bcd(FPU_REG *st0_ptr, u_char st0_tag, u_char __user *d)
  794. {
  795. FPU_REG t;
  796. unsigned long long ll;
  797. u_char b;
  798. int i, precision_loss;
  799. u_char sign = (getsign(st0_ptr) == SIGN_NEG) ? 0x80 : 0;
  800. if (st0_tag == TAG_Empty) {
  801. /* Empty register (stack underflow) */
  802. EXCEPTION(EX_StackUnder);
  803. goto invalid_operand;
  804. } else if (st0_tag == TAG_Special) {
  805. st0_tag = FPU_Special(st0_ptr);
  806. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  807. EXCEPTION(EX_Invalid);
  808. goto invalid_operand;
  809. }
  810. }
  811. reg_copy(st0_ptr, &t);
  812. precision_loss = FPU_round_to_int(&t, st0_tag);
  813. ll = significand(&t);
  814. /* Check for overflow, by comparing with 999999999999999999 decimal. */
  815. if ((t.sigh > 0x0de0b6b3) ||
  816. ((t.sigh == 0x0de0b6b3) && (t.sigl > 0xa763ffff))) {
  817. EXCEPTION(EX_Invalid);
  818. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  819. invalid_operand:
  820. if (control_word & CW_Invalid) {
  821. /* Produce the QNaN "indefinite" */
  822. RE_ENTRANT_CHECK_OFF;
  823. FPU_access_ok(VERIFY_WRITE, d, 10);
  824. for (i = 0; i < 7; i++)
  825. FPU_put_user(0, d + i); /* These bytes "undefined" */
  826. FPU_put_user(0xc0, d + 7); /* This byte "undefined" */
  827. FPU_put_user(0xff, d + 8);
  828. FPU_put_user(0xff, d + 9);
  829. RE_ENTRANT_CHECK_ON;
  830. return 1;
  831. } else
  832. return 0;
  833. } else if (precision_loss) {
  834. /* Precision loss doesn't stop the data transfer */
  835. set_precision_flag(precision_loss);
  836. }
  837. RE_ENTRANT_CHECK_OFF;
  838. FPU_access_ok(VERIFY_WRITE, d, 10);
  839. RE_ENTRANT_CHECK_ON;
  840. for (i = 0; i < 9; i++) {
  841. b = FPU_div_small(&ll, 10);
  842. b |= (FPU_div_small(&ll, 10)) << 4;
  843. RE_ENTRANT_CHECK_OFF;
  844. FPU_put_user(b, d + i);
  845. RE_ENTRANT_CHECK_ON;
  846. }
  847. RE_ENTRANT_CHECK_OFF;
  848. FPU_put_user(sign, d + 9);
  849. RE_ENTRANT_CHECK_ON;
  850. return 1;
  851. }
  852. /*===========================================================================*/
  853. /* r gets mangled such that sig is int, sign:
  854. it is NOT normalized */
  855. /* The return value (in eax) is zero if the result is exact,
  856. if bits are changed due to rounding, truncation, etc, then
  857. a non-zero value is returned */
  858. /* Overflow is signalled by a non-zero return value (in eax).
  859. In the case of overflow, the returned significand always has the
  860. largest possible value */
  861. int FPU_round_to_int(FPU_REG *r, u_char tag)
  862. {
  863. u_char very_big;
  864. unsigned eax;
  865. if (tag == TAG_Zero) {
  866. /* Make sure that zero is returned */
  867. significand(r) = 0;
  868. return 0; /* o.k. */
  869. }
  870. if (exponent(r) > 63) {
  871. r->sigl = r->sigh = ~0; /* The largest representable number */
  872. return 1; /* overflow */
  873. }
  874. eax = FPU_shrxs(&r->sigl, 63 - exponent(r));
  875. very_big = !(~(r->sigh) | ~(r->sigl)); /* test for 0xfff...fff */
  876. #define half_or_more (eax & 0x80000000)
  877. #define frac_part (eax)
  878. #define more_than_half ((eax & 0x80000001) == 0x80000001)
  879. switch (control_word & CW_RC) {
  880. case RC_RND:
  881. if (more_than_half /* nearest */
  882. || (half_or_more && (r->sigl & 1))) { /* odd -> even */
  883. if (very_big)
  884. return 1; /* overflow */
  885. significand(r)++;
  886. return PRECISION_LOST_UP;
  887. }
  888. break;
  889. case RC_DOWN:
  890. if (frac_part && getsign(r)) {
  891. if (very_big)
  892. return 1; /* overflow */
  893. significand(r)++;
  894. return PRECISION_LOST_UP;
  895. }
  896. break;
  897. case RC_UP:
  898. if (frac_part && !getsign(r)) {
  899. if (very_big)
  900. return 1; /* overflow */
  901. significand(r)++;
  902. return PRECISION_LOST_UP;
  903. }
  904. break;
  905. case RC_CHOP:
  906. break;
  907. }
  908. return eax ? PRECISION_LOST_DOWN : 0;
  909. }
  910. /*===========================================================================*/
  911. u_char __user *fldenv(fpu_addr_modes addr_modes, u_char __user *s)
  912. {
  913. unsigned short tag_word = 0;
  914. u_char tag;
  915. int i;
  916. if ((addr_modes.default_mode == VM86) ||
  917. ((addr_modes.default_mode == PM16)
  918. ^ (addr_modes.override.operand_size == OP_SIZE_PREFIX))) {
  919. RE_ENTRANT_CHECK_OFF;
  920. FPU_access_ok(VERIFY_READ, s, 0x0e);
  921. FPU_get_user(control_word, (unsigned short __user *)s);
  922. FPU_get_user(partial_status, (unsigned short __user *)(s + 2));
  923. FPU_get_user(tag_word, (unsigned short __user *)(s + 4));
  924. FPU_get_user(instruction_address.offset,
  925. (unsigned short __user *)(s + 6));
  926. FPU_get_user(instruction_address.selector,
  927. (unsigned short __user *)(s + 8));
  928. FPU_get_user(operand_address.offset,
  929. (unsigned short __user *)(s + 0x0a));
  930. FPU_get_user(operand_address.selector,
  931. (unsigned short __user *)(s + 0x0c));
  932. RE_ENTRANT_CHECK_ON;
  933. s += 0x0e;
  934. if (addr_modes.default_mode == VM86) {
  935. instruction_address.offset
  936. += (instruction_address.selector & 0xf000) << 4;
  937. operand_address.offset +=
  938. (operand_address.selector & 0xf000) << 4;
  939. }
  940. } else {
  941. RE_ENTRANT_CHECK_OFF;
  942. FPU_access_ok(VERIFY_READ, s, 0x1c);
  943. FPU_get_user(control_word, (unsigned short __user *)s);
  944. FPU_get_user(partial_status, (unsigned short __user *)(s + 4));
  945. FPU_get_user(tag_word, (unsigned short __user *)(s + 8));
  946. FPU_get_user(instruction_address.offset,
  947. (unsigned long __user *)(s + 0x0c));
  948. FPU_get_user(instruction_address.selector,
  949. (unsigned short __user *)(s + 0x10));
  950. FPU_get_user(instruction_address.opcode,
  951. (unsigned short __user *)(s + 0x12));
  952. FPU_get_user(operand_address.offset,
  953. (unsigned long __user *)(s + 0x14));
  954. FPU_get_user(operand_address.selector,
  955. (unsigned long __user *)(s + 0x18));
  956. RE_ENTRANT_CHECK_ON;
  957. s += 0x1c;
  958. }
  959. #ifdef PECULIAR_486
  960. control_word &= ~0xe080;
  961. #endif /* PECULIAR_486 */
  962. top = (partial_status >> SW_Top_Shift) & 7;
  963. if (partial_status & ~control_word & CW_Exceptions)
  964. partial_status |= (SW_Summary | SW_Backward);
  965. else
  966. partial_status &= ~(SW_Summary | SW_Backward);
  967. for (i = 0; i < 8; i++) {
  968. tag = tag_word & 3;
  969. tag_word >>= 2;
  970. if (tag == TAG_Empty)
  971. /* New tag is empty. Accept it */
  972. FPU_settag(i, TAG_Empty);
  973. else if (FPU_gettag(i) == TAG_Empty) {
  974. /* Old tag is empty and new tag is not empty. New tag is determined
  975. by old reg contents */
  976. if (exponent(&fpu_register(i)) == -EXTENDED_Ebias) {
  977. if (!
  978. (fpu_register(i).sigl | fpu_register(i).
  979. sigh))
  980. FPU_settag(i, TAG_Zero);
  981. else
  982. FPU_settag(i, TAG_Special);
  983. } else if (exponent(&fpu_register(i)) ==
  984. 0x7fff - EXTENDED_Ebias) {
  985. FPU_settag(i, TAG_Special);
  986. } else if (fpu_register(i).sigh & 0x80000000)
  987. FPU_settag(i, TAG_Valid);
  988. else
  989. FPU_settag(i, TAG_Special); /* An Un-normal */
  990. }
  991. /* Else old tag is not empty and new tag is not empty. Old tag
  992. remains correct */
  993. }
  994. return s;
  995. }
  996. void frstor(fpu_addr_modes addr_modes, u_char __user *data_address)
  997. {
  998. int i, regnr;
  999. u_char __user *s = fldenv(addr_modes, data_address);
  1000. int offset = (top & 7) * 10, other = 80 - offset;
  1001. /* Copy all registers in stack order. */
  1002. RE_ENTRANT_CHECK_OFF;
  1003. FPU_access_ok(VERIFY_READ, s, 80);
  1004. __copy_from_user(register_base + offset, s, other);
  1005. if (offset)
  1006. __copy_from_user(register_base, s + other, offset);
  1007. RE_ENTRANT_CHECK_ON;
  1008. for (i = 0; i < 8; i++) {
  1009. regnr = (i + top) & 7;
  1010. if (FPU_gettag(regnr) != TAG_Empty)
  1011. /* The loaded data over-rides all other cases. */
  1012. FPU_settag(regnr, FPU_tagof(&st(i)));
  1013. }
  1014. }
  1015. u_char __user *fstenv(fpu_addr_modes addr_modes, u_char __user *d)
  1016. {
  1017. if ((addr_modes.default_mode == VM86) ||
  1018. ((addr_modes.default_mode == PM16)
  1019. ^ (addr_modes.override.operand_size == OP_SIZE_PREFIX))) {
  1020. RE_ENTRANT_CHECK_OFF;
  1021. FPU_access_ok(VERIFY_WRITE, d, 14);
  1022. #ifdef PECULIAR_486
  1023. FPU_put_user(control_word & ~0xe080, (unsigned long __user *)d);
  1024. #else
  1025. FPU_put_user(control_word, (unsigned short __user *)d);
  1026. #endif /* PECULIAR_486 */
  1027. FPU_put_user(status_word(), (unsigned short __user *)(d + 2));
  1028. FPU_put_user(fpu_tag_word, (unsigned short __user *)(d + 4));
  1029. FPU_put_user(instruction_address.offset,
  1030. (unsigned short __user *)(d + 6));
  1031. FPU_put_user(operand_address.offset,
  1032. (unsigned short __user *)(d + 0x0a));
  1033. if (addr_modes.default_mode == VM86) {
  1034. FPU_put_user((instruction_address.
  1035. offset & 0xf0000) >> 4,
  1036. (unsigned short __user *)(d + 8));
  1037. FPU_put_user((operand_address.offset & 0xf0000) >> 4,
  1038. (unsigned short __user *)(d + 0x0c));
  1039. } else {
  1040. FPU_put_user(instruction_address.selector,
  1041. (unsigned short __user *)(d + 8));
  1042. FPU_put_user(operand_address.selector,
  1043. (unsigned short __user *)(d + 0x0c));
  1044. }
  1045. RE_ENTRANT_CHECK_ON;
  1046. d += 0x0e;
  1047. } else {
  1048. RE_ENTRANT_CHECK_OFF;
  1049. FPU_access_ok(VERIFY_WRITE, d, 7 * 4);
  1050. #ifdef PECULIAR_486
  1051. control_word &= ~0xe080;
  1052. /* An 80486 sets nearly all of the reserved bits to 1. */
  1053. control_word |= 0xffff0040;
  1054. partial_status = status_word() | 0xffff0000;
  1055. fpu_tag_word |= 0xffff0000;
  1056. I387->soft.fcs &= ~0xf8000000;
  1057. I387->soft.fos |= 0xffff0000;
  1058. #endif /* PECULIAR_486 */
  1059. if (__copy_to_user(d, &control_word, 7 * 4))
  1060. FPU_abort;
  1061. RE_ENTRANT_CHECK_ON;
  1062. d += 0x1c;
  1063. }
  1064. control_word |= CW_Exceptions;
  1065. partial_status &= ~(SW_Summary | SW_Backward);
  1066. return d;
  1067. }
  1068. void fsave(fpu_addr_modes addr_modes, u_char __user *data_address)
  1069. {
  1070. u_char __user *d;
  1071. int offset = (top & 7) * 10, other = 80 - offset;
  1072. d = fstenv(addr_modes, data_address);
  1073. RE_ENTRANT_CHECK_OFF;
  1074. FPU_access_ok(VERIFY_WRITE, d, 80);
  1075. /* Copy all registers in stack order. */
  1076. if (__copy_to_user(d, register_base + offset, other))
  1077. FPU_abort;
  1078. if (offset)
  1079. if (__copy_to_user(d + other, register_base, offset))
  1080. FPU_abort;
  1081. RE_ENTRANT_CHECK_ON;
  1082. finit();
  1083. }
  1084. /*===========================================================================*/