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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [bochs486/] [cpu/] [ret_far.cc] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
////////////////////////////////////////////////////////////////////////
2
// $Id: ret_far.cc 11106 2012-03-25 11:54:32Z 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(2)
30
BX_CPU_C::return_protected(bxInstruction_c *i, Bit16u pop_bytes)
31
{
32
  Bit16u raw_cs_selector, raw_ss_selector;
33
  bx_selector_t cs_selector, ss_selector;
34
  bx_descriptor_t cs_descriptor, ss_descriptor;
35
  Bit32u stack_param_offset;
36
  bx_address return_RIP, return_RSP, temp_RSP;
37
  Bit32u dword1, dword2;
38
 
39
  /* + 6+N*2: SS      | +12+N*4:     SS | +24+N*8      SS */
40
  /* + 4+N*2: SP      | + 8+N*4:    ESP | +16+N*8     RSP */
41
  /*          parm N  | +        parm N | +        parm N */
42
  /*          parm 3  | +        parm 3 | +        parm 3 */
43
  /*          parm 2  | +        parm 2 | +        parm 2 */
44
  /* + 4:     parm 1  | + 8:     parm 1 | +16:     parm 1 */
45
  /* + 2:     CS      | + 4:         CS | + 8:         CS */
46
  /* + 0:     IP      | + 0:        EIP | + 0:        RIP */
47
 
48
#if BX_SUPPORT_X86_64
49
  if (long64_mode()) temp_RSP = RSP;
50
  else
51
#endif
52
  {
53
    if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b) temp_RSP = ESP;
54
    else temp_RSP = SP;
55
  }
56
 
57
#if BX_SUPPORT_X86_64
58
  if (i->os64L()) {
59
    raw_cs_selector = (Bit16u) stack_read_qword(temp_RSP + 8);
60
    return_RIP      =          stack_read_qword(temp_RSP);
61
    stack_param_offset = 16;
62
  }
63
  else
64
#endif
65
  if (i->os32L()) {
66
    raw_cs_selector = (Bit16u) stack_read_dword(temp_RSP + 4);
67
    return_RIP      =          stack_read_dword(temp_RSP);
68
    stack_param_offset = 8;
69
  }
70
  else {
71
    raw_cs_selector = stack_read_word(temp_RSP + 2);
72
    return_RIP      = stack_read_word(temp_RSP);
73
    stack_param_offset = 4;
74
  }
75
 
76
  // selector must be non-null else #GP(0)
77
  if ((raw_cs_selector & 0xfffc) == 0) {
78
    BX_ERROR(("return_protected: CS selector null"));
79
    exception(BX_GP_EXCEPTION, 0);
80
  }
81
 
82
  parse_selector(raw_cs_selector, &cs_selector);
83
 
84
  // selector index must be within its descriptor table limits,
85
  // else #GP(selector)
86
  fetch_raw_descriptor(&cs_selector, &dword1, &dword2, BX_GP_EXCEPTION);
87
 
88
  // descriptor AR byte must indicate code segment, else #GP(selector)
89
  parse_descriptor(dword1, dword2, &cs_descriptor);
90
 
91
  // return selector RPL must be >= CPL, else #GP(return selector)
92
  if (cs_selector.rpl < CPL) {
93
    BX_ERROR(("return_protected: CS.rpl < CPL"));
94
    exception(BX_GP_EXCEPTION, raw_cs_selector & 0xfffc);
95
  }
96
 
97
  // check code-segment descriptor
98
  check_cs(&cs_descriptor, raw_cs_selector, 0, cs_selector.rpl);
99
 
100
  // if return selector RPL == CPL then
101
  // RETURN TO SAME PRIVILEGE LEVEL
102
  if (cs_selector.rpl == CPL)
103
  {
104
    BX_DEBUG(("return_protected: return to SAME PRIVILEGE LEVEL"));
105
 
106
    branch_far64(&cs_selector, &cs_descriptor, return_RIP, CPL);
107
 
108
#if BX_SUPPORT_X86_64
109
    if (long64_mode())
110
      RSP += stack_param_offset + pop_bytes;
111
    else
112
#endif
113
    {
114
      if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b)
115
        RSP = ESP + stack_param_offset + pop_bytes;
116
      else
117
         SP += stack_param_offset + pop_bytes;
118
    }
119
  }
120
  /* RETURN TO OUTER PRIVILEGE LEVEL */
121
  else {
122
    /* + 6+N*2: SS      | +12+N*4:     SS | +24+N*8      SS */
123
    /* + 4+N*2: SP      | + 8+N*4:    ESP | +16+N*8     RSP */
124
    /*          parm N  | +        parm N | +        parm N */
125
    /*          parm 3  | +        parm 3 | +        parm 3 */
126
    /*          parm 2  | +        parm 2 | +        parm 2 */
127
    /* + 4:     parm 1  | + 8:     parm 1 | +16:     parm 1 */
128
    /* + 2:     CS      | + 4:         CS | + 8:         CS */
129
    /* + 0:     IP      | + 0:        EIP | + 0:        RIP */
130
 
131
    BX_DEBUG(("return_protected: return to OUTER PRIVILEGE LEVEL"));
132
 
133
#if BX_SUPPORT_X86_64
134
    if (i->os64L()) {
135
      raw_ss_selector = stack_read_word(temp_RSP + 24 + pop_bytes);
136
      return_RSP      = stack_read_qword(temp_RSP + 16 + pop_bytes);
137
    }
138
    else
139
#endif
140
    if (i->os32L()) {
141
      raw_ss_selector = stack_read_word(temp_RSP + 12 + pop_bytes);
142
      return_RSP      = stack_read_dword(temp_RSP + 8 + pop_bytes);
143
    }
144
    else {
145
      raw_ss_selector = stack_read_word(temp_RSP + 6 + pop_bytes);
146
      return_RSP      = stack_read_word(temp_RSP + 4 + pop_bytes);
147
    }
148
 
149
    /* selector index must be within its descriptor table limits,
150
     * else #GP(selector) */
151
    parse_selector(raw_ss_selector, &ss_selector);
152
 
153
    if ((raw_ss_selector & 0xfffc) == 0) {
154
#if BX_SUPPORT_X86_64
155
      if (long_mode()) {
156
        if (! IS_LONG64_SEGMENT(cs_descriptor) || (cs_selector.rpl == 3)) {
157
          BX_ERROR(("return_protected: SS selector null"));
158
          exception(BX_GP_EXCEPTION, 0);
159
        }
160
      }
161
      else // not in long or compatibility mode
162
#endif
163
      {
164
        BX_ERROR(("return_protected: SS selector null"));
165
        exception(BX_GP_EXCEPTION, 0);
166
      }
167
    }
168
    else {
169
      fetch_raw_descriptor(&ss_selector, &dword1, &dword2, BX_GP_EXCEPTION);
170
      parse_descriptor(dword1, dword2, &ss_descriptor);
171
 
172
      /* selector RPL must = RPL of the return CS selector,
173
       * else #GP(selector) */
174
      if (ss_selector.rpl != cs_selector.rpl) {
175
        BX_ERROR(("return_protected: ss.rpl != cs.rpl"));
176
        exception(BX_GP_EXCEPTION, raw_ss_selector & 0xfffc);
177
      }
178
 
179
      /* descriptor AR byte must indicate a writable data segment,
180
       * else #GP(selector) */
181
      if (ss_descriptor.valid==0 || ss_descriptor.segment==0 ||
182
           IS_CODE_SEGMENT(ss_descriptor.type) ||
183
          !IS_DATA_SEGMENT_WRITEABLE(ss_descriptor.type))
184
      {
185
        BX_ERROR(("return_protected: SS.AR byte not writable data"));
186
        exception(BX_GP_EXCEPTION, raw_ss_selector & 0xfffc);
187
      }
188
 
189
      /* descriptor dpl must = RPL of the return CS selector,
190
       * else #GP(selector) */
191
      if (ss_descriptor.dpl != cs_selector.rpl) {
192
        BX_ERROR(("return_protected: SS.dpl != cs.rpl"));
193
        exception(BX_GP_EXCEPTION, raw_ss_selector & 0xfffc);
194
      }
195
 
196
      /* segment must be present else #SS(selector) */
197
      if (! IS_PRESENT(ss_descriptor)) {
198
        BX_ERROR(("return_protected: ss.present == 0"));
199
        exception(BX_SS_EXCEPTION, raw_ss_selector & 0xfffc);
200
      }
201
    }
202
 
203
    branch_far64(&cs_selector, &cs_descriptor, return_RIP, cs_selector.rpl);
204
 
205
    if ((raw_ss_selector & 0xfffc) != 0) {
206
      // load SS:RSP from stack
207
      // load the SS-cache with SS descriptor
208
      load_ss(&ss_selector, &ss_descriptor, cs_selector.rpl);
209
    }
210
#if BX_SUPPORT_X86_64
211
    else {
212
      // we are in 64-bit mode (checked above)
213
      load_null_selector(&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS], raw_ss_selector);
214
    }
215
 
216
    if (long64_mode())
217
      RSP = return_RSP + pop_bytes;
218
    else
219
#endif
220
    {
221
      if (ss_descriptor.u.segment.d_b)
222
        RSP = (Bit32u)(return_RSP + pop_bytes);
223
      else
224
        SP  = (Bit16u)(return_RSP + pop_bytes);
225
    }
226
 
227
    /* check ES, DS, FS, GS for validity */
228
    validate_seg_regs();
229
  }
230
}

powered by: WebSVN 2.1.0

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