Patches to make Bochs 2.6.2 work with Pintos
[pintos-anon] / src / misc / bochs-2.6.2-jitter-plus-segv.patch
1 diff --git a/bochs.h b/bochs.h
2 index c2d6c6b..d37cf12 100644
3 --- a/bochs.h
4 +++ b/bochs.h
5 @@ -392,6 +392,7 @@ BOCHSAPI extern logfunc_t *genlog;
6  void bx_gdbstub_init(void);
7  void bx_gdbstub_break(void);
8  int bx_gdbstub_check(unsigned int eip);
9 +void bx_gdbstub_exception(unsigned int nr);
10  #define GDBSTUB_STOP_NO_REASON   (0xac0)
11  
12  #if BX_SUPPORT_SMP
13 @@ -589,4 +590,6 @@ BX_CPP_INLINE Bit64u bx_bswap64(Bit64u val64)
14  #define CopyHostQWordLittleEndian(hostAddrDst,  hostAddrSrc) \
15      (* (Bit64u *)(hostAddrDst)) = (* (Bit64u *)(hostAddrSrc));
16  
17 +extern int jitter;
18 +
19  #endif  /* BX_BOCHS_H */
20 diff --git a/cpu/exception.cc b/cpu/exception.cc
21 index db38d1b..b2c5f29 100644
22 --- a/cpu/exception.cc
23 +++ b/cpu/exception.cc
24 @@ -914,6 +914,10 @@ void BX_CPU_C::exception(unsigned vector, Bit16u error_code)
25  
26    BX_CPU_THIS_PTR last_exception_type = exception_type;
27  
28 +#if BX_GDBSTUB
29 +  bx_gdbstub_exception(vector);
30 +#endif
31 +
32    if (real_mode()) {
33      push_error = 0; // not INT, no error code pushed
34      error_code = 0;
35 diff --git a/gdbstub.cc b/gdbstub.cc
36 index da600b4..577938d 100644
37 --- a/gdbstub.cc
38 +++ b/gdbstub.cc
39 @@ -49,6 +49,7 @@ static int last_stop_reason = GDBSTUB_STOP_NO_REASON;
40  #define GDBSTUB_EXECUTION_BREAKPOINT    (0xac1)
41  #define GDBSTUB_TRACE                   (0xac2)
42  #define GDBSTUB_USER_BREAK              (0xac3)
43 +#define GDBSTUB_EXCEPTION_0E            (0xac4)
44  
45  static bx_list_c *gdbstub_list;
46  static int listen_socket_fd;
47 @@ -317,6 +318,12 @@ int bx_gdbstub_check(unsigned int eip)
48    return GDBSTUB_STOP_NO_REASON;
49  }
50  
51 +void bx_gdbstub_exception(unsigned int nr) 
52 +{
53 +    if (nr == 0x0e)
54 +        last_stop_reason = GDBSTUB_EXCEPTION_0E;
55 +}
56 +
57  static int remove_breakpoint(unsigned addr, int len)
58  {
59    if (len != 1)
60 @@ -487,6 +494,10 @@ static void debug_loop(void)
61          {
62            write_signal(&buf[1], SIGTRAP);
63          }
64 +        else if (last_stop_reason == GDBSTUB_EXCEPTION_0E)
65 +        {
66 +          write_signal(&buf[1], SIGSEGV);
67 +        }
68          else
69          {
70            write_signal(&buf[1], 0);
71 @@ -514,6 +525,10 @@ static void debug_loop(void)
72          {
73            write_signal(&buf[1], SIGTRAP);
74          }
75 +        else if (last_stop_reason == GDBSTUB_EXCEPTION_0E)
76 +        {
77 +          write_signal(&buf[1], SIGSEGV);
78 +        }
79          else
80          {
81            write_signal(&buf[1], SIGTRAP);
82 diff --git a/iodev/pit82c54.cc b/iodev/pit82c54.cc
83 index 09dcd8e..7e335d4 100644
84 --- a/iodev/pit82c54.cc
85 +++ b/iodev/pit82c54.cc
86 @@ -49,6 +49,7 @@
87  
88  #include "iodev.h"
89  #include "pit82c54.h"
90 +#include <stdlib.h>
91  #define LOG_THIS this->
92  
93  
94 @@ -410,7 +411,14 @@ void BX_CPP_AttrRegparmN(1) pit_82C54::clock(Bit8u cnum)
95        case 2:
96          if (thisctr.count_written) {
97            if (thisctr.triggerGATE || thisctr.first_pass) {
98 -            set_count(thisctr, thisctr.inlatch);
99 +            //set_count(thisctr, thisctr.inlatch);
100 +            unsigned n = thisctr.inlatch;
101 +            if (jitter && n > 5) {
102 +                n *= (double) rand() / RAND_MAX;
103 +                if (n < 5)
104 +                    n = 5;
105 +            }
106 +            set_count(thisctr, n);
107              thisctr.next_change_time=(thisctr.count_binary-1) & 0xFFFF;
108              thisctr.null_count=0;
109              if (thisctr.inlatch==1) {
110 diff --git a/main.cc b/main.cc
111 index 0f11e31..008f05f 100644
112 --- a/main.cc
113 +++ b/main.cc
114 @@ -101,6 +101,7 @@ BOCHSAPI BX_CPU_C bx_cpu;
115  BOCHSAPI BX_MEM_C bx_mem;
116  
117  char *bochsrc_filename = NULL;
118 +int jitter = 0;
119  
120  void bx_print_header()
121  {
122 @@ -639,6 +640,13 @@ int bx_init_main(int argc, char *argv[])
123        else SIM->get_param_string(BXPN_DEBUGGER_LOG_FILENAME)->set(argv[arg]);
124      }
125  #endif
126 +    else if (!strcmp ("-j", argv[arg])) {
127 +      if (++arg >= argc) BX_PANIC(("-j must be followed by a number"));
128 +      else {
129 +        jitter = 1;
130 +        srand (atoi (argv[arg]));
131 +      }
132 +    }
133      else if (!strcmp("-f", argv[arg])) {
134        if (++arg >= argc) BX_PANIC(("-f must be followed by a filename"));
135        else bochsrc_filename = argv[arg];