Add jitter feature to Bochs.
[pintos-anon] / src / misc / bochs-2.1.1.patch
1 The first two patches are needed for Bochs to properly compile and run
2 under Solaris on Sparc.  If you're not compiling on Solaris, don't
3 apply them.  The first should probably be handled by Autoconf; I'm not
4 sure why it isn't.  The second is needed to make GCC accept X.h's
5 frequent use of "extern foo(...)", because use of an implicit int
6 return type is forbidden in C++.
7
8 The third patch makes the gdb stubs work on Solaris/Sparc, by doing
9 proper byteswapping.  It should be harmless elsewhere.
10
11 The fourth patch enables the serial device under Solaris and disables
12 tty setup at the same time.  Tty setup is a pain when you want to
13 connect a serial port to stdout and it doesn't work under Solaris
14 anyway.  This patch is useful everywhere with `pintos', but may not be
15 wanted elsewhere.
16
17 The remaining patches add the `jitter' feature described in the
18 project documentation, in which timer interrupts are delivered at
19 random intervals.
20
21 To apply all the patches, cd into the Bochs directory, then type
22         patch -p1 < ~/pintos/src/misc/bochs-2.1.1.patch
23 You will have to supply the proper path to the patch, of course.  You
24 can provide the --dry-run option to patch if you want to test whether
25 the patch would apply cleanly before actually patching.
26
27 To apply a subset of the patches, use a text editor to delete the
28 unwanted patches, then follow the above instructions.
29
30 Here's a ./configure invocation for Bochs that works optimally with
31 the `pintos' utility:
32         ./configure --with-x --with-x11 --with-term --with-nogui
33 If you want the gdb stub, add --enable-gdb-stub
34 If you want the internal debugger, add --enable-debugger
35
36 diff -urp orig/bochs-2.1.1/Makefile.in bochs-2.1.1/Makefile.in
37 --- orig/bochs-2.1.1/Makefile.in        2004-02-11 14:28:02.000000000 -0800
38 +++ bochs-2.1.1/Makefile.in     2004-09-13 15:05:26.281550000 -0700
39 @@ -92,7 +92,7 @@ CXX = @CXX@
40  CFLAGS = @CFLAGS@ @GUI_CFLAGS@ $(MCH_CFLAGS) $(FLA_FLAGS) @DEFINE_PLUGIN_PATH@ -DBX_SHARE_PATH='"$(sharedir)"'
41  CXXFLAGS = @CXXFLAGS@ @GUI_CXXFLAGS@ $(MCH_CFLAGS) $(FLA_FLAGS) @DEFINE_PLUGIN_PATH@ -DBX_SHARE_PATH='"$(sharedir)"'
42  
43 -LDFLAGS = @LDFLAGS@
44 +LDFLAGS = @LDFLAGS@ -lsocket
45  LIBS = @LIBS@
46  # To compile with readline:
47  #   linux needs just -lreadline
48
49 diff -urp orig/bochs-2.1.1/gui/Makefile.in bochs-2.1.1/gui/Makefile.in
50 --- orig/bochs-2.1.1/gui/Makefile.in    2003-11-28 07:07:28.000000000 -0800
51 +++ bochs-2.1.1/gui/Makefile.in 2004-09-13 15:05:09.402039000 -0700
52 @@ -44,7 +44,7 @@ SHELL = /bin/sh
53  @SET_MAKE@
54  
55  CXX = @CXX@
56 -CXXFLAGS = $(BX_INCDIRS) @CXXFLAGS@  @GUI_CXXFLAGS@
57 +CXXFLAGS = $(BX_INCDIRS) @CXXFLAGS@  @GUI_CXXFLAGS@ -fms-extensions
58  LOCAL_CXXFLAGS =
59  LDFLAGS = @LDFLAGS@
60  LIBS = @LIBS@
61
62
63 diff -urp orig/bochs-2.1.1/gdbstub.cc bochs-2.1.1/gdbstub.cc
64 --- orig/bochs-2.1.1/gdbstub.cc 2004-02-11 14:28:41.000000000 -0800
65 +++ bochs-2.1.1/gdbstub.cc      2004-09-13 16:41:59.652988000 -0700
66 @@ -474,11 +475,13 @@ static void debug_loop(void)
67            case 'P':
68                {
69                   int reg;
70 -                 int value;
71 +                 Bit8u vbits[4];
72 +                 Bit32u value;
73                   char* ebuf;
74                   
75                   reg = strtoul(&buffer[1], &ebuf, 16);
76 -                 value = ntohl(strtoul(ebuf + 1, &ebuf, 16));
77 +                 hex2mem(ebuf + 1, vbits, sizeof value);
78 +                 ReadHostDWordFromLittleEndian(vbits, value);
79                   
80                   BX_INFO (("reg %d set to %x", reg, value));
81                   
82 @@ -527,35 +530,36 @@ static void debug_loop(void)
83                }
84              
85            case 'g':
86 -            registers[0] = EAX;
87 -            registers[1] = ECX;
88 -            registers[2] = EDX;
89 -            registers[3] = EBX;
90 -            registers[4] = ESP;
91 -            registers[5] = EBP;
92 -            registers[6] = ESI;
93 -            registers[7] = EDI;
94 +            WriteHostDWordToLittleEndian(registers + 0, EAX);
95 +            WriteHostDWordToLittleEndian(registers + 1, ECX);
96 +            WriteHostDWordToLittleEndian(registers + 2, EDX);
97 +            WriteHostDWordToLittleEndian(registers + 3, EBX);
98 +            WriteHostDWordToLittleEndian(registers + 4, ESP);
99 +            WriteHostDWordToLittleEndian(registers + 5, EBP);
100 +            WriteHostDWordToLittleEndian(registers + 6, ESI);
101 +            WriteHostDWordToLittleEndian(registers + 7, EDI);
102              if (last_stop_reason == GDBSTUB_EXECUTION_BREAKPOINT)
103                {
104 -                 registers[8] = EIP + 1;
105 +                WriteHostDWordToLittleEndian(registers + 8, EIP + 1);
106                }
107              else
108                {
109 -                 registers[8] = EIP;
110 +                WriteHostDWordToLittleEndian(registers + 8, EIP);
111                }
112 -            registers[9] = BX_CPU_THIS_PTR read_eflags();
113 -            registers[10] = 
114 -              BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
115 -            registers[11] = 
116 -              BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value;
117 -            registers[12] = 
118 -              BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value;
119 -            registers[13] = 
120 -              BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value;
121 -            registers[14] = 
122 -              BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].selector.value;
123 -            registers[15] = 
124 -              BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].selector.value;
125 +            WriteHostDWordToLittleEndian(registers + 9,
126 +                                         BX_CPU_THIS_PTR read_eflags());
127 +            WriteHostDWordToLittleEndian(registers + 10,
128 +              BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value);
129 +            WriteHostDWordToLittleEndian(registers + 11,
130 +              BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value);
131 +            WriteHostDWordToLittleEndian(registers + 12,
132 +              BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value);
133 +            WriteHostDWordToLittleEndian(registers + 13,
134 +              BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value);
135 +            WriteHostDWordToLittleEndian(registers + 14,
136 +              BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].selector.value);
137 +            WriteHostDWordToLittleEndian(registers + 15,
138 +              BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].selector.value);
139              mem2hex((char *)registers, obuf, NUMREGSBYTES);
140              put_reply(obuf);
141              break;
142 --- tmp/bochs-2.1.1/iodev/serial.cc     2004-02-11 14:28:54.000001000 -0800
143 +++ bochs-2.1.1/iodev/serial.cc 2004-09-14 23:02:04.000001000 -0700
144 @@ -53,7 +53,7 @@
145  #endif
146  #endif
147  
148 -#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__) || defined(__GNU__) || defined(__APPLE__)
149 +#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__) || defined(__GNU__) || defined(__APPLE__) || defined(__sun__)
150  #define SERIAL_ENABLE
151  #endif
152  
153 @@ -122,6 +122,7 @@
154      if (tty_id < 0)
155        BX_PANIC(("open of %s (%s) failed\n",
156                  "com1", bx_options.com[0].Odev->getptr ()));
157 +#if 0
158      BX_DEBUG(("tty_id: %d",tty_id));
159      tcgetattr(tty_id, &term_orig);
160      bcopy((caddr_t) &term_orig, (caddr_t) &term_new, sizeof(struct termios));
161 @@ -145,6 +146,7 @@
162      term_new.c_cc[VTIME] = 0;
163      //term_new.c_iflag |= IXOFF;
164      tcsetattr(tty_id, TCSAFLUSH, &term_new);
165 +#endif
166    }
167  #endif   /* def SERIAL_ENABLE */
168    // nothing for now
169
170 diff -urp bochs-2.1.1.orig/bochs.h bochs-2.1.1/bochs.h
171 --- bochs-2.1.1.orig/bochs.h    2004-02-11 14:28:03.000000000 -0800
172 +++ bochs-2.1.1/bochs.h 2004-09-20 17:02:01.000000000 -0700
173 @@ -757,4 +757,6 @@ int bx_init_hardware ();
174  
175  #endif
176  
177 +extern int jitter;
178 +
179  #endif  /* BX_BOCHS_H */
180 diff -urp bochs-2.1.1.orig/iodev/pit82c54.cc bochs-2.1.1/iodev/pit82c54.cc
181 --- bochs-2.1.1.orig/iodev/pit82c54.cc  2004-02-11 14:28:53.000000000 -0800
182 +++ bochs-2.1.1/iodev/pit82c54.cc       2004-09-20 17:18:24.000000000 -0700
183 @@ -28,6 +28,7 @@
184  
185  #include "bochs.h"
186  #include "pit82c54.h"
187 +#include <stdlib.h>
188  #define LOG_THIS this->
189  
190  
191 @@ -356,7 +357,13 @@ pit_82C54::clock(Bit8u cnum) {
192        case 2:
193         if(thisctr.count_written) {
194           if(thisctr.triggerGATE || thisctr.first_pass) {
195 -           set_count(thisctr, thisctr.inlatch);
196 +            unsigned n = thisctr.inlatch;
197 +            if (jitter) {
198 +                n *= (double) rand() / RAND_MAX;
199 +                if (n < 1)
200 +                    n = 1;
201 +            }
202 +           set_count(thisctr, n);
203             thisctr.next_change_time=(thisctr.count_binary-1) & 0xFFFF;
204             thisctr.null_count=0;
205             if(thisctr.inlatch==1) {
206 diff -urp bochs-2.1.1.orig/iodev/pit_wrap.cc bochs-2.1.1/iodev/pit_wrap.cc
207 diff -urp bochs-2.1.1.orig/iodev/serial.cc bochs-2.1.1/iodev/serial.cc
208 diff -urp bochs-2.1.1.orig/main.cc bochs-2.1.1/main.cc
209 --- bochs-2.1.1.orig/main.cc    2004-02-11 14:28:41.000000000 -0800
210 +++ bochs-2.1.1/main.cc 2004-09-20 17:15:39.000000000 -0700
211 @@ -58,6 +58,7 @@
212  
213  
214  int bochsrc_include_count = 0;
215 +int jitter = 0;
216  
217  extern "C" {
218  #include <signal.h>
219 @@ -2022,6 +2024,13 @@ bx_init_main (int argc, char *argv[])
220      else if (!strcmp ("-q", argv[arg])) {
221        SIM->get_param_enum(BXP_BOCHS_START)->set (BX_QUICK_START);
222      }
223 +    else if (!strcmp ("-j", argv[arg])) {
224 +      if (++arg >= argc) BX_PANIC(("-j must be followed by a number"));
225 +      else {
226 +        jitter = 1;
227 +        srand (atoi (argv[arg]));
228 +      }
229 +    }
230      else if (!strcmp ("-f", argv[arg])) {
231        if (++arg >= argc) BX_PANIC(("-f must be followed by a filename"));
232        else bochsrc_filename = argv[arg];