aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ioemu/iodev/cpu.cc
blob: 17a85539ce3c54335aa460394205139aea655e6c (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Main cpu loop for handling I/O requests coming from a virtual machine
 * Copyright © 2004, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA.
 */

#include "bochs.h"
#include <cpu/cpu.h>
#ifdef BX_USE_VMX
#include <sys/ioctl.h>
/* According to POSIX 1003.1-2001 */
#include <sys/select.h>

/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <values.h>
#endif

#define LOG_THIS BX_CPU_THIS_PTR

#ifdef BX_USE_VMX

//the evtchn fd for polling
int evtchn_fd = -1;
//the evtchn port for polling the notification, should be inputed as bochs's parameter
u16 ioreq_port = 0;

void *shared_page = NULL;

//some functions to handle the io req packet

//get the ioreq packets from share mem
ioreq_t* bx_cpu_c::__get_ioreq(void)
{
	ioreq_t *req;
	req = &((vcpu_iodata_t *) shared_page)->vp_ioreq;
	if (req->state == STATE_IOREQ_READY) {
		req->state = STATE_IOREQ_INPROCESS;
	} else {
		BX_INFO(("False I/O requrest ... in-service already: %lx, pvalid: %lx,port: %lx, data: %lx, count: %lx, size: %lx\n", req->state, req->pdata_valid, req->addr, req->u.data, req->count, req->size));
		req = NULL;
	}

	return req;
}

//use poll to get the port notification
//ioreq_vec--out,the 
//retval--the number of ioreq packet
ioreq_t* bx_cpu_c::get_ioreq(void)
{
	ioreq_t *req;
	int rc;
	u16 buf[2];
	rc = read(evtchn_fd, buf, 2);
	if (rc == 2 && buf[0] == ioreq_port){//got only one matched 16bit port index
		// unmask the wanted port again
		write(evtchn_fd, &ioreq_port, 2);

		//get the io packet from shared memory
		return __get_ioreq();
	}

	//read error or read nothing
	return NULL;
}

//send the ioreq to device model
void bx_cpu_c::dispatch_ioreq(ioreq_t *req)
{
	int ret, i;
    int sign;

    sign = (req->df) ? -1 : 1;

	if ((!req->pdata_valid) && (req->dir == IOREQ_WRITE)) {
		if (req->size != 4) {
			// Bochs expects higher bits to be 0
			req->u.data &= (1UL << (8 * req->size))-1;
		}
	}
	if (req->port_mm == 0){//port io
		if(req->dir == IOREQ_READ){//read
			if (!req->pdata_valid)
				req->u.data = BX_INP(req->addr, req->size);
			else {
				unsigned long tmp; 

				for (i = 0; i < req->count; i++) {
					tmp = BX_INP(req->addr, req->size);
					BX_MEM_WRITE_PHYSICAL((Bit32u) req->u.pdata + (sign * i * req->size), 
							       req->size, &tmp);
				}
			}
		} else if(req->dir == IOREQ_WRITE) {
			if (!req->pdata_valid) {
				BX_OUTP(req->addr, (Bit32u) req->u.data, req->size);
			} else {
				for (i = 0; i < req->count; i++) {
					unsigned long tmp;

					BX_MEM_READ_PHYSICAL((Bit32u) req->u.pdata + (sign * i * req->size), req->size, 
							 &tmp);
					BX_OUTP(req->addr, (Bit32u) tmp, req->size);
				}
			}
			
		}
	} else if (req->port_mm == 1){//memory map io
		if (!req->pdata_valid) {
			if(req->dir == IOREQ_READ){//read
				BX_MEM_READ_PHYSICAL(req->addr, req->size, &req->u.data);
			} else if(req->dir == IOREQ_WRITE)//write
				BX_MEM_WRITE_PHYSICAL(req->addr, req->size, &req->u.data);
		} else {
			//handle movs
			unsigned long tmp;
			if (req->dir == IOREQ_READ) {
				//BX_INFO(("<READ>addr:%llx, pdata:%llx, size: %x, count: %x\n", req->addr, req->u.pdata, req->size, req->count));
				for (i = 0; i < req->count; i++) {
					BX_MEM_READ_PHYSICAL(req->addr + (sign * i * req->size), req->size, &tmp);
					BX_MEM_WRITE_PHYSICAL((Bit32u) req->u.pdata + (sign * i * req->size), req->size, &tmp);
				}
			} else if (req->dir == IOREQ_WRITE) {
				//BX_INFO(("<WRITE>addr:%llx, pdata:%llx, size: %x, count: %x\n", req->addr, req->u.pdata, req->size, req->count));
				for (i = 0; i < req->count; i++) {
					BX_MEM_READ_PHYSICAL((Bit32u)req->u.pdata + (sign * i * req->size), req->size, &tmp);
					BX_MEM_WRITE_PHYSICAL(req->addr + (sign * i * req->size), req->size, &tmp);
				}
			}
		}
	}
	req->state = STATE_IORESP_READY;
	send_event = 1;
}

void
bx_cpu_c::handle_ioreq(void)
{
	ioreq_t *req = get_ioreq();
	if (req)
		dispatch_ioreq(req);
}

void
bx_cpu_c::timer_handler(void)
{
	handle_ioreq();
}

#endif

#define rdtscl(low) \
     __asm__ __volatile__("rdtsc" : "=a" (low) : : "edx")

void
bx_cpu_c::cpu_loop(int max_instr_count)
{
	Bit8u vector;
 	fd_set rfds;
	unsigned long stime_usec;
	struct timeval tv;
	int retval;

 	/* Watch stdin (fd 0) to see when it has input. */
	FD_ZERO(&rfds);

	while (1) {
		unsigned long t1, t2;

		/* Wait up to one seconds. */
		tv.tv_sec = 0;
		tv.tv_usec = 100000;
		FD_SET(evtchn_fd, &rfds);

		send_event = 0;
		rdtscl(t1);		
		retval = select(evtchn_fd+1, &rfds, NULL, NULL, &tv);
		rdtscl(t2);
		if (retval == -1) {
			perror("select");
			return;
		}
		//stime_usec = 1000000 * (1 - tv.tv_sec)  - tv.tv_usec;
		if (t2 > t1)
			BX_TICKN((t2 - t1) / 2000);	// should match ips in bochsrc
		else
			BX_TICKN((MAXINT - t1 + t2) / 2000);	// should match ips in bochsrc
		timer_handler();
		if (BX_CPU_INTR) {
#if BX_SUPPORT_APIC
			if (BX_CPU_THIS_PTR local_apic.INTR)
				vector = BX_CPU_THIS_PTR local_apic.acknowledge_int ();
			else
				vector = DEV_pic_iac(); // may set INTR with next interrupt
#else
			// if no local APIC, always acknowledge the PIC.
			vector = DEV_pic_iac(); // may set INTR with next interrupt
#endif
			interrupt(vector);
		}
		/* we check DMA after interrupt check*/
		while(BX_HRQ){
			DEV_dma_raise_hlda();
		}

		if (send_event) {
			int ret;
			ret = xc_evtchn_send(xc_handle, ioreq_port);
			if (ret == -1) {
				BX_ERROR(("evtchn_send failed on port: %d\n", ioreq_port));
			}
		}
	}
}

static __inline__ void set_bit(long nr, volatile void *addr)
{
	__asm__ __volatile__( "lock ; "
		"btsl %1,%0"
		:"=m" ((*(volatile long *)addr))
		:"Ir" (nr));

	return;
}

void
bx_cpu_c::interrupt(Bit8u vector)
{
	unsigned long *intr, tscl;
	int ret;

	// Send a message on the event channel. Add the vector to the shared mem
	// page.

	rdtscl(tscl);
	BX_INFO(("%lx: injecting vector: %x\n", tscl, vector));
	intr = &(((vcpu_iodata_t *) shared_page)->vp_intr[0]);
	set_bit(vector, intr);
	
	send_event = 1;
}

void
bx_cpu_c::init(bx_mem_c*)
{
#ifdef BX_USE_VMX
	if (evtchn_fd != -1)//the evtchn has been opened by another cpu object
		return;

	//use nonblock reading not polling, may change in future.
	evtchn_fd = open("/dev/xen/evtchn", O_RDWR|O_NONBLOCK); 
	if (evtchn_fd == -1) {
		perror("open");
		return;
	}

	BX_INFO(("listening to port: %d\n", ioreq_port));
	/*unmask the wanted port -- bind*/
	if (ioctl(evtchn_fd, ('E'<<8)|2, ioreq_port) == -1) {
		perror("ioctl");
		return;
	}

#if 0	
	//register the reading evtchn function as timer
	bx_pc_system.register_timer(this, timer_handler, 1000,//1000 us, may change
			1,//continuous timer
			1,//active
			"cpu reading evtchn handler");
#endif

#endif
}

void
bx_cpu_c::reset(unsigned)
{
}

void
bx_cpu_c::atexit()
{
}

void
bx_cpu_c::set_INTR(unsigned value)
{
	BX_CPU_THIS_PTR INTR = value;
}

void
bx_cpu_c::pagingA20Changed()
{
}

bx_cpu_c::bx_cpu_c()
{
}

bx_cpu_c::~bx_cpu_c()
{
}