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