OpenCores
URL https://opencores.org/ocsvn/ao486/ao486/trunk

Subversion Repositories ao486

[/] [ao486/] [trunk/] [bochs486/] [cpu/] [jmp_far.cc] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
////////////////////////////////////////////////////////////////////////
2
// $Id: jmp_far.cc 10969 2012-01-11 20:21:29Z sshwarts $
3
/////////////////////////////////////////////////////////////////////////
4
//
5
//   Copyright (c) 2005-2012 Stanislav Shwartsman
6
//          Written by Stanislav Shwartsman [sshwarts at sourceforge net]
7
//
8
//  This library is free software; you can redistribute it and/or
9
//  modify it under the terms of the GNU Lesser General Public
10
//  License as published by the Free Software Foundation; either
11
//  version 2 of the License, or (at your option) any later version.
12
//
13
//  This library 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 GNU
16
//  Lesser General Public License for more details.
17
//
18
//  You should have received a copy of the GNU Lesser General Public
19
//  License along with this library; if not, write to the Free Software
20
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
21
//
22
////////////////////////////////////////////////////////////////////////
23
 
24
#define NEED_CPU_REG_SHORTCUTS 1
25
#include "bochs.h"
26
#include "cpu.h"
27
#define LOG_THIS BX_CPU_THIS_PTR
28
 
29
  void BX_CPP_AttrRegparmN(3)
30
BX_CPU_C::jump_protected(bxInstruction_c *i, Bit16u cs_raw, bx_address disp)
31
{
32
  bx_descriptor_t  descriptor;
33
  bx_selector_t    selector;
34
  Bit32u dword1, dword2;
35
 
36
  /* destination selector is not null else #GP(0) */
37
  if ((cs_raw & 0xfffc) == 0) {
38
    BX_ERROR(("jump_protected: cs == 0"));
39
    exception(BX_GP_EXCEPTION, 0);
40
  }
41
 
42
  parse_selector(cs_raw, &selector);
43
 
44
  /* destination selector index is within its descriptor table
45
     limits else #GP(selector) */
46
  fetch_raw_descriptor(&selector, &dword1, &dword2, BX_GP_EXCEPTION);
47
 
48
  /* examine AR byte of destination selector for legal values: */
49
  parse_descriptor(dword1, dword2, &descriptor);
50
 
51
  if (descriptor.segment) {
52
    check_cs(&descriptor, cs_raw, BX_SELECTOR_RPL(cs_raw), CPL);
53
    branch_far64(&selector, &descriptor, disp, CPL);
54
    return;
55
  }
56
  else {
57
    // call gate DPL must be >= CPL else #GP(gate selector)
58
    if (descriptor.dpl < CPL) {
59
      BX_ERROR(("jump_protected: call gate.dpl < CPL"));
60
      exception(BX_GP_EXCEPTION, cs_raw & 0xfffc);
61
    }
62
 
63
    // call gate DPL must be >= gate selector RPL else #GP(gate selector)
64
    if (descriptor.dpl < selector.rpl) {
65
      BX_ERROR(("jump_protected: call gate.dpl < selector.rpl"));
66
      exception(BX_GP_EXCEPTION, cs_raw & 0xfffc);
67
    }
68
 
69
#if BX_SUPPORT_X86_64
70
    if (long_mode()) {
71
      if (descriptor.type != BX_386_CALL_GATE) {
72
        BX_ERROR(("jump_protected: gate type %u unsupported in long mode", (unsigned) descriptor.type));
73
        exception(BX_GP_EXCEPTION, cs_raw & 0xfffc);
74
      }
75
      // gate must be present else #NP(gate selector)
76
      if (! IS_PRESENT(descriptor)) {
77
        BX_ERROR(("jump_protected: call gate not present!"));
78
        exception(BX_NP_EXCEPTION, cs_raw & 0xfffc);
79
      }
80
 
81
      jmp_call_gate64(&selector);
82
      return;
83
    }
84
#endif
85
 
86
    switch (descriptor.type) {
87
      case BX_SYS_SEGMENT_AVAIL_286_TSS:
88
      case BX_SYS_SEGMENT_AVAIL_386_TSS:
89
 
90
        if (descriptor.type==BX_SYS_SEGMENT_AVAIL_286_TSS)
91
          BX_DEBUG(("jump_protected: jump to 286 TSS"));
92
        else
93
          BX_DEBUG(("jump_protected: jump to 386 TSS"));
94
 
95
        if (descriptor.valid==0 || selector.ti) {
96
          BX_ERROR(("jump_protected: jump to bad TSS selector !"));
97
          exception(BX_GP_EXCEPTION, cs_raw & 0xfffc);
98
        }
99
 
100
        // TSS must be present, else #NP(TSS selector)
101
        if (! IS_PRESENT(descriptor)) {
102
          BX_ERROR(("jump_protected: jump to not present TSS !"));
103
          exception(BX_NP_EXCEPTION, cs_raw & 0xfffc);
104
        }
105
 
106
        // SWITCH_TASKS _without_ nesting to TSS
107
        task_switch(i, &selector, &descriptor, BX_TASK_FROM_JUMP, dword1, dword2);
108
        return;
109
 
110
      case BX_TASK_GATE:
111
        task_gate(i, &selector, &descriptor, BX_TASK_FROM_JUMP);
112
        return;
113
 
114
      case BX_286_CALL_GATE:
115
      case BX_386_CALL_GATE:
116
        jmp_call_gate(&selector, &descriptor);
117
        return;
118
 
119
      default:
120
        BX_ERROR(("jump_protected: gate type %u unsupported", (unsigned) descriptor.type));
121
        exception(BX_GP_EXCEPTION, cs_raw & 0xfffc);
122
    }
123
  }
124
}
125
 
126
void BX_CPU_C::task_gate(bxInstruction_c *i, bx_selector_t *selector, bx_descriptor_t *gate_descriptor, unsigned source)
127
{
128
  Bit16u          raw_tss_selector;
129
  bx_selector_t   tss_selector;
130
  bx_descriptor_t tss_descriptor;
131
  Bit32u dword1, dword2;
132
 
133
  // task gate must be present else #NP(gate selector)
134
  if (! gate_descriptor->p) {
135
    BX_ERROR(("task_gate: task gate not present"));
136
    exception(BX_NP_EXCEPTION, selector->value & 0xfffc);
137
  }
138
 
139
  // examine selector to TSS, given in Task Gate descriptor
140
  // must specify global in the local/global bit else #GP(TSS selector)
141
  raw_tss_selector = gate_descriptor->u.taskgate.tss_selector;
142
  parse_selector(raw_tss_selector, &tss_selector);
143
 
144
  if (tss_selector.ti) {
145
    BX_ERROR(("task_gate: tss_selector.ti=1"));
146
    exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc);
147
  }
148
 
149
  // index must be within GDT limits else #GP(TSS selector)
150
  fetch_raw_descriptor(&tss_selector, &dword1, &dword2, BX_GP_EXCEPTION);
151
 
152
  // descriptor AR byte must specify available TSS
153
  //   else #GP(TSS selector)
154
  parse_descriptor(dword1, dword2, &tss_descriptor);
155
 
156
  if (tss_descriptor.valid==0 || tss_descriptor.segment) {
157
    BX_ERROR(("task_gate: TSS selector points to bad TSS"));
158
    exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc);
159
  }
160
  if (tss_descriptor.type!=BX_SYS_SEGMENT_AVAIL_286_TSS &&
161
      tss_descriptor.type!=BX_SYS_SEGMENT_AVAIL_386_TSS)
162
  {
163
    BX_ERROR(("task_gate: TSS selector points to bad TSS"));
164
    exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc);
165
  }
166
 
167
  // task state segment must be present, else #NP(tss selector)
168
  if (! IS_PRESENT(tss_descriptor)) {
169
    BX_ERROR(("task_gate: TSS descriptor.p == 0"));
170
    exception(BX_NP_EXCEPTION, raw_tss_selector & 0xfffc);
171
  }
172
 
173
  // SWITCH_TASKS _without_ nesting to TSS
174
  task_switch(i, &tss_selector, &tss_descriptor, source, dword1, dword2);
175
}
176
 
177
  void BX_CPP_AttrRegparmN(2)
178
BX_CPU_C::jmp_call_gate(bx_selector_t *selector, bx_descriptor_t *gate_descriptor)
179
{
180
  bx_selector_t gate_cs_selector;
181
  bx_descriptor_t gate_cs_descriptor;
182
  Bit32u dword1, dword2;
183
 
184
  if (gate_descriptor->type==BX_286_CALL_GATE)
185
    BX_DEBUG(("jmp_call_gate: jump to 286 CALL GATE"));
186
  else
187
    BX_DEBUG(("jmp_call_gate: jump to 386 CALL GATE"));
188
 
189
  // task gate must be present else #NP(gate selector)
190
  if (! gate_descriptor->p) {
191
    BX_ERROR(("jmp_call_gate: call gate not present!"));
192
    exception(BX_NP_EXCEPTION, selector->value & 0xfffc);
193
  }
194
 
195
  // examine selector to code segment given in call gate descriptor
196
  // selector must not be null, else #GP(0)
197
  Bit16u gate_cs_raw = gate_descriptor->u.gate.dest_selector;
198
 
199
  if ((gate_cs_raw & 0xfffc) == 0) {
200
    BX_ERROR(("jmp_call_gate: CS selector null"));
201
    exception(BX_GP_EXCEPTION, 0);
202
  }
203
 
204
  parse_selector(gate_cs_raw, &gate_cs_selector);
205
  // selector must be within its descriptor table limits else #GP(CS selector)
206
  fetch_raw_descriptor(&gate_cs_selector, &dword1, &dword2, BX_GP_EXCEPTION);
207
  parse_descriptor(dword1, dword2, &gate_cs_descriptor);
208
 
209
  // check code-segment descriptor
210
  check_cs(&gate_cs_descriptor, gate_cs_raw, 0, CPL);
211
 
212
  Bit32u temp_EIP = gate_descriptor->u.gate.dest_offset;
213
  branch_far32(&gate_cs_selector, &gate_cs_descriptor, temp_EIP, CPL);
214
}
215
 
216
#if BX_SUPPORT_X86_64
217
  void BX_CPP_AttrRegparmN(1)
218
BX_CPU_C::jmp_call_gate64(bx_selector_t *gate_selector)
219
{
220
  bx_selector_t cs_selector;
221
  Bit32u dword1, dword2, dword3;
222
  bx_descriptor_t cs_descriptor;
223
  bx_descriptor_t gate_descriptor;
224
 
225
  BX_DEBUG(("jmp_call_gate64: jump to CALL GATE 64"));
226
 
227
  fetch_raw_descriptor_64(gate_selector, &dword1, &dword2, &dword3, BX_GP_EXCEPTION);
228
  parse_descriptor(dword1, dword2, &gate_descriptor);
229
 
230
  Bit16u dest_selector = gate_descriptor.u.gate.dest_selector;
231
  // selector must not be null else #GP(0)
232
  if ((dest_selector & 0xfffc) == 0) {
233
    BX_ERROR(("jmp_call_gate64: selector in gate null"));
234
    exception(BX_GP_EXCEPTION, 0);
235
  }
236
 
237
  parse_selector(dest_selector, &cs_selector);
238
  // selector must be within its descriptor table limits,
239
  //   else #GP(code segment selector)
240
  fetch_raw_descriptor(&cs_selector, &dword1, &dword2, BX_GP_EXCEPTION);
241
  parse_descriptor(dword1, dword2, &cs_descriptor);
242
 
243
  // find the RIP in the gate_descriptor
244
  Bit64u new_RIP = gate_descriptor.u.gate.dest_offset;
245
  new_RIP |= ((Bit64u)dword3 << 32);
246
 
247
  // AR byte of selected descriptor must indicate code segment,
248
  //   else #GP(code segment selector)
249
  if (cs_descriptor.valid==0 || cs_descriptor.segment==0 ||
250
      IS_DATA_SEGMENT(cs_descriptor.type))
251
  {
252
    BX_ERROR(("jmp_call_gate64: not code segment in 64-bit call gate"));
253
    exception(BX_GP_EXCEPTION, dest_selector & 0xfffc);
254
  }
255
 
256
  // In long mode, only 64-bit call gates are allowed, and they must point
257
  // to 64-bit code segments, else #GP(selector)
258
  if (! IS_LONG64_SEGMENT(cs_descriptor) || cs_descriptor.u.segment.d_b)
259
  {
260
    BX_ERROR(("jmp_call_gate64: not 64-bit code segment in 64-bit call gate"));
261
    exception(BX_GP_EXCEPTION, dest_selector & 0xfffc);
262
  }
263
 
264
  // check code-segment descriptor
265
  check_cs(&cs_descriptor, dest_selector, 0, CPL);
266
  // and transfer the control
267
  branch_far64(&cs_selector, &cs_descriptor, new_RIP, CPL);
268
}
269
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.