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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
////////////////////////////////////////////////////////////////////////
// $Id: virt_timer.cc,v 1.31 2006/05/29 22:33:38 sshwarts Exp $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2002 MandrakeSoft S.A.
//
// MandrakeSoft S.A.
// 43, rue d'Aboukir
// 75002 Paris - France
// http://www.linux-mandrake.com/
// http://www.mandrakesoft.com/
//
// 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
/////////////////////////////////////////////////////////////////////////
//
//Realtime Algorithm (with gettimeofday)
// HAVE:
// Real number of usec.
// Emulated number of usec.
// WANT:
// Number of ticks to use.
// Number of emulated usec to wait until next try.
//
// ticks=number of ticks needed to match total real usec.
// if(desired ticks > max ticks for elapsed real time)
// ticks = max ticks for elapsed real time.
// if(desired ticks > max ticks for elapsed emulated usec)
// ticks = max ticks for emulated usec.
// next wait ticks = number of ticks until next event.
// next wait real usec = (current ticks + next wait ticks) * usec per ticks
// next wait emulated usec = next wait real usec * emulated usec / real usec
// if(next wait emulated usec < minimum emulated usec for next wait ticks)
// next wait emulated usec = minimum emulated usec for next wait ticks.
// if(next wait emulated usec > max emulated usec wait)
// next wait emulated usec = max emulated usec wait.
//
// How to calculate elapsed real time:
// store an unused time value whenever no ticks are used in a given time.
// add this to the current elapsed time.
// How to calculate elapsed emulated time:
// same as above.
// Above can be done by not updating last_usec and last_sec.
//
// How to calculate emulated usec/real usec:
// Each time there are actual ticks:
// Alpha_product(old emulated usec, emulated usec);
// Alpha_product(old real usec, real usec);
// Divide resulting values.
//
/////////////////////////////////////////////////////////////////////////
#include "bochs.h"
#include "virt_timer.h"
#define BX_USE_VIRTUAL_TIMERS 1
#define BX_VIRTUAL_TIMERS_REALTIME 1
//Important constant #defines:
#define USEC_PER_SECOND (1000000)
// define a macro to convert floating point numbers into 64-bit integers.
// In MSVC++ you can convert a 64-bit float into a 64-bit signed integer,
// but it will not convert a 64-bit float into a 64-bit unsigned integer.
// This macro works around that.
#define F2I(x) ((Bit64u)(Bit64s) (x))
#define I2F(x) ((double)(Bit64s) (x))
//CONFIGURATION #defines:
//MAINLINE Configuration (For realtime PIT):
//How much faster than real time we can go:
#define MAX_MULT (1.25)
//Minimum number of emulated useconds per second.
// Now calculated using BX_MIN_IPS, the minimum number of
// instructions per second.
#define MIN_USEC_PER_SECOND (((((Bit64u)USEC_PER_SECOND)*((Bit64u)BX_MIN_IPS))/((Bit64u)(SIM->get_param_num(BXPN_IPS)->get())))+(Bit64u)1)
//DEBUG configuration:
//Debug with printf options.
#define DEBUG_REALTIME_WITH_PRINTF 0
//Use to test execution at multiples of real time.
#define TIME_DIVIDER (1)
#define TIME_MULTIPLIER (1)
#define TIME_HEADSTART (0)
#define GET_VIRT_REALTIME64_USEC() (((bx_get_realtime64_usec()*(Bit64u)TIME_MULTIPLIER/(Bit64u)TIME_DIVIDER)))
//Set up Logging.
#define LOG_THIS bx_virt_timer.
//A single instance.
bx_virt_timer_c bx_virt_timer;
//Generic MAX and MIN Functions
#define BX_MAX(a,b) ( ((a)>(b))?(a):(b) )
#define BX_MIN(a,b) ( ((a)>(b))?(b):(a) )
//USEC_ALPHA is multiplier for the past.
//USEC_ALPHA_B is 1-USEC_ALPHA, or multiplier for the present.
#define USEC_ALPHA ((double)(.8))
#define USEC_ALPHA_B ((double)(((double)1)-USEC_ALPHA))
#define USEC_ALPHA2 ((double)(.5))
#define USEC_ALPHA2_B ((double)(((double)1)-USEC_ALPHA2))
#define ALPHA_LOWER(old,new) ((Bit64u)((old<new)?((USEC_ALPHA*(I2F(old)))+(USEC_ALPHA_B*(I2F(new)))):((USEC_ALPHA2*(I2F(old)))+(USEC_ALPHA2_B*(I2F(new))))))
//Conversion between emulated useconds and optionally realtime ticks.
#define TICKS_TO_USEC(a) ( ((a)*usec_per_second)/ticks_per_second )
#define USEC_TO_TICKS(a) ( ((a)*ticks_per_second)/usec_per_second )
bx_virt_timer_c::bx_virt_timer_c()
{
put("VTIMER");
settype(VTIMERLOG);
numTimers = 0;
current_timers_time = 0;
timers_next_event_time = BX_MAX_VIRTUAL_TIME;
last_sequential_time = 0;
in_timer_handler = 0;
virtual_next_event_time = BX_MAX_VIRTUAL_TIME;
current_virtual_time = 0;
use_virtual_timers = BX_USE_VIRTUAL_TIMERS;
init_done = 0;
}
const Bit64u bx_virt_timer_c::NullTimerInterval = BX_MAX_VIRTUAL_TIME;
void bx_virt_timer_c::nullTimer(void* this_ptr)
{
UNUSED(this_ptr);
}
void bx_virt_timer_c::periodic(Bit64u time_passed)
{
//Assert that we haven't skipped any events.
BX_ASSERT (time_passed <= timers_next_event_time);
BX_ASSERT(!in_timer_handler);
//Update time variables.
timers_next_event_time -= time_passed;
current_timers_time += time_passed;
//If no events are occurring, just pass the time and we're done.
if(time_passed < timers_next_event_time) return;
//Starting timer handler calls.
in_timer_handler = 1;
//Otherwise, cause any events to occur that should.
unsigned i;
for(i=0;i<numTimers;i++) {
if(timer[i].inUse && timer[i].active) {
//Assert that we haven't skipped any timers.
BX_ASSERT(current_timers_time <= timer[i].timeToFire);
if(timer[i].timeToFire == current_timers_time) {
if(timer[i].continuous) {
timer[i].timeToFire+=timer[i].period;
} else {
timer[i].active = 0;
}
//This function MUST return, or the timer mechanism
// will be broken.
timer[i].funct(timer[i].this_ptr);
}
}
}
//Finished timer handler calls.
in_timer_handler = 0;
//Use a second FOR loop so that a timer function call can
// change the behavior of another timer.
//timers_next_event_time normally contains a cycle count, not a cycle time.
// here we use it as a temporary variable that IS a cycle time,
// but then convert it back to a cycle count afterwards.
timers_next_event_time = current_timers_time + BX_MAX_VIRTUAL_TIME;
for(i=0;i<numTimers;i++) {
if(timer[i].inUse && timer[i].active && ((timer[i].timeToFire)<timers_next_event_time)) {
timers_next_event_time = timer[i].timeToFire;
}
}
timers_next_event_time-=current_timers_time;
next_event_time_update();
//FIXME
}
//Get the current virtual time.
// This may return the same value on subsequent calls.
Bit64u bx_virt_timer_c::time_usec(void)
{
if(!use_virtual_timers) {
return bx_pc_system.time_usec();
}
//Update the time here only if we're not in a timer handler.
//If we're in a timer handler we're up-to-date, and otherwise
// this prevents call stack loops.
if(!in_timer_handler) {
timer_handler();
}
return current_timers_time;
}
//Get the current virtual time.
// This will return a monotonically increasing value.
// MUST NOT be called from within a timer interrupt.
Bit64u bx_virt_timer_c::time_usec_sequential(void)
{
if(!use_virtual_timers) {
return bx_pc_system.time_usec_sequential();
}
//Can't prevent call stack loops here, so this
// MUST NOT be called from within a timer handler.
BX_ASSERT(timers_next_event_time>0);
BX_ASSERT(!in_timer_handler);
if(last_sequential_time >= current_timers_time) {
periodic(1);
last_sequential_time = current_timers_time;
}
return current_timers_time;
}
//Register a timer handler to go off after a given interval.
//Register a timer handler to go off with a periodic interval.
int bx_virt_timer_c::register_timer(void *this_ptr, bx_timer_handler_t handler,
Bit32u useconds,
bx_bool continuous, bx_bool active,
const char *id)
{
if(!use_virtual_timers) {
return bx_pc_system.register_timer(this_ptr, handler, useconds,
continuous, active, id);
}
//We don't like starting with a zero period timer.
BX_ASSERT((!active) || (useconds>0));
//Search for an unused timer.
unsigned int i;
for (i=0; i < numTimers; i++) {
if (timer[i].inUse == 0 || i==numTimers)
break;
}
// If we didn't find a free slot, increment the bound, numTimers.
if (i==numTimers)
numTimers++; // One new timer installed.
BX_ASSERT(numTimers<BX_MAX_VIRTUAL_TIMERS);
timer[i].inUse = 1;
timer[i].period = useconds;
timer[i].timeToFire = current_timers_time + (Bit64u)useconds;
timer[i].active = active;
timer[i].continuous = continuous;
timer[i].funct = handler;
timer[i].this_ptr = this_ptr;
strncpy(timer[i].id, id, BxMaxTimerIDLen);
timer[i].id[BxMaxTimerIDLen-1]=0; //I like null terminated strings.
if(useconds < timers_next_event_time) {
timers_next_event_time = useconds;
next_event_time_update();
//FIXME
}
return i;
}
//unregister a previously registered timer.
bx_bool bx_virt_timer_c::unregisterTimer(unsigned timerID)
{
if(!use_virtual_timers) {
return bx_pc_system.unregisterTimer(timerID);
}
BX_ASSERT(timerID < BX_MAX_VIRTUAL_TIMERS);
if (timer[timerID].active) {
BX_PANIC(("unregisterTimer: timer '%s' is still active!", timer[timerID].id));
return(0); // Fail.
}
//No need to prevent doing this to unused timers.
timer[timerID].inUse = 0;
if (timerID == (numTimers-1)) numTimers--;
return(1);
}
void bx_virt_timer_c::start_timers(void)
{
if(!use_virtual_timers) {
bx_pc_system.start_timers();
return;
}
//FIXME
}
//activate a deactivated but registered timer.
void bx_virt_timer_c::activate_timer(unsigned timer_index, Bit32u useconds,
bx_bool continuous)
{
if(!use_virtual_timers) {
bx_pc_system.activate_timer(timer_index, useconds, continuous);
return;
}
BX_ASSERT(timer_index < BX_MAX_VIRTUAL_TIMERS);
BX_ASSERT(timer[timer_index].inUse);
BX_ASSERT(useconds>0);
timer[timer_index].period=useconds;
timer[timer_index].timeToFire = current_timers_time + (Bit64u)useconds;
timer[timer_index].active=1;
timer[timer_index].continuous=continuous;
if(useconds < timers_next_event_time) {
timers_next_event_time = useconds;
next_event_time_update();
//FIXME
}
}
//deactivate (but don't unregister) a currently registered timer.
void bx_virt_timer_c::deactivate_timer(unsigned timer_index)
{
if(!use_virtual_timers) {
bx_pc_system.deactivate_timer(timer_index);
return;
}
BX_ASSERT(timer_index < BX_MAX_VIRTUAL_TIMERS);
//No need to prevent doing this to unused/inactive timers.
timer[timer_index].active = 0;
}
void bx_virt_timer_c::advance_virtual_time(Bit64u time_passed)
{
BX_ASSERT(time_passed <= virtual_next_event_time);
current_virtual_time += time_passed;
virtual_next_event_time -= time_passed;
if(current_virtual_time > current_timers_time) {
periodic(current_virtual_time - current_timers_time);
}
}
//Called when next_event_time changes.
void bx_virt_timer_c::next_event_time_update(void)
{
virtual_next_event_time = timers_next_event_time + current_timers_time - current_virtual_time;
if(init_done) {
bx_pc_system.deactivate_timer(system_timer_id);
BX_ASSERT(virtual_next_event_time);
bx_pc_system.activate_timer(system_timer_id,
(Bit32u)BX_MIN(0x7FFFFFFF,BX_MAX(1,TICKS_TO_USEC(virtual_next_event_time))),
0);
}
}
void bx_virt_timer_c::init(void)
{
if ( (SIM->get_param_enum(BXPN_CLOCK_SYNC)->get()!=BX_CLOCK_SYNC_REALTIME)
&& (SIM->get_param_enum(BXPN_CLOCK_SYNC)->get()!=BX_CLOCK_SYNC_BOTH) )
virtual_timers_realtime = 0;
else
virtual_timers_realtime = 1;
if (virtual_timers_realtime) {
BX_INFO(("using 'realtime pit' synchronization method"));
}
register_timer(this, nullTimer, (Bit32u)NullTimerInterval, 1, 1, "Null Timer");
system_timer_id = bx_pc_system.register_timer(this, pc_system_timer_handler,
(Bit32u)virtual_next_event_time, 0, 1, "Virtual Timer");
//Real time variables:
#if BX_HAVE_REALTIME_USEC
last_real_time=GET_VIRT_REALTIME64_USEC()+(Bit64u)TIME_HEADSTART*(Bit64u)USEC_PER_SECOND;
#endif
total_real_usec=0;
last_realtime_delta=0;
//System time variables:
last_usec = 0;
usec_per_second = USEC_PER_SECOND;
stored_delta=0;
last_system_usec=0;
em_last_realtime=0;
//Virtual timer variables:
total_ticks=0;
last_realtime_ticks=0;
ticks_per_second = USEC_PER_SECOND;
init_done = 1;
}
#if BX_SUPPORT_SAVE_RESTORE
void bx_virt_timer_c::register_state(void)
{
bx_list_c *list = new bx_list_c(SIM->get_sr_root(), "virt_timer", "Virtual Timer State", 17);
bx_list_c *vtimers = new bx_list_c(list, "timer", numTimers);
for (unsigned i = 0; i < numTimers; i++) {
char name[4];
sprintf(name, "%d", i);
bx_list_c *bxtimer = new bx_list_c(vtimers, name, 5);
BXRS_PARAM_BOOL(bxtimer, inUse, timer[i].inUse);
BXRS_DEC_PARAM_FIELD(bxtimer, period, timer[i].period);
BXRS_DEC_PARAM_FIELD(bxtimer, timeToFire, timer[i].timeToFire);
BXRS_PARAM_BOOL(bxtimer, active, timer[i].active);
BXRS_PARAM_BOOL(bxtimer, continuous, timer[i].continuous);
}
BXRS_DEC_PARAM_SIMPLE(list, current_timers_time);
BXRS_DEC_PARAM_SIMPLE(list, timers_next_event_time);
BXRS_DEC_PARAM_SIMPLE(list, last_sequential_time);
BXRS_DEC_PARAM_SIMPLE(list, virtual_next_event_time);
BXRS_DEC_PARAM_SIMPLE(list, current_virtual_time);
BXRS_DEC_PARAM_SIMPLE(list, last_real_time);
BXRS_DEC_PARAM_SIMPLE(list, total_real_usec);
BXRS_DEC_PARAM_SIMPLE(list, last_realtime_delta);
BXRS_DEC_PARAM_SIMPLE(list, last_usec);
BXRS_DEC_PARAM_SIMPLE(list, usec_per_second);
BXRS_DEC_PARAM_SIMPLE(list, stored_delta);
BXRS_DEC_PARAM_SIMPLE(list, last_system_usec);
BXRS_DEC_PARAM_SIMPLE(list, em_last_realtime);
BXRS_DEC_PARAM_SIMPLE(list, total_ticks);
BXRS_DEC_PARAM_SIMPLE(list, last_realtime_ticks);
BXRS_DEC_PARAM_SIMPLE(list, ticks_per_second);
}
#endif
void bx_virt_timer_c::timer_handler(void)
{
if(!virtual_timers_realtime) {
Bit64u temp_final_time = bx_pc_system.time_usec();
temp_final_time-=current_virtual_time;
while(temp_final_time) {
if((temp_final_time)>(virtual_next_event_time)) {
temp_final_time-=virtual_next_event_time;
advance_virtual_time(virtual_next_event_time);
} else {
advance_virtual_time(temp_final_time);
temp_final_time-=temp_final_time;
}
}
bx_pc_system.activate_timer(system_timer_id,
(Bit32u)BX_MIN(0x7FFFFFFF,(virtual_next_event_time>2)?(virtual_next_event_time-2):1),
0);
return;
}
Bit64u usec_delta = bx_pc_system.time_usec()-last_usec;
if (usec_delta) {
#if BX_HAVE_REALTIME_USEC
Bit64u ticks_delta = 0;
Bit64u real_time_delta = GET_VIRT_REALTIME64_USEC() - last_real_time;
Bit64u real_time_total = real_time_delta + total_real_usec;
Bit64u system_time_delta = (Bit64u)usec_delta + (Bit64u)stored_delta;
if(real_time_delta) {
last_realtime_delta = real_time_delta;
last_realtime_ticks = total_ticks;
}
ticks_per_second = USEC_PER_SECOND;
//Start out with the number of ticks we would like
// to have to line up with real time.
ticks_delta = real_time_total - total_ticks;
if(real_time_total < total_ticks) {
//This slows us down if we're already ahead.
// probably only an issue on startup, but it solves some problems.
ticks_delta = 0;
}
if(ticks_delta + total_ticks - last_realtime_ticks > (F2I(MAX_MULT * I2F(last_realtime_delta)))) {
//This keeps us from going too fast in relation to real time.
#if 0
ticks_delta = (F2I(MAX_MULT * I2F(last_realtime_delta))) + last_realtime_ticks - total_ticks;
#endif
ticks_per_second = F2I(MAX_MULT * I2F(USEC_PER_SECOND));
}
if(ticks_delta > system_time_delta * USEC_PER_SECOND / MIN_USEC_PER_SECOND) {
//This keeps us from having too few instructions between ticks.
ticks_delta = system_time_delta * USEC_PER_SECOND / MIN_USEC_PER_SECOND;
}
if(ticks_delta > virtual_next_event_time) {
//This keeps us from missing ticks.
ticks_delta = virtual_next_event_time;
}
if(ticks_delta) {
# if DEBUG_REALTIME_WITH_PRINTF
//Every second print some info.
if(((last_real_time + real_time_delta) / USEC_PER_SECOND) > (last_real_time / USEC_PER_SECOND)) {
Bit64u temp1, temp2, temp3, temp4;
temp1 = (Bit64u) total_real_usec;
temp2 = (total_real_usec);
temp3 = (Bit64u)total_ticks;
temp4 = (Bit64u)((total_real_usec) - total_ticks);
printf("useconds: %llu, ",temp1);
printf("expect ticks: %llu, ",temp2);
printf("ticks: %llu, ",temp3);
printf("diff: %llu\n",temp4);
}
# endif
last_real_time += real_time_delta;
total_real_usec += real_time_delta;
last_system_usec += system_time_delta;
stored_delta = 0;
total_ticks += ticks_delta;
} else {
stored_delta = system_time_delta;
}
Bit64u a,b;
a=(usec_per_second);
if(real_time_delta) {
//FIXME
Bit64u em_realtime_delta = last_system_usec + stored_delta - em_last_realtime;
b=((Bit64u)USEC_PER_SECOND * em_realtime_delta / real_time_delta);
em_last_realtime = last_system_usec + stored_delta;
} else {
b=a;
}
usec_per_second = ALPHA_LOWER(a,b);
#else
BX_ASSERT(0);
#endif
#if BX_HAVE_REALTIME_USEC
advance_virtual_time(ticks_delta);
#endif
}
last_usec=last_usec + usec_delta;
bx_pc_system.deactivate_timer(system_timer_id);
BX_ASSERT(virtual_next_event_time);
bx_pc_system.activate_timer(system_timer_id,
(Bit32u)BX_MIN(0x7FFFFFFF,BX_MAX(1,TICKS_TO_USEC(virtual_next_event_time))),
0);
}
void bx_virt_timer_c::pc_system_timer_handler(void* this_ptr)
{
((bx_virt_timer_c *)this_ptr)->timer_handler();
}
|