File: segment-mon.c

package info (click to toggle)
plex86 0.0.20011018-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,868 kB
  • ctags: 8,721
  • sloc: ansic: 46,915; cpp: 17,817; xml: 1,283; makefile: 1,130; sh: 451; asm: 360; csh: 18
file content (242 lines) | stat: -rw-r--r-- 8,798 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 *  plex86: run multiple x86 operating systems concurrently
 *  Copyright (C) 1999-2001 Kevin P. Lawton
 *
 *  segment-mon.c:  access memory using virtual (seg:offset) ptr
 *                
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */


#include "plex86.h"
#define IN_MONITOR_SPACE
#include "monitor.h"


// xxx Could add code for REP string ops





  void
copyGuestDescToMon(vm_t *vm, unsigned sreg)
{
// xxx What checks prevent CS: for writes?
// xxx CS writable in RM


// xxx Could optimize reload of seg if same desc as before
// xxx Maybe we should check valid/p bits.
    /* If seg is CS, convert to data descriptor. */

    {
    /* This stuff needs to be done with interrupts off, otherwise
     * interrupts will cause monitor->host->monitor switches, will
     * reload a partially initialized descriptor!
     */
    asm volatile ( "cli\n\t" );
    vm->guest.addr.gdt[4+sreg] = vm->guest_cpu.desc_cache[sreg].desc;
    vm->guest.addr.gdt[4+sreg].dpl = 3;
    if (sreg==SRegCS) {
      /* When the guest CS descriptor cache is loaded, load the FS
       * descriptor with the virtualized descriptor.
       */
      vm->guest.addr.gdt[4+sreg].type = D_DATA | D_WRITE | D_ACCESSED;
      asm volatile ("movl %0, %%fs \n\t"
                    :
                    : "r" (Selector(4+sreg,0,RPL3))
                   );
      }
    else {
      vm->guest.addr.gdt[4+sreg].type =
          vm->guest_cpu.desc_cache[sreg].desc.type;
      }
    asm volatile ( "sti\n\t" );
    }
}


/* NOTE: I make use of the fact that GCC caller-saves certain registers,
 * so we are free to clobber them here.  This eliminates extra push/pop
 * instructions.
 */


/* Fetch routines.  Stack:
 *   offset32
 *   [return address]
 */

asm (
  ".globl __fetchCheckNative      \n\t"
  "__fetchCheckNative:            \n\t"
  "movl  4(%esp), %eax            \n\t" /* Guest offset. */
  ".globl __fetchCheckAttempt     \n\t"
  "__fetchCheckAttempt:           \n\t"
  "fs; movb (%eax), %al           \n\t" /* Read byte using seg:offset. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"
  );


/* Read routines.  Stack:
 *   data pointer
 *   offset32
 *   selector
 *   [return address]
 */

asm (
  ".globl __readByteNative        \n\t"
  "__readByteNative:              \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Ptr to store result. */
  ".globl __readByteAttempt       \n\t"
  "__readByteAttempt:             \n\t"
  "gs; movb (%eax), %al           \n\t" /* Read byte using seg:offset. */
  "movb  %al, (%edx)              \n\t" /* Store byte using ptr. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"

  ".globl __readError             \n\t"
  "__readError:                   \n\t"
  "movl  $0, %eax                 \n\t" /* Return Error. */
  "ret                            \n\t"
  );

asm (
  ".globl __readWordNative        \n\t"
  "__readWordNative:              \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Ptr to store result. */
  ".globl __readWordAttempt       \n\t"
  "__readWordAttempt:             \n\t"
  "gs; movw (%eax), %ax           \n\t" /* Read word using seg:offset. */
  "movw  %ax, (%edx)              \n\t" /* Store word using ptr. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"
  );

asm (
  ".globl __readDWordNative       \n\t"
  "__readDWordNative:             \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Ptr to store result. */
  ".globl __readDWordAttempt      \n\t"
  "__readDWordAttempt:            \n\t"
  "gs; movl (%eax), %eax          \n\t" /* Read dword using seg:offset. */
  "movl  %eax, (%edx)             \n\t" /* Store dword using ptr. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"
  );

/* Read-Modify-Write versions of native read functions. */
asm (
  ".globl __readRMWByteNative     \n\t"
  "__readRMWByteNative:           \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Ptr to store result. */
  ".globl __readRMWByteAttempt    \n\t"
  "__readRMWByteAttempt:          \n\t"
  "gs; orb  $0, (%eax)            \n\t" /* Test writability of data area. */
  "gs; movb (%eax), %al           \n\t" /* Read byte using seg:offset. */
  "movb  %al, (%edx)              \n\t" /* Store byte using ptr. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"
  );

asm (
  ".globl __readRMWWordNative     \n\t"
  "__readRMWWordNative:           \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Ptr to store result. */
  ".globl __readRMWWordAttempt    \n\t"
  "__readRMWWordAttempt:          \n\t"
  "gs; orw  $0, (%eax)            \n\t" /* Test writability of data area. */
  "gs; movw (%eax), %ax           \n\t" /* Read word using seg:offset. */
  "movw  %ax, (%edx)              \n\t" /* Store word using ptr. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"
  );

asm (
  ".globl __readRMWDWordNative    \n\t"
  "__readRMWDWordNative:          \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Ptr to store result. */
  ".globl __readRMWDWordAttempt   \n\t"
  "__readRMWDWordAttempt:         \n\t"
  "gs; orl  $0, (%eax)            \n\t" /* Test writability of data area. */
  "gs; movl (%eax), %eax          \n\t" /* Read dword using seg:offset. */
  "movl  %eax, (%edx)             \n\t" /* Store dword using ptr. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"
  );


/* Write routines. */
asm (
  ".globl __writeByteNative       \n\t"
  "__writeByteNative:             \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Data ptr. */
  "movb  (%edx), %dl              \n\t" /* Get byte using data ptr. */
  ".globl __writeByteAttempt      \n\t"
  "__writeByteAttempt:            \n\t"
  "gs; movb %dl, (%eax)           \n\t" /* Write byte using seg:offset. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"

  ".globl __writeError            \n\t"
  "__writeError:                  \n\t"
  "movl  $0, %eax                 \n\t" /* Return Error. */
  "ret                            \n\t"
  );

asm (
  ".globl __writeWordNative       \n\t"
  "__writeWordNative:             \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Data ptr. */
  "movw  (%edx), %dx              \n\t" /* Get word using data ptr. */
  ".globl __writeWordAttempt      \n\t"
  "__writeWordAttempt:            \n\t"
  "gs; movw %dx, (%eax)           \n\t" /* Write word using seg:offset. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"
  );

asm (
  ".globl __writeDWordNative      \n\t"
  "__writeDWordNative:            \n\t"
  "movl  4(%esp), %gs             \n\t"
  "movl  8(%esp), %eax            \n\t" /* Guest offset. */
  "movl  12(%esp), %edx           \n\t" /* Data ptr. */
  "movl  (%edx), %edx             \n\t" /* Get dword using data ptr. */
  ".globl __writeDWordAttempt     \n\t"
  "__writeDWordAttempt:           \n\t"
  "gs; movl %edx, (%eax)          \n\t" /* Write dword using seg:offset. */
  "movl  $1, %eax                 \n\t" /* Return OK. */
  "ret                            \n\t"
  );