Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. trap.c
  5. Abstract:
  6. WinDbg Extension Api
  7. Author:
  8. Ken Reneris
  9. Environment:
  10. User Mode.
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. typedef struct {
  16. ULONG Mask;
  17. PUCHAR String;
  18. } BITENCODING, *PBITENCODING;
  19. typedef unsigned __int64 ULONGLONG;
  20. void DumpNpxULongLong (PUCHAR s, ULONGLONG l);
  21. void DumpNpxExtended (PUCHAR str, PUCHAR Value);
  22. void DumpNpxBits (ULONG, PUCHAR, ULONG, PBITENCODING);
  23. PUCHAR NpxPrecisionControl[] = { "24Bits", "?1?", "53Bits", "64Bits" };
  24. PUCHAR NpxRoundingControl[] = { "Nearest", "Down", "Up", "Chop" };
  25. PUCHAR NpxTagWord[] = { " ", "ZR", "SP", " " };
  26. BITENCODING NpxStatusBits[] = {
  27. 1 << 8, "C0",
  28. 1 << 9, "C1",
  29. 1 << 10, "C2",
  30. 1 << 14, "C3",
  31. 0x8000, "Busy",
  32. 0x0001, "InvalidOp",
  33. 0x0002, "Denormal",
  34. 0x0004, "ZeroDivide",
  35. 0x0008, "Overflow",
  36. 0x0010, "Underflow",
  37. 0x0020, "Precision",
  38. 0x0040, "StackFault",
  39. 0x0080, "Summary",
  40. 0, 0
  41. };
  42. PUCHAR NpxOpD8[] = {
  43. "fadd", "fmul", "fcom", "fcomp", "fsub", "fsubr", "fdiv", "fdivr"
  44. };
  45. PUCHAR NpxOpD9[] = {
  46. "fld", "??3", "fst", "fstp", "fldenv", "fldcw", "fstenv", "fstcw"
  47. };
  48. PUCHAR NpxOpDA[] = {
  49. "fiadd", "fimul", "ficom", "ficomp", "fisub", "fisubr", "fidiv", "fidivr"
  50. };
  51. PUCHAR NpxOpDB[] = {
  52. "fild", "??4", "fist", "fistp", "??5", "fld", "??6", "fstp"
  53. };
  54. PUCHAR NpxOpDF[] = {
  55. "fild", "??4", "fist", "fistp", "fbld", "fild", "fbstp", "fstp"
  56. };
  57. PUCHAR *NpxSmOpTable[] = {
  58. NpxOpD8,
  59. NpxOpD9,
  60. NpxOpDA,
  61. NpxOpDB,
  62. NpxOpD8, // DC
  63. NpxOpD9, // DD
  64. NpxOpDA, // DE
  65. NpxOpDF
  66. };
  67. DECLARE_API( npx )
  68. /*++
  69. Routine Description:
  70. Dumps FNSAVE area format of NPX state
  71. Arguments:
  72. args -
  73. Return Value:
  74. None
  75. --*/
  76. {
  77. ULONG64 Address;
  78. UCHAR s[300], Reg[100];
  79. PUCHAR Stack, p;
  80. ULONG i, j, t, tos, Tag;
  81. ULONG ControlWord, StatusWord;
  82. // X86_ONLY_API
  83. if (TargetMachine != IMAGE_FILE_MACHINE_I386) {
  84. dprintf("!npx is X86 Only API\n");
  85. return E_INVALIDARG;
  86. }
  87. Address = GetExpression(args);
  88. if ( InitTypeRead(
  89. Address,
  90. FLOATING_SAVE_AREA)) {
  91. dprintf("unable to read floating save area\n");
  92. return E_INVALIDARG;
  93. }
  94. ControlWord = (ULONG) ReadField(ControlWord);
  95. dprintf ("EIP.......: %08x ControlWord: %s-%s mask: %02x Cr0NpxState: %08x\n",
  96. (ULONG) ReadField(ErrorOffset),
  97. NpxPrecisionControl [(ControlWord >> 8) & 0x3],
  98. NpxRoundingControl [(ControlWord >> 10) & 0x3],
  99. ControlWord & 0x3f,
  100. (ULONG) ReadField(Cr0NpxState)
  101. );
  102. DumpNpxBits ( StatusWord = (ULONG) ReadField(StatusWord), s, sizeof(s), NpxStatusBits);
  103. tos = (StatusWord >> 11) & 7,
  104. dprintf ("StatusWord: %04x TOS:%x %s (tagword: %04x)\n",
  105. StatusWord & 0xffff,
  106. tos,
  107. s,
  108. (ULONG) ReadField(TagWord) & 0xffff
  109. );
  110. GetFieldValue(Address, "FLOATING_SAVE_AREA", "RegisterArea", Reg);
  111. Stack = &Reg[0];
  112. Tag = (ULONG) ReadField(TagWord);
  113. for (i=0; i < 8; i++) {
  114. j = (tos + i) & 7;
  115. t = (Tag >> (j*2)) & 3;
  116. if (t != 3) {
  117. sprintf (s, "%x%c%s",
  118. j,
  119. j == tos ? '>' : '.',
  120. NpxTagWord [t]
  121. );
  122. DumpNpxExtended (s, Stack);
  123. }
  124. Stack += 10; // next stack location
  125. }
  126. dprintf ("\n");
  127. return S_OK;
  128. }
  129. void DumpNpxBits (
  130. ULONG Value,
  131. PUCHAR Str,
  132. ULONG StrSize,
  133. PBITENCODING Bits
  134. )
  135. {
  136. BOOLEAN Flag;
  137. Flag = FALSE;
  138. *Str = 0;
  139. while (Bits->Mask) {
  140. if (Bits->Mask & Value) {
  141. if (Flag) {
  142. if (SUCCEEDED( StringCchCopy(Str, StrSize, ", ") ) )
  143. {
  144. StrSize -=2; Str+=2;
  145. }
  146. if (StringCchCopy(Str, StrSize, Bits->String) == S_OK)
  147. {
  148. StrSize -= strlen (Str);
  149. Str += strlen (Str);
  150. }
  151. } else {
  152. if (StringCchCopy(Str, StrSize, Bits->String) == S_OK)
  153. {
  154. StrSize -= strlen (Str);
  155. Str += strlen (Str);
  156. Flag = TRUE;
  157. }
  158. }
  159. }
  160. Bits += 1;
  161. }
  162. }
  163. void
  164. DumpNpxULongLong (
  165. PUCHAR s,
  166. ULONGLONG l
  167. )
  168. {
  169. UCHAR c;
  170. UCHAR t[80], *p;
  171. if (l == 0) {
  172. *(s++)= '0';
  173. }
  174. p = t;
  175. while (l) {
  176. c = (UCHAR) ((ULONGLONG) l % 10);
  177. *(p++) = c + '0';
  178. l /= 10;
  179. }
  180. while (p != t) {
  181. *(s++) = *(--p);
  182. }
  183. *(s++) = 0;
  184. }
  185. void
  186. DumpNpxExtended (
  187. PUCHAR str,
  188. PUCHAR Value
  189. )
  190. {
  191. UCHAR *p, *o, c, out[100], t[100], ssig[100], ExponSign, SigSign;
  192. ULONG i, indent, mag, scale;
  193. LONG expon, delta;
  194. ULONGLONG sig;
  195. p = Value;
  196. c = 0;
  197. o = out+90;
  198. indent = strlen (str) + 1;
  199. dprintf ("%s ", str);
  200. //
  201. // Build string of bits
  202. //
  203. *(--o) = 0;
  204. while (c < 80) {
  205. *(--o) = (*p & 0x01) + '0';
  206. *p >>= 1;
  207. c += 1;
  208. if ((c % 8) == 0) {
  209. p += 1;
  210. }
  211. }
  212. p = o;
  213. //dprintf (" %s\n", o);
  214. //dprintf ("%*s", indent, "");
  215. //
  216. // print bit string seperated into fields
  217. //
  218. p = o;
  219. //dprintf ("%c %15.15s 1%c%s\n", p[0], p+1, '.', p+1+15);
  220. //dprintf ("%*s", indent, "");
  221. //
  222. // Pull out exponent
  223. //
  224. expon = 0;
  225. p = o + 1;
  226. for (i=0; i < 15; i++) {
  227. expon *= 2;
  228. if (p[i] == '1') {
  229. expon += 1;
  230. }
  231. }
  232. expon -= 16383; // take out exponent bias
  233. //
  234. // Build sig into big #
  235. //
  236. p = o + 1+15;
  237. scale = 0;
  238. for (i=0; p[i]; i++) {
  239. if (p[i] == '1') {
  240. scale = i+1;
  241. }
  242. }
  243. SigSign = p[i-1] == '0' ? '+' : '-';
  244. sig = 0;
  245. for (i=0; i < scale; i++) {
  246. sig <<= 1;
  247. if (p[i] == '1') {
  248. sig += 1;
  249. }
  250. }
  251. delta = expon - (scale - 1);
  252. //dprintf ("delta %d, expon %d, scale %d\n", delta, expon, scale);
  253. //
  254. // Print values of each field
  255. //
  256. DumpNpxULongLong (ssig, sig);
  257. p = o;
  258. ExponSign = p[0] == '0' ? '+' : '-';
  259. dprintf ("%c %15.15s (%+5d) %c%c%s\n",
  260. ExponSign,
  261. p + 1,
  262. expon,
  263. p[1+15], '.', p+1+15+1
  264. );
  265. dprintf ("%*s", indent, "");
  266. if (expon == -16383) {
  267. if (SigSign == '+') {
  268. dprintf ("Denormal\n\n");
  269. } else {
  270. dprintf ("Pseudodenormal\n\n");
  271. }
  272. return ;
  273. }
  274. if (expon == 1024) {
  275. if (scale == 1) {
  276. dprintf ("%c Infinity\n", ExponSign);
  277. } else {
  278. p = o + 1+15;
  279. c = 0;
  280. for (i=0; p[i]; i++) {
  281. if (p[i] == '1') {
  282. c++;
  283. }
  284. }
  285. if (SigSign == '+') {
  286. dprintf ("Signaling NaN\n");
  287. } else {
  288. if (c == 1) {
  289. dprintf ("Indefinite - quite NaN\n");
  290. } else {
  291. dprintf ("Quite NaN\n");
  292. }
  293. }
  294. }
  295. dprintf ("%*s", indent, "");
  296. }
  297. //dprintf ("%*s%c %15d %s (delta %d)\n",
  298. // indent, "", // indent
  299. // p[0] == '0' ? '+' : '-', // sign of exponent
  300. // expon, ssig,
  301. // delta
  302. // );
  303. //dprintf ("%*s", indent, "");
  304. t[0] = 0;
  305. p = t;
  306. if (delta < 0) {
  307. p += sprintf (p, "/ ");
  308. delta = -delta;
  309. } else if (delta > 0) {
  310. p += sprintf (p, "* ");
  311. }
  312. if (delta) {
  313. if (delta < 31) {
  314. p += sprintf (p, "%d", 1 << delta);
  315. } else {
  316. p += sprintf (p, "2^%d", delta);
  317. }
  318. }
  319. dprintf ("%s %s\n",
  320. ssig,
  321. t
  322. );
  323. }