fpu.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * SIMD Floating Point.
  11. * Assembler support to get at the individual instructions is in l64fpu.s.
  12. */
  13. #include "u.h"
  14. #include "../port/lib.h"
  15. #include "mem.h"
  16. #include "dat.h"
  17. #include "fns.h"
  18. #include "amd64.h"
  19. #include "ureg.h"
  20. enum { /* FCW, FSW and MXCSR */
  21. I = 0x00000001, /* Invalid-Operation */
  22. D = 0x00000002, /* Denormalized-Operand */
  23. Z = 0x00000004, /* Zero-Divide */
  24. O = 0x00000008, /* Overflow */
  25. U = 0x00000010, /* Underflow */
  26. P = 0x00000020, /* Precision */
  27. };
  28. enum { /* FCW */
  29. PCs = 0x00000000, /* Precision Control -Single */
  30. PCd = 0x00000200, /* -Double */
  31. PCde = 0x00000300, /* -Double Extended */
  32. RCn = 0x00000000, /* Rounding Control -Nearest */
  33. RCd = 0x00000400, /* -Down */
  34. RCu = 0x00000800, /* -Up */
  35. RCz = 0x00000C00, /* -Toward Zero */
  36. };
  37. enum { /* FSW */
  38. Sff = 0x00000040, /* Stack Fault Flag */
  39. Es = 0x00000080, /* Error Summary Status */
  40. C0 = 0x00000100, /* ZF - Condition Code Bits */
  41. C1 = 0x00000200, /* O/U# */
  42. C2 = 0x00000400, /* PF */
  43. C3 = 0x00004000, /* ZF */
  44. B = 0x00008000, /* Busy */
  45. };
  46. enum { /* MXCSR */
  47. Daz = 0x00000040, /* Denormals are Zeros */
  48. Im = 0x00000080, /* I Mask */
  49. Dm = 0x00000100, /* D Mask */
  50. Zm = 0x00000200, /* Z Mask */
  51. Om = 0x00000400, /* O Mask */
  52. Um = 0x00000800, /* U Mask */
  53. Pm = 0x00001000, /* P Mask */
  54. Rn = 0x00000000, /* Round to Nearest */
  55. Rd = 0x00002000, /* Round Down */
  56. Ru = 0x00004000, /* Round Up */
  57. Rz = 0x00006000, /* Round toward Zero */
  58. Fz = 0x00008000, /* Flush to Zero for Um */
  59. };
  60. static Fxsave defaultFxsave;
  61. extern void _fxrstor(Fxsave*);
  62. extern void _fxsave(Fxsave*);
  63. static void
  64. fpusave(Fxsave *fxsave)
  65. {
  66. _fxsave(fxsave);
  67. }
  68. static void
  69. fpurestore(Fxsave *fxsave)
  70. {
  71. _fxrstor(fxsave);
  72. }
  73. int
  74. fpudevprocio(Proc* proc, void* a, int32_t n, uintptr_t offset, int write)
  75. {
  76. /*
  77. * Called from procdevtab.read and procdevtab.write
  78. * allow user process access to the FPU registers.
  79. * This is the only FPU routine which is called directly
  80. * from the port code; it would be nice to have dynamic
  81. * creation of entries in the device file trees...
  82. */
  83. if (offset >= sizeof(Fxsave))
  84. return 0;
  85. uint8_t *p = (uint8_t*)&proc->FPU.fxsave;
  86. switch (write) {
  87. default:
  88. if(offset+n > sizeof(Fxsave))
  89. n = sizeof(Fxsave) - offset;
  90. memmove(p+offset, a, n);
  91. break;
  92. case 0:
  93. if(offset+n > sizeof(Fxsave))
  94. n = sizeof(Fxsave) - offset;
  95. memmove(a, p+offset, n);
  96. break;
  97. }
  98. return n;
  99. }
  100. void
  101. fpunotify(Ureg* u)
  102. {
  103. /*
  104. * Called when a note is about to be delivered to a
  105. * user process, usually at the end of a system call.
  106. */
  107. Proc *up = externup();
  108. fpusave(&up->FPU.fxsave);
  109. }
  110. void
  111. fpunoted(void)
  112. {
  113. // Called from sysnoted() via the machine-dependent noted() routine.
  114. Proc *up = externup();
  115. fpurestore(&up->FPU.fxsave);
  116. }
  117. void
  118. fpusysrfork(Ureg* u)
  119. {
  120. /*
  121. * Called early in the non-interruptible path of
  122. * sysrfork() via the machine-dependent syscall() routine.
  123. * Save the state so that it can be easily copied
  124. * to the child process later.
  125. */
  126. Proc *up = externup();
  127. fpusave(&up->FPU.fxsave);
  128. }
  129. void
  130. fpusysrforkchild(Proc* child, Proc* parent)
  131. {
  132. /*
  133. * Called later in sysrfork() via the machine-dependent
  134. * sysrforkchild() routine.
  135. * Copy the parent FPU state to the child.
  136. */
  137. memmove(&child->FPU.fxsave, &parent->FPU.fxsave, sizeof(Fxsave));
  138. }
  139. void
  140. fpuprocsave(Proc* p)
  141. {
  142. /*
  143. * Called from sched() and sleep() via the machine-dependent
  144. * procsave() routine.
  145. * About to go in to the scheduler.
  146. */
  147. // The process is dead so don't save anything
  148. if (p->state == Moribund) {
  149. return;
  150. }
  151. // Save the FPU state without handling pending unmasked exceptions.
  152. // Postnote() can't be called here as sleep() already has up->rlock,.
  153. fpusave(&p->FPU.fxsave);
  154. }
  155. void
  156. fpuprocrestore(Proc* p)
  157. {
  158. // The process has been rescheduled and is about to run.
  159. fpurestore(&p->FPU.fxsave);
  160. }
  161. void
  162. fpusysprocsetup(Proc* p)
  163. {
  164. fpurestore(&defaultFxsave);
  165. }
  166. void
  167. acfpusysprocsetup(Proc *p)
  168. {
  169. fpusysprocsetup(p);
  170. }
  171. static char*
  172. xfpuxm(Ureg* ureg, void* v)
  173. {
  174. Proc *up = externup();
  175. uint32_t mxcsr;
  176. char *cm;
  177. // #XM - SIMD Floating Point Exception (Vector 19).
  178. // Save FPU state to check out the error.
  179. fpusave(&up->FPU.fxsave);
  180. if(ureg->ip & KZERO)
  181. panic("#XM: ip=%#p", ureg->ip);
  182. // Notify the user process.
  183. mxcsr = up->FPU.fxsave.mxcsr;
  184. if ((mxcsr & (Im|I)) == I)
  185. cm = "Invalid Operation";
  186. else if ((mxcsr & (Dm|D)) == D)
  187. cm = "Denormal Operand";
  188. else if ((mxcsr & (Zm|Z)) == Z)
  189. cm = "Divide-By-Zero";
  190. else if ((mxcsr & (Om|O)) == O)
  191. cm = "Numeric Overflow";
  192. else if ((mxcsr & (Um|U)) == U)
  193. cm = "Numeric Underflow";
  194. else if ((mxcsr & (Pm|P)) == P)
  195. cm = "Precision";
  196. else
  197. cm = "Unknown";
  198. snprint(up->genbuf, sizeof(up->genbuf), "sys: fp: %s Exception mxcsr=%#x", cm, mxcsr);
  199. return up->genbuf;
  200. }
  201. static void
  202. fpuxm(Ureg *ureg, void *p)
  203. {
  204. Proc *up = externup();
  205. char *n = xfpuxm(ureg, p);
  206. if (n != nil)
  207. postnote(up, 1, n, NDebug);
  208. }
  209. static char*
  210. acfpuxm(Ureg *ureg, void *p)
  211. {
  212. return xfpuxm(ureg, p);
  213. }
  214. void
  215. fpuinit(void)
  216. {
  217. // It's assumed there is an integrated FPU, so Em is cleared;
  218. uint64_t cr0 = cr0get();
  219. cr0 &= ~Em;
  220. cr0 |= Ne|Mp;
  221. cr0put(cr0);
  222. uint64_t cr4 = cr4get();
  223. cr4 |= Osxmmexcpt|Osfxsr;
  224. cr4put(cr4);
  225. memset(&defaultFxsave, 0, sizeof(defaultFxsave));
  226. fpusave(&defaultFxsave);
  227. defaultFxsave.mxcsrmask = 0x0000FFBF;
  228. defaultFxsave.mxcsr &= Rn|Pm|Um|Dm;
  229. if (machp()->machno != 0)
  230. return;
  231. // Set up the exception handlers.
  232. trapenable(IdtXM, fpuxm, 0, "#XM");
  233. actrapenable(IdtXM, acfpuxm, 0, "#XM");
  234. }