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