go32-nat.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152
  1. /* Native debugging support for Intel x86 running DJGPP.
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. Written by Robert Hoehne.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* To whomever it may concern, here's a general description of how
  16. debugging in DJGPP works, and the special quirks GDB does to
  17. support that.
  18. When the DJGPP port of GDB is debugging a DJGPP program natively,
  19. there aren't 2 separate processes, the debuggee and GDB itself, as
  20. on other systems. (This is DOS, where there can only be one active
  21. process at any given time, remember?) Instead, GDB and the
  22. debuggee live in the same process. So when GDB calls
  23. go32_create_inferior below, and that function calls edi_init from
  24. the DJGPP debug support library libdbg.a, we load the debuggee's
  25. executable file into GDB's address space, set it up for execution
  26. as the stub loader (a short real-mode program prepended to each
  27. DJGPP executable) normally would, and do a lot of preparations for
  28. swapping between GDB's and debuggee's internal state, primarily wrt
  29. the exception handlers. This swapping happens every time we resume
  30. the debuggee or switch back to GDB's code, and it includes:
  31. . swapping all the segment registers
  32. . swapping the PSP (the Program Segment Prefix)
  33. . swapping the signal handlers
  34. . swapping the exception handlers
  35. . swapping the FPU status
  36. . swapping the 3 standard file handles (more about this below)
  37. Then running the debuggee simply means longjmp into it where its PC
  38. is and let it run until it stops for some reason. When it stops,
  39. GDB catches the exception that stopped it and longjmp's back into
  40. its own code. All the possible exit points of the debuggee are
  41. watched; for example, the normal exit point is recognized because a
  42. DOS program issues a special system call to exit. If one of those
  43. exit points is hit, we mourn the inferior and clean up after it.
  44. Cleaning up is very important, even if the process exits normally,
  45. because otherwise we might leave behind traces of previous
  46. execution, and in several cases GDB itself might be left hosed,
  47. because all the exception handlers were not restored.
  48. Swapping of the standard handles (in redir_to_child and
  49. redir_to_debugger) is needed because, since both GDB and the
  50. debuggee live in the same process, as far as the OS is concerned,
  51. the share the same file table. This means that the standard
  52. handles 0, 1, and 2 point to the same file table entries, and thus
  53. are connected to the same devices. Therefore, if the debugger
  54. redirects its standard output, the standard output of the debuggee
  55. is also automagically redirected to the same file/device!
  56. Similarly, if the debuggee redirects its stdout to a file, you
  57. won't be able to see debugger's output (it will go to the same file
  58. where the debuggee has its output); and if the debuggee closes its
  59. standard input, you will lose the ability to talk to debugger!
  60. For this reason, every time the debuggee is about to be resumed, we
  61. call redir_to_child, which redirects the standard handles to where
  62. the debuggee expects them to be. When the debuggee stops and GDB
  63. regains control, we call redir_to_debugger, which redirects those 3
  64. handles back to where GDB expects.
  65. Note that only the first 3 handles are swapped, so if the debuggee
  66. redirects or closes any other handles, GDB will not notice. In
  67. particular, the exit code of a DJGPP program forcibly closes all
  68. file handles beyond the first 3 ones, so when the debuggee exits,
  69. GDB currently loses its stdaux and stdprn streams. Fortunately,
  70. GDB does not use those as of this writing, and will never need
  71. to. */
  72. #include "defs.h"
  73. #include <fcntl.h>
  74. #include "x86-nat.h"
  75. #include "inferior.h"
  76. #include "infrun.h"
  77. #include "gdbthread.h"
  78. #include "gdbsupport/gdb_wait.h"
  79. #include "gdbcore.h"
  80. #include "command.h"
  81. #include "gdbcmd.h"
  82. #include "floatformat.h"
  83. #include "buildsym-legacy.h"
  84. #include "i387-tdep.h"
  85. #include "i386-tdep.h"
  86. #include "nat/x86-cpuid.h"
  87. #include "value.h"
  88. #include "regcache.h"
  89. #include "top.h"
  90. #include "cli/cli-utils.h"
  91. #include "inf-child.h"
  92. #include <ctype.h>
  93. #include <unistd.h>
  94. #include <sys/utsname.h>
  95. #include <io.h>
  96. #include <dos.h>
  97. #include <dpmi.h>
  98. #include <go32.h>
  99. #include <sys/farptr.h>
  100. #include <debug/v2load.h>
  101. #include <debug/dbgcom.h>
  102. #if __DJGPP_MINOR__ > 2
  103. #include <debug/redir.h>
  104. #endif
  105. #include <langinfo.h>
  106. #if __DJGPP_MINOR__ < 3
  107. /* This code will be provided from DJGPP 2.03 on. Until then I code it
  108. here. */
  109. typedef struct
  110. {
  111. unsigned short sig0;
  112. unsigned short sig1;
  113. unsigned short sig2;
  114. unsigned short sig3;
  115. unsigned short exponent:15;
  116. unsigned short sign:1;
  117. }
  118. NPXREG;
  119. typedef struct
  120. {
  121. unsigned int control;
  122. unsigned int status;
  123. unsigned int tag;
  124. unsigned int eip;
  125. unsigned int cs;
  126. unsigned int dataptr;
  127. unsigned int datasel;
  128. NPXREG reg[8];
  129. }
  130. NPX;
  131. static NPX npx;
  132. static void save_npx (void); /* Save the FPU of the debugged program. */
  133. static void load_npx (void); /* Restore the FPU of the debugged program. */
  134. /* ------------------------------------------------------------------------- */
  135. /* Store the contents of the NPX in the global variable `npx'. */
  136. /* *INDENT-OFF* */
  137. static void
  138. save_npx (void)
  139. {
  140. asm ("inb $0xa0, %%al \n\
  141. testb $0x20, %%al \n\
  142. jz 1f \n\
  143. xorb %%al, %%al \n\
  144. outb %%al, $0xf0 \n\
  145. movb $0x20, %%al \n\
  146. outb %%al, $0xa0 \n\
  147. outb %%al, $0x20 \n\
  148. 1: \n\
  149. fnsave %0 \n\
  150. fwait "
  151. : "=m" (npx)
  152. : /* No input */
  153. : "%eax");
  154. }
  155. /* *INDENT-ON* */
  156. /* ------------------------------------------------------------------------- */
  157. /* Reload the contents of the NPX from the global variable `npx'. */
  158. static void
  159. load_npx (void)
  160. {
  161. asm ("frstor %0":"=m" (npx));
  162. }
  163. /* ------------------------------------------------------------------------- */
  164. /* Stubs for the missing redirection functions. */
  165. typedef struct {
  166. char *command;
  167. int redirected;
  168. } cmdline_t;
  169. void
  170. redir_cmdline_delete (cmdline_t *ptr)
  171. {
  172. ptr->redirected = 0;
  173. }
  174. int
  175. redir_cmdline_parse (const char *args, cmdline_t *ptr)
  176. {
  177. return -1;
  178. }
  179. int
  180. redir_to_child (cmdline_t *ptr)
  181. {
  182. return 1;
  183. }
  184. int
  185. redir_to_debugger (cmdline_t *ptr)
  186. {
  187. return 1;
  188. }
  189. int
  190. redir_debug_init (cmdline_t *ptr)
  191. {
  192. return 0;
  193. }
  194. #endif /* __DJGPP_MINOR < 3 */
  195. typedef enum { wp_insert, wp_remove, wp_count } wp_op;
  196. /* This holds the current reference counts for each debug register. */
  197. static int dr_ref_count[4];
  198. #define SOME_PID 42
  199. static int prog_has_started = 0;
  200. #define r_ofs(x) (offsetof(TSS,x))
  201. static struct
  202. {
  203. size_t tss_ofs;
  204. size_t size;
  205. }
  206. regno_mapping[] =
  207. {
  208. {r_ofs (tss_eax), 4}, /* normal registers, from a_tss */
  209. {r_ofs (tss_ecx), 4},
  210. {r_ofs (tss_edx), 4},
  211. {r_ofs (tss_ebx), 4},
  212. {r_ofs (tss_esp), 4},
  213. {r_ofs (tss_ebp), 4},
  214. {r_ofs (tss_esi), 4},
  215. {r_ofs (tss_edi), 4},
  216. {r_ofs (tss_eip), 4},
  217. {r_ofs (tss_eflags), 4},
  218. {r_ofs (tss_cs), 2},
  219. {r_ofs (tss_ss), 2},
  220. {r_ofs (tss_ds), 2},
  221. {r_ofs (tss_es), 2},
  222. {r_ofs (tss_fs), 2},
  223. {r_ofs (tss_gs), 2},
  224. {0, 10}, /* 8 FP registers, from npx.reg[] */
  225. {1, 10},
  226. {2, 10},
  227. {3, 10},
  228. {4, 10},
  229. {5, 10},
  230. {6, 10},
  231. {7, 10},
  232. /* The order of the next 7 registers must be consistent
  233. with their numbering in config/i386/tm-i386.h, which see. */
  234. {0, 2}, /* control word, from npx */
  235. {4, 2}, /* status word, from npx */
  236. {8, 2}, /* tag word, from npx */
  237. {16, 2}, /* last FP exception CS from npx */
  238. {12, 4}, /* last FP exception EIP from npx */
  239. {24, 2}, /* last FP exception operand selector from npx */
  240. {20, 4}, /* last FP exception operand offset from npx */
  241. {18, 2} /* last FP opcode from npx */
  242. };
  243. static struct
  244. {
  245. int go32_sig;
  246. enum gdb_signal gdb_sig;
  247. }
  248. sig_map[] =
  249. {
  250. {0, GDB_SIGNAL_FPE},
  251. {1, GDB_SIGNAL_TRAP},
  252. /* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
  253. but I think SIGBUS is better, since the NMI is usually activated
  254. as a result of a memory parity check failure. */
  255. {2, GDB_SIGNAL_BUS},
  256. {3, GDB_SIGNAL_TRAP},
  257. {4, GDB_SIGNAL_FPE},
  258. {5, GDB_SIGNAL_SEGV},
  259. {6, GDB_SIGNAL_ILL},
  260. {7, GDB_SIGNAL_EMT}, /* no-coprocessor exception */
  261. {8, GDB_SIGNAL_SEGV},
  262. {9, GDB_SIGNAL_SEGV},
  263. {10, GDB_SIGNAL_BUS},
  264. {11, GDB_SIGNAL_SEGV},
  265. {12, GDB_SIGNAL_SEGV},
  266. {13, GDB_SIGNAL_SEGV},
  267. {14, GDB_SIGNAL_SEGV},
  268. {16, GDB_SIGNAL_FPE},
  269. {17, GDB_SIGNAL_BUS},
  270. {31, GDB_SIGNAL_ILL},
  271. {0x1b, GDB_SIGNAL_INT},
  272. {0x75, GDB_SIGNAL_FPE},
  273. {0x78, GDB_SIGNAL_ALRM},
  274. {0x79, GDB_SIGNAL_INT},
  275. {0x7a, GDB_SIGNAL_QUIT},
  276. {-1, GDB_SIGNAL_LAST}
  277. };
  278. static struct {
  279. enum gdb_signal gdb_sig;
  280. int djgpp_excepno;
  281. } excepn_map[] = {
  282. {GDB_SIGNAL_0, -1},
  283. {GDB_SIGNAL_ILL, 6}, /* Invalid Opcode */
  284. {GDB_SIGNAL_EMT, 7}, /* triggers SIGNOFP */
  285. {GDB_SIGNAL_SEGV, 13}, /* GPF */
  286. {GDB_SIGNAL_BUS, 17}, /* Alignment Check */
  287. /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
  288. details. */
  289. {GDB_SIGNAL_TERM, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
  290. {GDB_SIGNAL_FPE, 0x75},
  291. {GDB_SIGNAL_INT, 0x79},
  292. {GDB_SIGNAL_QUIT, 0x7a},
  293. {GDB_SIGNAL_ALRM, 0x78}, /* triggers SIGTIMR */
  294. {GDB_SIGNAL_PROF, 0x78},
  295. {GDB_SIGNAL_LAST, -1}
  296. };
  297. /* The go32 target. */
  298. struct go32_nat_target final : public x86_nat_target<inf_child_target>
  299. {
  300. void attach (const char *, int) override;
  301. void resume (ptid_t, int, enum gdb_signal) override;
  302. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  303. void fetch_registers (struct regcache *, int) override;
  304. void store_registers (struct regcache *, int) override;
  305. enum target_xfer_status xfer_partial (enum target_object object,
  306. const char *annex,
  307. gdb_byte *readbuf,
  308. const gdb_byte *writebuf,
  309. ULONGEST offset, ULONGEST len,
  310. ULONGEST *xfered_len) override;
  311. void files_info () override;
  312. void terminal_init () override;
  313. void terminal_inferior () override;
  314. void terminal_ours_for_output () override;
  315. void terminal_ours () override;
  316. void terminal_info (const char *, int) override;
  317. void pass_ctrlc () override;
  318. void kill () override;
  319. void create_inferior (const char *, const std::string &,
  320. char **, int) override;
  321. void mourn_inferior () override;
  322. bool thread_alive (ptid_t ptid) override;
  323. std::string pid_to_str (ptid_t) override;
  324. };
  325. static go32_nat_target the_go32_nat_target;
  326. void
  327. go32_nat_target::attach (const char *args, int from_tty)
  328. {
  329. error (_("\
  330. You cannot attach to a running program on this platform.\n\
  331. Use the `run' command to run DJGPP programs."));
  332. }
  333. static int resume_is_step;
  334. static int resume_signal = -1;
  335. void
  336. go32_nat_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
  337. {
  338. int i;
  339. resume_is_step = step;
  340. if (siggnal != GDB_SIGNAL_0 && siggnal != GDB_SIGNAL_TRAP)
  341. {
  342. for (i = 0, resume_signal = -1;
  343. excepn_map[i].gdb_sig != GDB_SIGNAL_LAST; i++)
  344. if (excepn_map[i].gdb_sig == siggnal)
  345. {
  346. resume_signal = excepn_map[i].djgpp_excepno;
  347. break;
  348. }
  349. if (resume_signal == -1)
  350. printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
  351. gdb_signal_to_name (siggnal));
  352. }
  353. }
  354. static char child_cwd[FILENAME_MAX];
  355. ptid_t
  356. go32_nat_target::wait (ptid_t ptid, struct target_waitstatus *status,
  357. target_wait_flags options)
  358. {
  359. int i;
  360. unsigned char saved_opcode;
  361. unsigned long INT3_addr = 0;
  362. int stepping_over_INT = 0;
  363. a_tss.tss_eflags &= 0xfeff; /* Reset the single-step flag (TF). */
  364. if (resume_is_step)
  365. {
  366. /* If the next instruction is INT xx or INTO, we need to handle
  367. them specially. Intel manuals say that these instructions
  368. reset the single-step flag (a.k.a. TF). However, it seems
  369. that, at least in the DPMI environment, and at least when
  370. stepping over the DPMI interrupt 31h, the problem is having
  371. TF set at all when INT 31h is executed: the debuggee either
  372. crashes (and takes the system with it) or is killed by a
  373. SIGTRAP.
  374. So we need to emulate single-step mode: we put an INT3 opcode
  375. right after the INT xx instruction, let the debuggee run
  376. until it hits INT3 and stops, then restore the original
  377. instruction which we overwrote with the INT3 opcode, and back
  378. up the debuggee's EIP to that instruction. */
  379. read_child (a_tss.tss_eip, &saved_opcode, 1);
  380. if (saved_opcode == 0xCD || saved_opcode == 0xCE)
  381. {
  382. unsigned char INT3_opcode = 0xCC;
  383. INT3_addr
  384. = saved_opcode == 0xCD ? a_tss.tss_eip + 2 : a_tss.tss_eip + 1;
  385. stepping_over_INT = 1;
  386. read_child (INT3_addr, &saved_opcode, 1);
  387. write_child (INT3_addr, &INT3_opcode, 1);
  388. }
  389. else
  390. a_tss.tss_eflags |= 0x0100; /* normal instruction: set TF */
  391. }
  392. /* The special value FFFFh in tss_trap indicates to run_child that
  393. tss_irqn holds a signal to be delivered to the debuggee. */
  394. if (resume_signal <= -1)
  395. {
  396. a_tss.tss_trap = 0;
  397. a_tss.tss_irqn = 0xff;
  398. }
  399. else
  400. {
  401. a_tss.tss_trap = 0xffff; /* run_child looks for this. */
  402. a_tss.tss_irqn = resume_signal;
  403. }
  404. /* The child might change working directory behind our back. The
  405. GDB users won't like the side effects of that when they work with
  406. relative file names, and GDB might be confused by its current
  407. directory not being in sync with the truth. So we always make a
  408. point of changing back to where GDB thinks is its cwd, when we
  409. return control to the debugger, but restore child's cwd before we
  410. run it. */
  411. /* Initialize child_cwd, before the first call to run_child and not
  412. in the initialization, so the child get also the changed directory
  413. set with the gdb-command "cd ..." */
  414. if (!*child_cwd)
  415. /* Initialize child's cwd with the current one. */
  416. getcwd (child_cwd, sizeof (child_cwd));
  417. chdir (child_cwd);
  418. #if __DJGPP_MINOR__ < 3
  419. load_npx ();
  420. #endif
  421. run_child ();
  422. #if __DJGPP_MINOR__ < 3
  423. save_npx ();
  424. #endif
  425. /* Did we step over an INT xx instruction? */
  426. if (stepping_over_INT && a_tss.tss_eip == INT3_addr + 1)
  427. {
  428. /* Restore the original opcode. */
  429. a_tss.tss_eip--; /* EIP points *after* the INT3 instruction. */
  430. write_child (a_tss.tss_eip, &saved_opcode, 1);
  431. /* Simulate a TRAP exception. */
  432. a_tss.tss_irqn = 1;
  433. a_tss.tss_eflags |= 0x0100;
  434. }
  435. getcwd (child_cwd, sizeof (child_cwd)); /* in case it has changed */
  436. if (current_directory != NULL)
  437. chdir (current_directory);
  438. if (a_tss.tss_irqn == 0x21)
  439. status->set_exited (a_tss.tss_eax & 0xff);
  440. else
  441. {
  442. status->set_stopped (GDB_SIGNAL_UNKNOWN);
  443. for (i = 0; sig_map[i].go32_sig != -1; i++)
  444. {
  445. if (a_tss.tss_irqn == sig_map[i].go32_sig)
  446. {
  447. #if __DJGPP_MINOR__ < 3
  448. status->set_stopped (sig_map[i].gdb_sig);
  449. if (status->sig () != GDB_SIGNAL_TRAP)
  450. status->set_signalled (status->sig ());
  451. #else
  452. status->set_stopped (sig_map[i].gdb_sig);
  453. #endif
  454. break;
  455. }
  456. }
  457. }
  458. return ptid_t (SOME_PID);
  459. }
  460. static void
  461. fetch_register (struct regcache *regcache, int regno)
  462. {
  463. struct gdbarch *gdbarch = regcache->arch ();
  464. if (regno < gdbarch_fp0_regnum (gdbarch))
  465. regcache->raw_supply (regno,
  466. (char *) &a_tss + regno_mapping[regno].tss_ofs);
  467. else if (i386_fp_regnum_p (gdbarch, regno) || i386_fpc_regnum_p (gdbarch,
  468. regno))
  469. i387_supply_fsave (regcache, regno, &npx);
  470. else
  471. internal_error (__FILE__, __LINE__,
  472. _("Invalid register no. %d in fetch_register."), regno);
  473. }
  474. void
  475. go32_nat_target::fetch_registers (struct regcache *regcache, int regno)
  476. {
  477. if (regno >= 0)
  478. fetch_register (regcache, regno);
  479. else
  480. {
  481. for (regno = 0;
  482. regno < gdbarch_fp0_regnum (regcache->arch ());
  483. regno++)
  484. fetch_register (regcache, regno);
  485. i387_supply_fsave (regcache, -1, &npx);
  486. }
  487. }
  488. static void
  489. store_register (const struct regcache *regcache, int regno)
  490. {
  491. struct gdbarch *gdbarch = regcache->arch ();
  492. if (regno < gdbarch_fp0_regnum (gdbarch))
  493. regcache->raw_collect (regno,
  494. (char *) &a_tss + regno_mapping[regno].tss_ofs);
  495. else if (i386_fp_regnum_p (gdbarch, regno) || i386_fpc_regnum_p (gdbarch,
  496. regno))
  497. i387_collect_fsave (regcache, regno, &npx);
  498. else
  499. internal_error (__FILE__, __LINE__,
  500. _("Invalid register no. %d in store_register."), regno);
  501. }
  502. void
  503. go32_nat_target::store_registers (struct regcache *regcache, int regno)
  504. {
  505. unsigned r;
  506. if (regno >= 0)
  507. store_register (regcache, regno);
  508. else
  509. {
  510. for (r = 0; r < gdbarch_fp0_regnum (regcache->arch ()); r++)
  511. store_register (regcache, r);
  512. i387_collect_fsave (regcache, -1, &npx);
  513. }
  514. }
  515. /* Const-correct version of DJGPP's write_child, which unfortunately
  516. takes a non-const buffer pointer. */
  517. static int
  518. my_write_child (unsigned child_addr, const void *buf, unsigned len)
  519. {
  520. static void *buffer = NULL;
  521. static unsigned buffer_len = 0;
  522. int res;
  523. if (buffer_len < len)
  524. {
  525. buffer = xrealloc (buffer, len);
  526. buffer_len = len;
  527. }
  528. memcpy (buffer, buf, len);
  529. res = write_child (child_addr, buffer, len);
  530. return res;
  531. }
  532. /* Helper for go32_xfer_partial that handles memory transfers.
  533. Arguments are like target_xfer_partial. */
  534. static enum target_xfer_status
  535. go32_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
  536. ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
  537. {
  538. int res;
  539. if (writebuf != NULL)
  540. res = my_write_child (memaddr, writebuf, len);
  541. else
  542. res = read_child (memaddr, readbuf, len);
  543. /* read_child and write_child return zero on success, non-zero on
  544. failure. */
  545. if (res != 0)
  546. return TARGET_XFER_E_IO;
  547. *xfered_len = len;
  548. return TARGET_XFER_OK;
  549. }
  550. /* Target to_xfer_partial implementation. */
  551. enum target_xfer_status
  552. go32_nat_target::xfer_partial (enum target_object object,
  553. const char *annex, gdb_byte *readbuf,
  554. const gdb_byte *writebuf, ULONGEST offset,
  555. ULONGEST len,
  556. ULONGEST *xfered_len)
  557. {
  558. switch (object)
  559. {
  560. case TARGET_OBJECT_MEMORY:
  561. return go32_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
  562. default:
  563. return this->beneath ()->xfer_partial (object, annex,
  564. readbuf, writebuf, offset, len,
  565. xfered_len);
  566. }
  567. }
  568. static cmdline_t child_cmd; /* Parsed child's command line kept here. */
  569. void
  570. go32_nat_target::files_info ()
  571. {
  572. gdb_printf ("You are running a DJGPP V2 program.\n");
  573. }
  574. void
  575. go32_nat_target::kill_inferior ()
  576. {
  577. mourn_inferior ();
  578. }
  579. void
  580. go32_nat_target::create_inferior (const char *exec_file,
  581. const std::string &allargs,
  582. char **env, int from_tty)
  583. {
  584. extern char **environ;
  585. jmp_buf start_state;
  586. char *cmdline;
  587. char **env_save = environ;
  588. size_t cmdlen;
  589. struct inferior *inf;
  590. int result;
  591. const char *args = allargs.c_str ();
  592. /* If no exec file handed to us, get it from the exec-file command -- with
  593. a good, common error message if none is specified. */
  594. if (exec_file == 0)
  595. exec_file = get_exec_file (1);
  596. resume_signal = -1;
  597. resume_is_step = 0;
  598. /* Initialize child's cwd as empty to be initialized when starting
  599. the child. */
  600. *child_cwd = 0;
  601. /* Init command line storage. */
  602. if (redir_debug_init (&child_cmd) == -1)
  603. internal_error (__FILE__, __LINE__,
  604. _("Cannot allocate redirection storage: "
  605. "not enough memory.\n"));
  606. /* Parse the command line and create redirections. */
  607. if (strpbrk (args, "<>"))
  608. {
  609. if (redir_cmdline_parse (args, &child_cmd) == 0)
  610. args = child_cmd.command;
  611. else
  612. error (_("Syntax error in command line."));
  613. }
  614. else
  615. child_cmd.command = xstrdup (args);
  616. cmdlen = strlen (args);
  617. /* v2loadimage passes command lines via DOS memory, so it cannot
  618. possibly handle commands longer than 1MB. */
  619. if (cmdlen > 1024*1024)
  620. error (_("Command line too long."));
  621. cmdline = (char *) xmalloc (cmdlen + 4);
  622. strcpy (cmdline + 1, args);
  623. /* If the command-line length fits into DOS 126-char limits, use the
  624. DOS command tail format; otherwise, tell v2loadimage to pass it
  625. through a buffer in conventional memory. */
  626. if (cmdlen < 127)
  627. {
  628. cmdline[0] = strlen (args);
  629. cmdline[cmdlen + 1] = 13;
  630. }
  631. else
  632. cmdline[0] = 0xff; /* Signal v2loadimage it's a long command. */
  633. environ = env;
  634. result = v2loadimage (exec_file, cmdline, start_state);
  635. environ = env_save;
  636. xfree (cmdline);
  637. if (result != 0)
  638. error (_("Load failed for image %s"), exec_file);
  639. edi_init (start_state);
  640. #if __DJGPP_MINOR__ < 3
  641. save_npx ();
  642. #endif
  643. inf = current_inferior ();
  644. inferior_appeared (inf, SOME_PID);
  645. if (!inf->target_is_pushed (this))
  646. inf->push_target (this);
  647. thread_info *thr = add_thread_silent (ptid_t (SOME_PID));
  648. switch_to_thread (thr);
  649. clear_proceed_status (0);
  650. insert_breakpoints ();
  651. prog_has_started = 1;
  652. }
  653. void
  654. go32_nat_target::mourn_inferior ()
  655. {
  656. redir_cmdline_delete (&child_cmd);
  657. resume_signal = -1;
  658. resume_is_step = 0;
  659. cleanup_client ();
  660. /* We need to make sure all the breakpoint enable bits in the DR7
  661. register are reset when the inferior exits. Otherwise, if they
  662. rerun the inferior, the uncleared bits may cause random SIGTRAPs,
  663. failure to set more watchpoints, and other calamities. It would
  664. be nice if GDB itself would take care to remove all breakpoints
  665. at all times, but it doesn't, probably under an assumption that
  666. the OS cleans up when the debuggee exits. */
  667. x86_cleanup_dregs ();
  668. prog_has_started = 0;
  669. generic_mourn_inferior ();
  670. maybe_unpush_target ();
  671. }
  672. /* Hardware watchpoint support. */
  673. #define D_REGS edi.dr
  674. #define CONTROL D_REGS[7]
  675. #define STATUS D_REGS[6]
  676. /* Pass the address ADDR to the inferior in the I'th debug register.
  677. Here we just store the address in D_REGS, the watchpoint will be
  678. actually set up when go32_wait runs the debuggee. */
  679. static void
  680. go32_set_dr (int i, CORE_ADDR addr)
  681. {
  682. if (i < 0 || i > 3)
  683. internal_error (__FILE__, __LINE__,
  684. _("Invalid register %d in go32_set_dr.\n"), i);
  685. D_REGS[i] = addr;
  686. }
  687. /* Pass the value VAL to the inferior in the DR7 debug control
  688. register. Here we just store the address in D_REGS, the watchpoint
  689. will be actually set up when go32_wait runs the debuggee. */
  690. static void
  691. go32_set_dr7 (unsigned long val)
  692. {
  693. CONTROL = val;
  694. }
  695. /* Get the value of the DR6 debug status register from the inferior.
  696. Here we just return the value stored in D_REGS, as we've got it
  697. from the last go32_wait call. */
  698. static unsigned long
  699. go32_get_dr6 (void)
  700. {
  701. return STATUS;
  702. }
  703. /* Get the value of the DR7 debug status register from the inferior.
  704. Here we just return the value stored in D_REGS, as we've got it
  705. from the last go32_wait call. */
  706. static unsigned long
  707. go32_get_dr7 (void)
  708. {
  709. return CONTROL;
  710. }
  711. /* Get the value of the DR debug register I from the inferior. Here
  712. we just return the value stored in D_REGS, as we've got it from the
  713. last go32_wait call. */
  714. static CORE_ADDR
  715. go32_get_dr (int i)
  716. {
  717. if (i < 0 || i > 3)
  718. internal_error (__FILE__, __LINE__,
  719. _("Invalid register %d in go32_get_dr.\n"), i);
  720. return D_REGS[i];
  721. }
  722. /* Put the device open on handle FD into either raw or cooked
  723. mode, return 1 if it was in raw mode, zero otherwise. */
  724. static int
  725. device_mode (int fd, int raw_p)
  726. {
  727. int oldmode, newmode;
  728. __dpmi_regs regs;
  729. regs.x.ax = 0x4400;
  730. regs.x.bx = fd;
  731. __dpmi_int (0x21, &regs);
  732. if (regs.x.flags & 1)
  733. return -1;
  734. newmode = oldmode = regs.x.dx;
  735. if (raw_p)
  736. newmode |= 0x20;
  737. else
  738. newmode &= ~0x20;
  739. if (oldmode & 0x80) /* Only for character dev. */
  740. {
  741. regs.x.ax = 0x4401;
  742. regs.x.bx = fd;
  743. regs.x.dx = newmode & 0xff; /* Force upper byte zero, else it fails. */
  744. __dpmi_int (0x21, &regs);
  745. if (regs.x.flags & 1)
  746. return -1;
  747. }
  748. return (oldmode & 0x20) == 0x20;
  749. }
  750. static int inf_mode_valid = 0;
  751. static int inf_terminal_mode;
  752. /* This semaphore is needed because, amazingly enough, GDB calls
  753. target.to_terminal_ours more than once after the inferior stops.
  754. But we need the information from the first call only, since the
  755. second call will always see GDB's own cooked terminal. */
  756. static int terminal_is_ours = 1;
  757. void
  758. go32_nat_target::terminal_init ()
  759. {
  760. inf_mode_valid = 0; /* Reinitialize, in case they are restarting child. */
  761. terminal_is_ours = 1;
  762. }
  763. void
  764. go32_nat_target::terminal_info (const char *args, int from_tty)
  765. {
  766. gdb_printf ("Inferior's terminal is in %s mode.\n",
  767. !inf_mode_valid
  768. ? "default" : inf_terminal_mode ? "raw" : "cooked");
  769. #if __DJGPP_MINOR__ > 2
  770. if (child_cmd.redirection)
  771. {
  772. int i;
  773. for (i = 0; i < DBG_HANDLES; i++)
  774. {
  775. if (child_cmd.redirection[i]->file_name)
  776. gdb_printf ("\tFile handle %d is redirected to `%s'.\n",
  777. i, child_cmd.redirection[i]->file_name);
  778. else if (_get_dev_info (child_cmd.redirection[i]->inf_handle) == -1)
  779. gdb_printf
  780. ("\tFile handle %d appears to be closed by inferior.\n", i);
  781. /* Mask off the raw/cooked bit when comparing device info words. */
  782. else if ((_get_dev_info (child_cmd.redirection[i]->inf_handle) & 0xdf)
  783. != (_get_dev_info (i) & 0xdf))
  784. gdb_printf
  785. ("\tFile handle %d appears to be redirected by inferior.\n", i);
  786. }
  787. }
  788. #endif
  789. }
  790. void
  791. go32_nat_target::terminal_inferior ()
  792. {
  793. /* Redirect standard handles as child wants them. */
  794. errno = 0;
  795. if (redir_to_child (&child_cmd) == -1)
  796. {
  797. redir_to_debugger (&child_cmd);
  798. error (_("Cannot redirect standard handles for program: %s."),
  799. safe_strerror (errno));
  800. }
  801. /* Set the console device of the inferior to whatever mode
  802. (raw or cooked) we found it last time. */
  803. if (terminal_is_ours)
  804. {
  805. if (inf_mode_valid)
  806. device_mode (0, inf_terminal_mode);
  807. terminal_is_ours = 0;
  808. }
  809. }
  810. void
  811. go32_nat_target::terminal_ours ()
  812. {
  813. /* Switch to cooked mode on the gdb terminal and save the inferior
  814. terminal mode to be restored when it is resumed. */
  815. if (!terminal_is_ours)
  816. {
  817. inf_terminal_mode = device_mode (0, 0);
  818. if (inf_terminal_mode != -1)
  819. inf_mode_valid = 1;
  820. else
  821. /* If device_mode returned -1, we don't know what happens with
  822. handle 0 anymore, so make the info invalid. */
  823. inf_mode_valid = 0;
  824. terminal_is_ours = 1;
  825. /* Restore debugger's standard handles. */
  826. errno = 0;
  827. if (redir_to_debugger (&child_cmd) == -1)
  828. {
  829. redir_to_child (&child_cmd);
  830. error (_("Cannot redirect standard handles for debugger: %s."),
  831. safe_strerror (errno));
  832. }
  833. }
  834. }
  835. void
  836. go32_nat_target::pass_ctrlc ()
  837. {
  838. }
  839. bool
  840. go32_nat_target::thread_alive (ptid_t ptid)
  841. {
  842. return ptid != null_ptid;
  843. }
  844. std::string
  845. go32_nat_target::pid_to_str (ptid_t ptid)
  846. {
  847. return normal_pid_to_str (ptid);
  848. }
  849. /* Return the current DOS codepage number. */
  850. static int
  851. dos_codepage (void)
  852. {
  853. __dpmi_regs regs;
  854. regs.x.ax = 0x6601;
  855. __dpmi_int (0x21, &regs);
  856. if (!(regs.x.flags & 1))
  857. return regs.x.bx & 0xffff;
  858. else
  859. return 437; /* default */
  860. }
  861. /* Limited emulation of `nl_langinfo', for charset.c. */
  862. char *
  863. nl_langinfo (nl_item item)
  864. {
  865. char *retval;
  866. switch (item)
  867. {
  868. case CODESET:
  869. {
  870. /* 8 is enough for SHORT_MAX + "CP" + null. */
  871. char buf[8];
  872. int blen = sizeof (buf);
  873. int needed = snprintf (buf, blen, "CP%d", dos_codepage ());
  874. if (needed > blen) /* Should never happen. */
  875. buf[0] = 0;
  876. retval = xstrdup (buf);
  877. }
  878. break;
  879. default:
  880. retval = xstrdup ("");
  881. break;
  882. }
  883. return retval;
  884. }
  885. unsigned short windows_major, windows_minor;
  886. /* Compute the version Windows reports via Int 2Fh/AX=1600h. */
  887. static void
  888. go32_get_windows_version(void)
  889. {
  890. __dpmi_regs r;
  891. r.x.ax = 0x1600;
  892. __dpmi_int(0x2f, &r);
  893. if (r.h.al > 2 && r.h.al != 0x80 && r.h.al != 0xff
  894. && (r.h.al > 3 || r.h.ah > 0))
  895. {
  896. windows_major = r.h.al;
  897. windows_minor = r.h.ah;
  898. }
  899. else
  900. windows_major = 0xff; /* meaning no Windows */
  901. }
  902. /* A subroutine of go32_sysinfo to display memory info. */
  903. static void
  904. print_mem (unsigned long datum, const char *header, int in_pages_p)
  905. {
  906. if (datum != 0xffffffffUL)
  907. {
  908. if (in_pages_p)
  909. datum <<= 12;
  910. gdb_puts (header);
  911. if (datum > 1024)
  912. {
  913. gdb_printf ("%lu KB", datum >> 10);
  914. if (datum > 1024 * 1024)
  915. gdb_printf (" (%lu MB)", datum >> 20);
  916. }
  917. else
  918. gdb_printf ("%lu Bytes", datum);
  919. gdb_puts ("\n");
  920. }
  921. }
  922. /* Display assorted information about the underlying OS. */
  923. static void
  924. go32_sysinfo (const char *arg, int from_tty)
  925. {
  926. static const char test_pattern[] =
  927. "deadbeafdeadbeafdeadbeafdeadbeafdeadbeaf"
  928. "deadbeafdeadbeafdeadbeafdeadbeafdeadbeaf"
  929. "deadbeafdeadbeafdeadbeafdeadbeafdeadbeafdeadbeaf";
  930. struct utsname u;
  931. char cpuid_vendor[13];
  932. unsigned cpuid_max = 0, cpuid_eax, cpuid_ebx, cpuid_ecx, cpuid_edx;
  933. unsigned true_dos_version = _get_dos_version (1);
  934. unsigned advertized_dos_version = ((unsigned int)_osmajor << 8) | _osminor;
  935. int dpmi_flags;
  936. char dpmi_vendor_info[129];
  937. int dpmi_vendor_available;
  938. __dpmi_version_ret dpmi_version_data;
  939. long eflags;
  940. __dpmi_free_mem_info mem_info;
  941. __dpmi_regs regs;
  942. cpuid_vendor[0] = '\0';
  943. if (uname (&u))
  944. strcpy (u.machine, "Unknown x86");
  945. else if (u.machine[0] == 'i' && u.machine[1] > 4)
  946. {
  947. /* CPUID with EAX = 0 returns the Vendor ID. */
  948. #if 0
  949. /* Ideally we would use x86_cpuid(), but it needs someone to run
  950. native tests first to make sure things actually work. They should.
  951. http://sourceware.org/ml/gdb-patches/2013-05/msg00164.html */
  952. unsigned int eax, ebx, ecx, edx;
  953. if (x86_cpuid (0, &eax, &ebx, &ecx, &edx))
  954. {
  955. cpuid_max = eax;
  956. memcpy (&vendor[0], &ebx, 4);
  957. memcpy (&vendor[4], &ecx, 4);
  958. memcpy (&vendor[8], &edx, 4);
  959. cpuid_vendor[12] = '\0';
  960. }
  961. #else
  962. __asm__ __volatile__ ("xorl %%ebx, %%ebx;"
  963. "xorl %%ecx, %%ecx;"
  964. "xorl %%edx, %%edx;"
  965. "movl $0, %%eax;"
  966. "cpuid;"
  967. "movl %%ebx, %0;"
  968. "movl %%edx, %1;"
  969. "movl %%ecx, %2;"
  970. "movl %%eax, %3;"
  971. : "=m" (cpuid_vendor[0]),
  972. "=m" (cpuid_vendor[4]),
  973. "=m" (cpuid_vendor[8]),
  974. "=m" (cpuid_max)
  975. :
  976. : "%eax", "%ebx", "%ecx", "%edx");
  977. cpuid_vendor[12] = '\0';
  978. #endif
  979. }
  980. gdb_printf ("CPU Type.......................%s", u.machine);
  981. if (cpuid_vendor[0])
  982. gdb_printf (" (%s)", cpuid_vendor);
  983. gdb_puts ("\n");
  984. /* CPUID with EAX = 1 returns processor signature and features. */
  985. if (cpuid_max >= 1)
  986. {
  987. static const char *brand_name[] = {
  988. "",
  989. " Celeron",
  990. " III",
  991. " III Xeon",
  992. "", "", "", "",
  993. " 4"
  994. };
  995. char cpu_string[80];
  996. char cpu_brand[20];
  997. unsigned brand_idx;
  998. int intel_p = strcmp (cpuid_vendor, "GenuineIntel") == 0;
  999. int amd_p = strcmp (cpuid_vendor, "AuthenticAMD") == 0;
  1000. int hygon_p = strcmp (cpuid_vendor, "HygonGenuine") == 0;
  1001. unsigned cpu_family, cpu_model;
  1002. #if 0
  1003. /* See comment above about cpuid usage. */
  1004. x86_cpuid (1, &cpuid_eax, &cpuid_ebx, NULL, &cpuid_edx);
  1005. #else
  1006. __asm__ __volatile__ ("movl $1, %%eax;"
  1007. "cpuid;"
  1008. : "=a" (cpuid_eax),
  1009. "=b" (cpuid_ebx),
  1010. "=d" (cpuid_edx)
  1011. :
  1012. : "%ecx");
  1013. #endif
  1014. brand_idx = cpuid_ebx & 0xff;
  1015. cpu_family = (cpuid_eax >> 8) & 0xf;
  1016. cpu_model = (cpuid_eax >> 4) & 0xf;
  1017. cpu_brand[0] = '\0';
  1018. if (intel_p)
  1019. {
  1020. if (brand_idx > 0
  1021. && brand_idx < sizeof(brand_name)/sizeof(brand_name[0])
  1022. && *brand_name[brand_idx])
  1023. strcpy (cpu_brand, brand_name[brand_idx]);
  1024. else if (cpu_family == 5)
  1025. {
  1026. if (((cpuid_eax >> 12) & 3) == 0 && cpu_model == 4)
  1027. strcpy (cpu_brand, " MMX");
  1028. else if (cpu_model > 1 && ((cpuid_eax >> 12) & 3) == 1)
  1029. strcpy (cpu_brand, " OverDrive");
  1030. else if (cpu_model > 1 && ((cpuid_eax >> 12) & 3) == 2)
  1031. strcpy (cpu_brand, " Dual");
  1032. }
  1033. else if (cpu_family == 6 && cpu_model < 8)
  1034. {
  1035. switch (cpu_model)
  1036. {
  1037. case 1:
  1038. strcpy (cpu_brand, " Pro");
  1039. break;
  1040. case 3:
  1041. strcpy (cpu_brand, " II");
  1042. break;
  1043. case 5:
  1044. strcpy (cpu_brand, " II Xeon");
  1045. break;
  1046. case 6:
  1047. strcpy (cpu_brand, " Celeron");
  1048. break;
  1049. case 7:
  1050. strcpy (cpu_brand, " III");
  1051. break;
  1052. }
  1053. }
  1054. }
  1055. else if (amd_p)
  1056. {
  1057. switch (cpu_family)
  1058. {
  1059. case 4:
  1060. strcpy (cpu_brand, "486/5x86");
  1061. break;
  1062. case 5:
  1063. switch (cpu_model)
  1064. {
  1065. case 0:
  1066. case 1:
  1067. case 2:
  1068. case 3:
  1069. strcpy (cpu_brand, "-K5");
  1070. break;
  1071. case 6:
  1072. case 7:
  1073. strcpy (cpu_brand, "-K6");
  1074. break;
  1075. case 8:
  1076. strcpy (cpu_brand, "-K6-2");
  1077. break;
  1078. case 9:
  1079. strcpy (cpu_brand, "-K6-III");
  1080. break;
  1081. }
  1082. break;
  1083. case 6:
  1084. switch (cpu_model)
  1085. {
  1086. case 1:
  1087. case 2:
  1088. case 4:
  1089. strcpy (cpu_brand, " Athlon");
  1090. break;
  1091. case 3:
  1092. strcpy (cpu_brand, " Duron");
  1093. break;
  1094. }
  1095. break;
  1096. }
  1097. }
  1098. xsnprintf (cpu_string, sizeof (cpu_string), "%s%s Model %d Stepping %d",
  1099. intel_p ? "Pentium" : (amd_p ? "AMD" : (hygon_p ? "Hygon" : "ix86")),
  1100. cpu_brand, cpu_model, cpuid_eax & 0xf);
  1101. gdb_printf ("%*s%s\n", 31, "", cpu_string);
  1102. if (((cpuid_edx & (6 | (0x0d << 23))) != 0)
  1103. || ((cpuid_edx & 1) == 0)
  1104. || ((amd_p || hygon_p) && (cpuid_edx & (3 << 30)) != 0))
  1105. {
  1106. gdb_puts ("CPU Features...................");
  1107. /* We only list features which might be useful in the DPMI
  1108. environment. */
  1109. if ((cpuid_edx & 1) == 0)
  1110. gdb_puts ("No FPU "); /* It's unusual to not have an FPU. */
  1111. if ((cpuid_edx & (1 << 1)) != 0)
  1112. gdb_puts ("VME ");
  1113. if ((cpuid_edx & (1 << 2)) != 0)
  1114. gdb_puts ("DE ");
  1115. if ((cpuid_edx & (1 << 4)) != 0)
  1116. gdb_puts ("TSC ");
  1117. if ((cpuid_edx & (1 << 23)) != 0)
  1118. gdb_puts ("MMX ");
  1119. if ((cpuid_edx & (1 << 25)) != 0)
  1120. gdb_puts ("SSE ");
  1121. if ((cpuid_edx & (1 << 26)) != 0)
  1122. gdb_puts ("SSE2 ");
  1123. if (amd_p || hygon_p)
  1124. {
  1125. if ((cpuid_edx & (1 << 31)) != 0)
  1126. gdb_puts ("3DNow! ");
  1127. if ((cpuid_edx & (1 << 30)) != 0)
  1128. gdb_puts ("3DNow!Ext");
  1129. }
  1130. gdb_puts ("\n");
  1131. }
  1132. }
  1133. gdb_puts ("\n");
  1134. gdb_printf ("DOS Version....................%s %s.%s",
  1135. _os_flavor, u.release, u.version);
  1136. if (true_dos_version != advertized_dos_version)
  1137. gdb_printf (" (disguised as v%d.%d)", _osmajor, _osminor);
  1138. gdb_puts ("\n");
  1139. if (!windows_major)
  1140. go32_get_windows_version ();
  1141. if (windows_major != 0xff)
  1142. {
  1143. const char *windows_flavor;
  1144. gdb_printf ("Windows Version................%d.%02d (Windows ",
  1145. windows_major, windows_minor);
  1146. switch (windows_major)
  1147. {
  1148. case 3:
  1149. windows_flavor = "3.X";
  1150. break;
  1151. case 4:
  1152. switch (windows_minor)
  1153. {
  1154. case 0:
  1155. windows_flavor = "95, 95A, or 95B";
  1156. break;
  1157. case 3:
  1158. windows_flavor = "95B OSR2.1 or 95C OSR2.5";
  1159. break;
  1160. case 10:
  1161. windows_flavor = "98 or 98 SE";
  1162. break;
  1163. case 90:
  1164. windows_flavor = "ME";
  1165. break;
  1166. default:
  1167. windows_flavor = "9X";
  1168. break;
  1169. }
  1170. break;
  1171. default:
  1172. windows_flavor = "??";
  1173. break;
  1174. }
  1175. gdb_printf ("%s)\n", windows_flavor);
  1176. }
  1177. else if (true_dos_version == 0x532 && advertized_dos_version == 0x500)
  1178. gdb_printf ("Windows Version................"
  1179. "Windows NT family (W2K/XP/W2K3/Vista/W2K8)\n");
  1180. gdb_puts ("\n");
  1181. /* On some versions of Windows, __dpmi_get_capabilities returns
  1182. zero, but the buffer is not filled with info, so we fill the
  1183. buffer with a known pattern and test for it afterwards. */
  1184. memcpy (dpmi_vendor_info, test_pattern, sizeof(dpmi_vendor_info));
  1185. dpmi_vendor_available =
  1186. __dpmi_get_capabilities (&dpmi_flags, dpmi_vendor_info);
  1187. if (dpmi_vendor_available == 0
  1188. && memcmp (dpmi_vendor_info, test_pattern,
  1189. sizeof(dpmi_vendor_info)) != 0)
  1190. {
  1191. /* The DPMI spec says the vendor string should be ASCIIZ, but
  1192. I don't trust the vendors to follow that... */
  1193. if (!memchr (&dpmi_vendor_info[2], 0, 126))
  1194. dpmi_vendor_info[128] = '\0';
  1195. gdb_printf ("DPMI Host......................"
  1196. "%s v%d.%d (capabilities: %#x)\n",
  1197. &dpmi_vendor_info[2],
  1198. (unsigned)dpmi_vendor_info[0],
  1199. (unsigned)dpmi_vendor_info[1],
  1200. ((unsigned)dpmi_flags & 0x7f));
  1201. }
  1202. else
  1203. gdb_printf ("DPMI Host......................(Info not available)\n");
  1204. __dpmi_get_version (&dpmi_version_data);
  1205. gdb_printf ("DPMI Version...................%d.%02d\n",
  1206. dpmi_version_data.major, dpmi_version_data.minor);
  1207. gdb_printf ("DPMI Info......................"
  1208. "%s-bit DPMI, with%s Virtual Memory support\n",
  1209. (dpmi_version_data.flags & 1) ? "32" : "16",
  1210. (dpmi_version_data.flags & 4) ? "" : "out");
  1211. gdb_printf ("%*sInterrupts reflected to %s mode\n", 31, "",
  1212. (dpmi_version_data.flags & 2) ? "V86" : "Real");
  1213. gdb_printf ("%*sProcessor type: i%d86\n", 31, "",
  1214. dpmi_version_data.cpu);
  1215. gdb_printf ("%*sPIC base interrupt: Master: %#x Slave: %#x\n", 31, "",
  1216. dpmi_version_data.master_pic, dpmi_version_data.slave_pic);
  1217. /* a_tss is only initialized when the debuggee is first run. */
  1218. if (prog_has_started)
  1219. {
  1220. __asm__ __volatile__ ("pushfl ; popl %0" : "=g" (eflags));
  1221. gdb_printf ("Protection....................."
  1222. "Ring %d (in %s), with%s I/O protection\n",
  1223. a_tss.tss_cs & 3, (a_tss.tss_cs & 4) ? "LDT" : "GDT",
  1224. (a_tss.tss_cs & 3) > ((eflags >> 12) & 3) ? "" : "out");
  1225. }
  1226. gdb_puts ("\n");
  1227. __dpmi_get_free_memory_information (&mem_info);
  1228. print_mem (mem_info.total_number_of_physical_pages,
  1229. "DPMI Total Physical Memory.....", 1);
  1230. print_mem (mem_info.total_number_of_free_pages,
  1231. "DPMI Free Physical Memory......", 1);
  1232. print_mem (mem_info.size_of_paging_file_partition_in_pages,
  1233. "DPMI Swap Space................", 1);
  1234. print_mem (mem_info.linear_address_space_size_in_pages,
  1235. "DPMI Total Linear Address Size.", 1);
  1236. print_mem (mem_info.free_linear_address_space_in_pages,
  1237. "DPMI Free Linear Address Size..", 1);
  1238. print_mem (mem_info.largest_available_free_block_in_bytes,
  1239. "DPMI Largest Free Memory Block.", 0);
  1240. regs.h.ah = 0x48;
  1241. regs.x.bx = 0xffff;
  1242. __dpmi_int (0x21, &regs);
  1243. print_mem (regs.x.bx << 4, "Free DOS Memory................", 0);
  1244. regs.x.ax = 0x5800;
  1245. __dpmi_int (0x21, &regs);
  1246. if ((regs.x.flags & 1) == 0)
  1247. {
  1248. static const char *dos_hilo[] = {
  1249. "Low", "", "", "", "High", "", "", "", "High, then Low"
  1250. };
  1251. static const char *dos_fit[] = {
  1252. "First", "Best", "Last"
  1253. };
  1254. int hilo_idx = (regs.x.ax >> 4) & 0x0f;
  1255. int fit_idx = regs.x.ax & 0x0f;
  1256. if (hilo_idx > 8)
  1257. hilo_idx = 0;
  1258. if (fit_idx > 2)
  1259. fit_idx = 0;
  1260. gdb_printf ("DOS Memory Allocation..........%s memory, %s fit\n",
  1261. dos_hilo[hilo_idx], dos_fit[fit_idx]);
  1262. regs.x.ax = 0x5802;
  1263. __dpmi_int (0x21, &regs);
  1264. if ((regs.x.flags & 1) != 0)
  1265. regs.h.al = 0;
  1266. gdb_printf ("%*sUMBs %sin DOS memory chain\n", 31, "",
  1267. regs.h.al == 0 ? "not " : "");
  1268. }
  1269. }
  1270. struct seg_descr {
  1271. unsigned short limit0;
  1272. unsigned short base0;
  1273. unsigned char base1;
  1274. unsigned stype:5;
  1275. unsigned dpl:2;
  1276. unsigned present:1;
  1277. unsigned limit1:4;
  1278. unsigned available:1;
  1279. unsigned dummy:1;
  1280. unsigned bit32:1;
  1281. unsigned page_granular:1;
  1282. unsigned char base2;
  1283. } __attribute__ ((packed));
  1284. struct gate_descr {
  1285. unsigned short offset0;
  1286. unsigned short selector;
  1287. unsigned param_count:5;
  1288. unsigned dummy:3;
  1289. unsigned stype:5;
  1290. unsigned dpl:2;
  1291. unsigned present:1;
  1292. unsigned short offset1;
  1293. } __attribute__ ((packed));
  1294. /* Read LEN bytes starting at logical address ADDR, and put the result
  1295. into DEST. Return 1 if success, zero if not. */
  1296. static int
  1297. read_memory_region (unsigned long addr, void *dest, size_t len)
  1298. {
  1299. unsigned long dos_ds_limit = __dpmi_get_segment_limit (_dos_ds);
  1300. int retval = 1;
  1301. /* For the low memory, we can simply use _dos_ds. */
  1302. if (addr <= dos_ds_limit - len)
  1303. dosmemget (addr, len, dest);
  1304. else
  1305. {
  1306. /* For memory above 1MB we need to set up a special segment to
  1307. be able to access that memory. */
  1308. int sel = __dpmi_allocate_ldt_descriptors (1);
  1309. if (sel <= 0)
  1310. retval = 0;
  1311. else
  1312. {
  1313. int access_rights = __dpmi_get_descriptor_access_rights (sel);
  1314. size_t segment_limit = len - 1;
  1315. /* Make sure the crucial bits in the descriptor access
  1316. rights are set correctly. Some DPMI providers might barf
  1317. if we set the segment limit to something that is not an
  1318. integral multiple of 4KB pages if the granularity bit is
  1319. not set to byte-granular, even though the DPMI spec says
  1320. it's the host's responsibility to set that bit correctly. */
  1321. if (len > 1024 * 1024)
  1322. {
  1323. access_rights |= 0x8000;
  1324. /* Page-granular segments should have the low 12 bits of
  1325. the limit set. */
  1326. segment_limit |= 0xfff;
  1327. }
  1328. else
  1329. access_rights &= ~0x8000;
  1330. if (__dpmi_set_segment_base_address (sel, addr) != -1
  1331. && __dpmi_set_descriptor_access_rights (sel, access_rights) != -1
  1332. && __dpmi_set_segment_limit (sel, segment_limit) != -1
  1333. /* W2K silently fails to set the segment limit, leaving
  1334. it at zero; this test avoids the resulting crash. */
  1335. && __dpmi_get_segment_limit (sel) >= segment_limit)
  1336. movedata (sel, 0, _my_ds (), (unsigned)dest, len);
  1337. else
  1338. retval = 0;
  1339. __dpmi_free_ldt_descriptor (sel);
  1340. }
  1341. }
  1342. return retval;
  1343. }
  1344. /* Get a segment descriptor stored at index IDX in the descriptor
  1345. table whose base address is TABLE_BASE. Return the descriptor
  1346. type, or -1 if failure. */
  1347. static int
  1348. get_descriptor (unsigned long table_base, int idx, void *descr)
  1349. {
  1350. unsigned long addr = table_base + idx * 8; /* 8 bytes per entry */
  1351. if (read_memory_region (addr, descr, 8))
  1352. return (int)((struct seg_descr *)descr)->stype;
  1353. return -1;
  1354. }
  1355. struct dtr_reg {
  1356. unsigned short limit __attribute__((packed));
  1357. unsigned long base __attribute__((packed));
  1358. };
  1359. /* Display a segment descriptor stored at index IDX in a descriptor
  1360. table whose type is TYPE and whose base address is BASE_ADDR. If
  1361. FORCE is non-zero, display even invalid descriptors. */
  1362. static void
  1363. display_descriptor (unsigned type, unsigned long base_addr, int idx, int force)
  1364. {
  1365. struct seg_descr descr;
  1366. struct gate_descr gate;
  1367. /* Get the descriptor from the table. */
  1368. if (idx == 0 && type == 0)
  1369. gdb_puts ("0x000: null descriptor\n");
  1370. else if (get_descriptor (base_addr, idx, &descr) != -1)
  1371. {
  1372. /* For each type of descriptor table, this has a bit set if the
  1373. corresponding type of selectors is valid in that table. */
  1374. static unsigned allowed_descriptors[] = {
  1375. 0xffffdafeL, /* GDT */
  1376. 0x0000c0e0L, /* IDT */
  1377. 0xffffdafaL /* LDT */
  1378. };
  1379. /* If the program hasn't started yet, assume the debuggee will
  1380. have the same CPL as the debugger. */
  1381. int cpl = prog_has_started ? (a_tss.tss_cs & 3) : _my_cs () & 3;
  1382. unsigned long limit = (descr.limit1 << 16) | descr.limit0;
  1383. if (descr.present
  1384. && (allowed_descriptors[type] & (1 << descr.stype)) != 0)
  1385. {
  1386. gdb_printf ("0x%03x: ",
  1387. type == 1
  1388. ? idx : (idx * 8) | (type ? (cpl | 4) : 0));
  1389. if (descr.page_granular)
  1390. limit = (limit << 12) | 0xfff; /* big segment: low 12 bit set */
  1391. if (descr.stype == 1 || descr.stype == 2 || descr.stype == 3
  1392. || descr.stype == 9 || descr.stype == 11
  1393. || (descr.stype >= 16 && descr.stype < 32))
  1394. gdb_printf ("base=0x%02x%02x%04x limit=0x%08lx",
  1395. descr.base2, descr.base1, descr.base0, limit);
  1396. switch (descr.stype)
  1397. {
  1398. case 1:
  1399. case 3:
  1400. gdb_printf (" 16-bit TSS (task %sactive)",
  1401. descr.stype == 3 ? "" : "in");
  1402. break;
  1403. case 2:
  1404. gdb_puts (" LDT");
  1405. break;
  1406. case 4:
  1407. memcpy (&gate, &descr, sizeof gate);
  1408. gdb_printf ("selector=0x%04x offs=0x%04x%04x",
  1409. gate.selector, gate.offset1, gate.offset0);
  1410. gdb_printf (" 16-bit Call Gate (params=%d)",
  1411. gate.param_count);
  1412. break;
  1413. case 5:
  1414. gdb_printf ("TSS selector=0x%04x", descr.base0);
  1415. gdb_printf ("%*sTask Gate", 16, "");
  1416. break;
  1417. case 6:
  1418. case 7:
  1419. memcpy (&gate, &descr, sizeof gate);
  1420. gdb_printf ("selector=0x%04x offs=0x%04x%04x",
  1421. gate.selector, gate.offset1, gate.offset0);
  1422. gdb_printf (" 16-bit %s Gate",
  1423. descr.stype == 6 ? "Interrupt" : "Trap");
  1424. break;
  1425. case 9:
  1426. case 11:
  1427. gdb_printf (" 32-bit TSS (task %sactive)",
  1428. descr.stype == 3 ? "" : "in");
  1429. break;
  1430. case 12:
  1431. memcpy (&gate, &descr, sizeof gate);
  1432. gdb_printf ("selector=0x%04x offs=0x%04x%04x",
  1433. gate.selector, gate.offset1, gate.offset0);
  1434. gdb_printf (" 32-bit Call Gate (params=%d)",
  1435. gate.param_count);
  1436. break;
  1437. case 14:
  1438. case 15:
  1439. memcpy (&gate, &descr, sizeof gate);
  1440. gdb_printf ("selector=0x%04x offs=0x%04x%04x",
  1441. gate.selector, gate.offset1, gate.offset0);
  1442. gdb_printf (" 32-bit %s Gate",
  1443. descr.stype == 14 ? "Interrupt" : "Trap");
  1444. break;
  1445. case 16: /* data segments */
  1446. case 17:
  1447. case 18:
  1448. case 19:
  1449. case 20:
  1450. case 21:
  1451. case 22:
  1452. case 23:
  1453. gdb_printf (" %s-bit Data (%s Exp-%s%s)",
  1454. descr.bit32 ? "32" : "16",
  1455. descr.stype & 2
  1456. ? "Read/Write," : "Read-Only, ",
  1457. descr.stype & 4 ? "down" : "up",
  1458. descr.stype & 1 ? "" : ", N.Acc");
  1459. break;
  1460. case 24: /* code segments */
  1461. case 25:
  1462. case 26:
  1463. case 27:
  1464. case 28:
  1465. case 29:
  1466. case 30:
  1467. case 31:
  1468. gdb_printf (" %s-bit Code (%s, %sConf%s)",
  1469. descr.bit32 ? "32" : "16",
  1470. descr.stype & 2 ? "Exec/Read" : "Exec-Only",
  1471. descr.stype & 4 ? "" : "N.",
  1472. descr.stype & 1 ? "" : ", N.Acc");
  1473. break;
  1474. default:
  1475. gdb_printf ("Unknown type 0x%02x", descr.stype);
  1476. break;
  1477. }
  1478. gdb_puts ("\n");
  1479. }
  1480. else if (force)
  1481. {
  1482. gdb_printf ("0x%03x: ",
  1483. type == 1
  1484. ? idx : (idx * 8) | (type ? (cpl | 4) : 0));
  1485. if (!descr.present)
  1486. gdb_puts ("Segment not present\n");
  1487. else
  1488. gdb_printf ("Segment type 0x%02x is invalid in this table\n",
  1489. descr.stype);
  1490. }
  1491. }
  1492. else if (force)
  1493. gdb_printf ("0x%03x: Cannot read this descriptor\n", idx);
  1494. }
  1495. static void
  1496. go32_sldt (const char *arg, int from_tty)
  1497. {
  1498. struct dtr_reg gdtr;
  1499. unsigned short ldtr = 0;
  1500. int ldt_idx;
  1501. struct seg_descr ldt_descr;
  1502. long ldt_entry = -1L;
  1503. int cpl = (prog_has_started ? a_tss.tss_cs : _my_cs ()) & 3;
  1504. if (arg && *arg)
  1505. {
  1506. arg = skip_spaces (arg);
  1507. if (*arg)
  1508. {
  1509. ldt_entry = parse_and_eval_long (arg);
  1510. if (ldt_entry < 0
  1511. || (ldt_entry & 4) == 0
  1512. || (ldt_entry & 3) != (cpl & 3))
  1513. error (_("Invalid LDT entry 0x%03lx."), (unsigned long)ldt_entry);
  1514. }
  1515. }
  1516. __asm__ __volatile__ ("sgdt %0" : "=m" (gdtr) : /* no inputs */ );
  1517. __asm__ __volatile__ ("sldt %0" : "=m" (ldtr) : /* no inputs */ );
  1518. ldt_idx = ldtr / 8;
  1519. if (ldt_idx == 0)
  1520. gdb_puts ("There is no LDT.\n");
  1521. /* LDT's entry in the GDT must have the type LDT, which is 2. */
  1522. else if (get_descriptor (gdtr.base, ldt_idx, &ldt_descr) != 2)
  1523. gdb_printf ("LDT is present (at %#x), but unreadable by GDB.\n",
  1524. ldt_descr.base0
  1525. | (ldt_descr.base1 << 16)
  1526. | (ldt_descr.base2 << 24));
  1527. else
  1528. {
  1529. unsigned base =
  1530. ldt_descr.base0
  1531. | (ldt_descr.base1 << 16)
  1532. | (ldt_descr.base2 << 24);
  1533. unsigned limit = ldt_descr.limit0 | (ldt_descr.limit1 << 16);
  1534. int max_entry;
  1535. if (ldt_descr.page_granular)
  1536. /* Page-granular segments must have the low 12 bits of their
  1537. limit set. */
  1538. limit = (limit << 12) | 0xfff;
  1539. /* LDT cannot have more than 8K 8-byte entries, i.e. more than
  1540. 64KB. */
  1541. if (limit > 0xffff)
  1542. limit = 0xffff;
  1543. max_entry = (limit + 1) / 8;
  1544. if (ldt_entry >= 0)
  1545. {
  1546. if (ldt_entry > limit)
  1547. error (_("Invalid LDT entry %#lx: outside valid limits [0..%#x]"),
  1548. (unsigned long)ldt_entry, limit);
  1549. display_descriptor (ldt_descr.stype, base, ldt_entry / 8, 1);
  1550. }
  1551. else
  1552. {
  1553. int i;
  1554. for (i = 0; i < max_entry; i++)
  1555. display_descriptor (ldt_descr.stype, base, i, 0);
  1556. }
  1557. }
  1558. }
  1559. static void
  1560. go32_sgdt (const char *arg, int from_tty)
  1561. {
  1562. struct dtr_reg gdtr;
  1563. long gdt_entry = -1L;
  1564. int max_entry;
  1565. if (arg && *arg)
  1566. {
  1567. arg = skip_spaces (arg);
  1568. if (*arg)
  1569. {
  1570. gdt_entry = parse_and_eval_long (arg);
  1571. if (gdt_entry < 0 || (gdt_entry & 7) != 0)
  1572. error (_("Invalid GDT entry 0x%03lx: "
  1573. "not an integral multiple of 8."),
  1574. (unsigned long)gdt_entry);
  1575. }
  1576. }
  1577. __asm__ __volatile__ ("sgdt %0" : "=m" (gdtr) : /* no inputs */ );
  1578. max_entry = (gdtr.limit + 1) / 8;
  1579. if (gdt_entry >= 0)
  1580. {
  1581. if (gdt_entry > gdtr.limit)
  1582. error (_("Invalid GDT entry %#lx: outside valid limits [0..%#x]"),
  1583. (unsigned long)gdt_entry, gdtr.limit);
  1584. display_descriptor (0, gdtr.base, gdt_entry / 8, 1);
  1585. }
  1586. else
  1587. {
  1588. int i;
  1589. for (i = 0; i < max_entry; i++)
  1590. display_descriptor (0, gdtr.base, i, 0);
  1591. }
  1592. }
  1593. static void
  1594. go32_sidt (const char *arg, int from_tty)
  1595. {
  1596. struct dtr_reg idtr;
  1597. long idt_entry = -1L;
  1598. int max_entry;
  1599. if (arg && *arg)
  1600. {
  1601. arg = skip_spaces (arg);
  1602. if (*arg)
  1603. {
  1604. idt_entry = parse_and_eval_long (arg);
  1605. if (idt_entry < 0)
  1606. error (_("Invalid (negative) IDT entry %ld."), idt_entry);
  1607. }
  1608. }
  1609. __asm__ __volatile__ ("sidt %0" : "=m" (idtr) : /* no inputs */ );
  1610. max_entry = (idtr.limit + 1) / 8;
  1611. if (max_entry > 0x100) /* No more than 256 entries. */
  1612. max_entry = 0x100;
  1613. if (idt_entry >= 0)
  1614. {
  1615. if (idt_entry > idtr.limit)
  1616. error (_("Invalid IDT entry %#lx: outside valid limits [0..%#x]"),
  1617. (unsigned long)idt_entry, idtr.limit);
  1618. display_descriptor (1, idtr.base, idt_entry, 1);
  1619. }
  1620. else
  1621. {
  1622. int i;
  1623. for (i = 0; i < max_entry; i++)
  1624. display_descriptor (1, idtr.base, i, 0);
  1625. }
  1626. }
  1627. /* Cached linear address of the base of the page directory. For
  1628. now, available only under CWSDPMI. Code based on ideas and
  1629. suggestions from Charles Sandmann <sandmann@clio.rice.edu>. */
  1630. static unsigned long pdbr;
  1631. static unsigned long
  1632. get_cr3 (void)
  1633. {
  1634. unsigned offset;
  1635. unsigned taskreg;
  1636. unsigned long taskbase, cr3;
  1637. struct dtr_reg gdtr;
  1638. if (pdbr > 0 && pdbr <= 0xfffff)
  1639. return pdbr;
  1640. /* Get the linear address of GDT and the Task Register. */
  1641. __asm__ __volatile__ ("sgdt %0" : "=m" (gdtr) : /* no inputs */ );
  1642. __asm__ __volatile__ ("str %0" : "=m" (taskreg) : /* no inputs */ );
  1643. /* Task Register is a segment selector for the TSS of the current
  1644. task. Therefore, it can be used as an index into the GDT to get
  1645. at the segment descriptor for the TSS. To get the index, reset
  1646. the low 3 bits of the selector (which give the CPL). Add 2 to the
  1647. offset to point to the 3 low bytes of the base address. */
  1648. offset = gdtr.base + (taskreg & 0xfff8) + 2;
  1649. /* CWSDPMI's task base is always under the 1MB mark. */
  1650. if (offset > 0xfffff)
  1651. return 0;
  1652. _farsetsel (_dos_ds);
  1653. taskbase = _farnspeekl (offset) & 0xffffffU;
  1654. taskbase += _farnspeekl (offset + 2) & 0xff000000U;
  1655. if (taskbase > 0xfffff)
  1656. return 0;
  1657. /* CR3 (a.k.a. PDBR, the Page Directory Base Register) is stored at
  1658. offset 1Ch in the TSS. */
  1659. cr3 = _farnspeekl (taskbase + 0x1c) & ~0xfff;
  1660. if (cr3 > 0xfffff)
  1661. {
  1662. #if 0 /* Not fully supported yet. */
  1663. /* The Page Directory is in UMBs. In that case, CWSDPMI puts
  1664. the first Page Table right below the Page Directory. Thus,
  1665. the first Page Table's entry for its own address and the Page
  1666. Directory entry for that Page Table will hold the same
  1667. physical address. The loop below searches the entire UMB
  1668. range of addresses for such an occurrence. */
  1669. unsigned long addr, pte_idx;
  1670. for (addr = 0xb0000, pte_idx = 0xb0;
  1671. pte_idx < 0xff;
  1672. addr += 0x1000, pte_idx++)
  1673. {
  1674. if (((_farnspeekl (addr + 4 * pte_idx) & 0xfffff027) ==
  1675. (_farnspeekl (addr + 0x1000) & 0xfffff027))
  1676. && ((_farnspeekl (addr + 4 * pte_idx + 4) & 0xfffff000) == cr3))
  1677. {
  1678. cr3 = addr + 0x1000;
  1679. break;
  1680. }
  1681. }
  1682. #endif
  1683. if (cr3 > 0xfffff)
  1684. cr3 = 0;
  1685. }
  1686. return cr3;
  1687. }
  1688. /* Return the N'th Page Directory entry. */
  1689. static unsigned long
  1690. get_pde (int n)
  1691. {
  1692. unsigned long pde = 0;
  1693. if (pdbr && n >= 0 && n < 1024)
  1694. {
  1695. pde = _farpeekl (_dos_ds, pdbr + 4*n);
  1696. }
  1697. return pde;
  1698. }
  1699. /* Return the N'th entry of the Page Table whose Page Directory entry
  1700. is PDE. */
  1701. static unsigned long
  1702. get_pte (unsigned long pde, int n)
  1703. {
  1704. unsigned long pte = 0;
  1705. /* pde & 0x80 tests the 4MB page bit. We don't support 4MB
  1706. page tables, for now. */
  1707. if ((pde & 1) && !(pde & 0x80) && n >= 0 && n < 1024)
  1708. {
  1709. pde &= ~0xfff; /* Clear non-address bits. */
  1710. pte = _farpeekl (_dos_ds, pde + 4*n);
  1711. }
  1712. return pte;
  1713. }
  1714. /* Display a Page Directory or Page Table entry. IS_DIR, if non-zero,
  1715. says this is a Page Directory entry. If FORCE is non-zero, display
  1716. the entry even if its Present flag is off. OFF is the offset of the
  1717. address from the page's base address. */
  1718. static void
  1719. display_ptable_entry (unsigned long entry, int is_dir, int force, unsigned off)
  1720. {
  1721. if ((entry & 1) != 0)
  1722. {
  1723. gdb_printf ("Base=0x%05lx000", entry >> 12);
  1724. if ((entry & 0x100) && !is_dir)
  1725. gdb_puts (" Global");
  1726. if ((entry & 0x40) && !is_dir)
  1727. gdb_puts (" Dirty");
  1728. gdb_printf (" %sAcc.", (entry & 0x20) ? "" : "Not-");
  1729. gdb_printf (" %sCached", (entry & 0x10) ? "" : "Not-");
  1730. gdb_printf (" Write-%s", (entry & 8) ? "Thru" : "Back");
  1731. gdb_printf (" %s", (entry & 4) ? "Usr" : "Sup");
  1732. gdb_printf (" Read-%s", (entry & 2) ? "Write" : "Only");
  1733. if (off)
  1734. gdb_printf (" +0x%x", off);
  1735. gdb_puts ("\n");
  1736. }
  1737. else if (force)
  1738. gdb_printf ("Page%s not present or not supported; value=0x%lx.\n",
  1739. is_dir ? " Table" : "", entry >> 1);
  1740. }
  1741. static void
  1742. go32_pde (const char *arg, int from_tty)
  1743. {
  1744. long pde_idx = -1, i;
  1745. if (arg && *arg)
  1746. {
  1747. arg = skip_spaces (arg);
  1748. if (*arg)
  1749. {
  1750. pde_idx = parse_and_eval_long (arg);
  1751. if (pde_idx < 0 || pde_idx >= 1024)
  1752. error (_("Entry %ld is outside valid limits [0..1023]."), pde_idx);
  1753. }
  1754. }
  1755. pdbr = get_cr3 ();
  1756. if (!pdbr)
  1757. gdb_puts ("Access to Page Directories is "
  1758. "not supported on this system.\n");
  1759. else if (pde_idx >= 0)
  1760. display_ptable_entry (get_pde (pde_idx), 1, 1, 0);
  1761. else
  1762. for (i = 0; i < 1024; i++)
  1763. display_ptable_entry (get_pde (i), 1, 0, 0);
  1764. }
  1765. /* A helper function to display entries in a Page Table pointed to by
  1766. the N'th entry in the Page Directory. If FORCE is non-zero, say
  1767. something even if the Page Table is not accessible. */
  1768. static void
  1769. display_page_table (long n, int force)
  1770. {
  1771. unsigned long pde = get_pde (n);
  1772. if ((pde & 1) != 0)
  1773. {
  1774. int i;
  1775. gdb_printf ("Page Table pointed to by "
  1776. "Page Directory entry 0x%lx:\n", n);
  1777. for (i = 0; i < 1024; i++)
  1778. display_ptable_entry (get_pte (pde, i), 0, 0, 0);
  1779. gdb_puts ("\n");
  1780. }
  1781. else if (force)
  1782. gdb_printf ("Page Table not present; value=0x%lx.\n", pde >> 1);
  1783. }
  1784. static void
  1785. go32_pte (const char *arg, int from_tty)
  1786. {
  1787. long pde_idx = -1L, i;
  1788. if (arg && *arg)
  1789. {
  1790. arg = skip_spaces (arg);
  1791. if (*arg)
  1792. {
  1793. pde_idx = parse_and_eval_long (arg);
  1794. if (pde_idx < 0 || pde_idx >= 1024)
  1795. error (_("Entry %ld is outside valid limits [0..1023]."), pde_idx);
  1796. }
  1797. }
  1798. pdbr = get_cr3 ();
  1799. if (!pdbr)
  1800. gdb_puts ("Access to Page Tables is not supported on this system.\n");
  1801. else if (pde_idx >= 0)
  1802. display_page_table (pde_idx, 1);
  1803. else
  1804. for (i = 0; i < 1024; i++)
  1805. display_page_table (i, 0);
  1806. }
  1807. static void
  1808. go32_pte_for_address (const char *arg, int from_tty)
  1809. {
  1810. CORE_ADDR addr = 0, i;
  1811. if (arg && *arg)
  1812. {
  1813. arg = skip_spaces (arg);
  1814. if (*arg)
  1815. addr = parse_and_eval_address (arg);
  1816. }
  1817. if (!addr)
  1818. error_no_arg (_("linear address"));
  1819. pdbr = get_cr3 ();
  1820. if (!pdbr)
  1821. gdb_puts ("Access to Page Tables is not supported on this system.\n");
  1822. else
  1823. {
  1824. int pde_idx = (addr >> 22) & 0x3ff;
  1825. int pte_idx = (addr >> 12) & 0x3ff;
  1826. unsigned offs = addr & 0xfff;
  1827. gdb_printf ("Page Table entry for address %s:\n",
  1828. hex_string(addr));
  1829. display_ptable_entry (get_pte (get_pde (pde_idx), pte_idx), 0, 1, offs);
  1830. }
  1831. }
  1832. static struct cmd_list_element *info_dos_cmdlist = NULL;
  1833. void _initialize_go32_nat ();
  1834. void
  1835. _initialize_go32_nat ()
  1836. {
  1837. x86_dr_low.set_control = go32_set_dr7;
  1838. x86_dr_low.set_addr = go32_set_dr;
  1839. x86_dr_low.get_status = go32_get_dr6;
  1840. x86_dr_low.get_control = go32_get_dr7;
  1841. x86_dr_low.get_addr = go32_get_dr;
  1842. x86_set_debug_register_length (4);
  1843. add_inf_child_target (&the_go32_nat_target);
  1844. /* Initialize child's cwd as empty to be initialized when starting
  1845. the child. */
  1846. *child_cwd = 0;
  1847. /* Initialize child's command line storage. */
  1848. if (redir_debug_init (&child_cmd) == -1)
  1849. internal_error (__FILE__, __LINE__,
  1850. _("Cannot allocate redirection storage: "
  1851. "not enough memory.\n"));
  1852. /* We are always processing GCC-compiled programs. */
  1853. processing_gcc_compilation = 2;
  1854. add_basic_prefix_cmd ("dos", class_info, _("\
  1855. Print information specific to DJGPP (aka MS-DOS) debugging."),
  1856. &info_dos_cmdlist, 0, &infolist);
  1857. add_cmd ("sysinfo", class_info, go32_sysinfo, _("\
  1858. Display information about the target system, including CPU, OS, DPMI, etc."),
  1859. &info_dos_cmdlist);
  1860. add_cmd ("ldt", class_info, go32_sldt, _("\
  1861. Display entries in the LDT (Local Descriptor Table).\n\
  1862. Entry number (an expression) as an argument means display only that entry."),
  1863. &info_dos_cmdlist);
  1864. add_cmd ("gdt", class_info, go32_sgdt, _("\
  1865. Display entries in the GDT (Global Descriptor Table).\n\
  1866. Entry number (an expression) as an argument means display only that entry."),
  1867. &info_dos_cmdlist);
  1868. add_cmd ("idt", class_info, go32_sidt, _("\
  1869. Display entries in the IDT (Interrupt Descriptor Table).\n\
  1870. Entry number (an expression) as an argument means display only that entry."),
  1871. &info_dos_cmdlist);
  1872. add_cmd ("pde", class_info, go32_pde, _("\
  1873. Display entries in the Page Directory.\n\
  1874. Entry number (an expression) as an argument means display only that entry."),
  1875. &info_dos_cmdlist);
  1876. add_cmd ("pte", class_info, go32_pte, _("\
  1877. Display entries in Page Tables.\n\
  1878. Entry number (an expression) as an argument means display only entries\n\
  1879. from the Page Table pointed to by the specified Page Directory entry."),
  1880. &info_dos_cmdlist);
  1881. add_cmd ("address-pte", class_info, go32_pte_for_address, _("\
  1882. Display a Page Table entry for a linear address.\n\
  1883. The address argument must be a linear address, after adding to\n\
  1884. it the base address of the appropriate segment.\n\
  1885. The base address of variables and functions in the debuggee's data\n\
  1886. or code segment is stored in the variable __djgpp_base_address,\n\
  1887. so use `__djgpp_base_address + (char *)&var' as the argument.\n\
  1888. For other segments, look up their base address in the output of\n\
  1889. the `info dos ldt' command."),
  1890. &info_dos_cmdlist);
  1891. }
  1892. pid_t
  1893. tcgetpgrp (int fd)
  1894. {
  1895. if (isatty (fd))
  1896. return SOME_PID;
  1897. errno = ENOTTY;
  1898. return -1;
  1899. }
  1900. int
  1901. tcsetpgrp (int fd, pid_t pgid)
  1902. {
  1903. if (isatty (fd) && pgid == SOME_PID)
  1904. return 0;
  1905. errno = pgid == SOME_PID ? ENOTTY : ENOSYS;
  1906. return -1;
  1907. }