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
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
Copyright (c) 1998-2001 Palm, Inc. or its subsidiaries.
All rights reserved.
This file is part of the Palm OS Emulator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
\* ===================================================================== */
// ---------------------------------------------------------------------------
#pragma mark ===== Includes
// ---------------------------------------------------------------------------
#include "EmCommon.h"
#include "ATraps.h"
#include "Byteswapping.h" // Canonical
#include "EmBankMapped.h" // EmBankMapped::GetEmulatedAddress
#include "EmCPU68K.h" // GetRegisters
#include "EmException.h" // EmExceptionReset
#include "EmPalmOS.h" // EmPalmOS
#include "EmSession.h" // gSession
#include "ErrorHandling.h" // Errors::ReportError
#include "Miscellaneous.h" // StMemoryMapper
#include "EmPalmFunction.h" // GetTrapName
#include "Profiling.h" // StDisableAllProfiling
// ---------------------------------------------------------------------------
#pragma mark ===== Types
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
#pragma mark ===== Functions
// ---------------------------------------------------------------------------
static Bool PrvHandleTrap12 (ExceptionNumber);
// ---------------------------------------------------------------------------
#pragma mark ===== Constants
// ---------------------------------------------------------------------------
const uint16 kOpcode_ROMCall = m68kTrapInstr + sysDispatchTrapNum;
const uint16 kOpcode_ATrapReturn = m68kTrapInstr + kATrapReturnTrapNum;
// ---------------------------------------------------------------------------
#pragma mark ===== Variables
// ---------------------------------------------------------------------------
// ----- Saved variables -----------------------------------------------------
// ----- UnSaved variables ---------------------------------------------------
#pragma mark -
// ===========================================================================
// ATrap
// ===========================================================================
/***********************************************************************
*
* FUNCTION: ATrap::ATrap
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
ATrap::ATrap (void) :
#if (__GNUC__ < 2)
/* Can't call the default constructor because there isn't one defined */
/* on a struct as there is with a class under GCC 2.8.1 */
fNewRegisters (),
#endif
fEmulatedStackMapper (this->GetStackBase (), kStackSize)
{
// Get the registers.
EmAssert (gCPU68K);
gCPU68K->GetRegisters (fOldRegisters);
fNewRegisters = fOldRegisters;
// Make sure the CPU is not stopped. I suppose that we could force the CPU
// to no longer be stopped, but I'd rather that the Palm OS itself woke up
// first before we try making calls into it. Therefore, anything making
// an out-of-the-blue Palm OS call via this class (that is, a call outside
// of the context of a Palm OS function head- or tailpatch) should first
// bring the CPU to a halt by calling EmSession::ExecuteUntilSysCall first.
EmAssert (fNewRegisters.stopped == 0);
// Give ourselves our own private stack. We'll want this in case
// we're in the debugger and the stack pointer is hosed.
m68k_areg (fNewRegisters, 7) = EmBankMapped::GetEmulatedAddress (
this->GetStackBase () + kStackSize - 4);
// Remember this as a stack so that our stack sniffer won't complain.
char* stackBase = this->GetStackBase ();
StackRange range ( EmBankMapped::GetEmulatedAddress (&stackBase[0]),
EmBankMapped::GetEmulatedAddress (&stackBase[kStackSize - 4]));
EmPalmOS::RememberStackRange (range);
}
/***********************************************************************
*
* FUNCTION: ATrap::~ATrap
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
ATrap::~ATrap (void)
{
// Put things back the way they were.
EmPalmOS::ForgetStack (EmBankMapped::GetEmulatedAddress (this->GetStackBase ()));
EmAssert (gCPU68K);
gCPU68K->SetRegisters (fOldRegisters);
// Check to see if anything interesting was registered while we
// were making the Palm OS subroutine call. The "check after end
// of cycle" bit may have gotten cleared when restoring the old
// registers, so set it on the off chance that it was. Doing this
// is harmless if there really aren't any scheduled tasks.
EmAssert (gSession);
gCPU68K->CheckAfterCycle ();
}
/***********************************************************************
*
* FUNCTION: ATrap::Call
*
* DESCRIPTION: Calls the given pseudo-ATrap.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void ATrap::Call (uint16 trapWord)
{
EmAssert (trapWord != sysTrapSysReset);
// Up until now, the registers in "regs" have been left alone. If any
// values were pushed on the stack, the stack position was reflected in
// fNewRegisters. Now's the time to move those values from fNewRegisters
// to regs.
EmAssert (gCPU68K);
gCPU68K->SetRegisters (fNewRegisters);
// Make the call.
this->DoCall(trapWord);
// Remember the resulting register values so that we can report them to
// the user when they call GetD0 and/or GetA0.
gCPU68K->GetRegisters (fNewRegisters);
}
/***********************************************************************
*
* FUNCTION: ATrap::PushByte
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void ATrap::PushByte (uint8 iByte)
{
StDisableAllProfiling stopper;
m68k_areg (fNewRegisters, 7) -= 2;
EmMemPut8 (m68k_areg (fNewRegisters, 7), iByte);
}
/***********************************************************************
*
* FUNCTION: ATrap::PushWord
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void ATrap::PushWord (uint16 iWord)
{
StDisableAllProfiling stopper;
m68k_areg (fNewRegisters, 7) -= 2;
EmMemPut16 (m68k_areg (fNewRegisters, 7), iWord);
}
/***********************************************************************
*
* FUNCTION: ATrap::PushLong
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void ATrap::PushLong (uint32 iLong)
{
StDisableAllProfiling stopper;
m68k_areg (fNewRegisters, 7) -= 4;
EmMemPut32 (m68k_areg (fNewRegisters, 7), iLong);
}
/***********************************************************************
*
* FUNCTION: ATrap::SetNewDReg
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void ATrap::SetNewDReg (int regNum, uint32 value)
{
m68k_dreg (fNewRegisters, regNum) = value;
}
/***********************************************************************
*
* FUNCTION: ATrap::SetNewAReg
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void ATrap::SetNewAReg (int regNum, uint32 value)
{
m68k_areg (fNewRegisters, regNum) = value;
}
/***********************************************************************
*
* FUNCTION: ATrap::GetD0
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
uint32 ATrap::GetD0 (void)
{
return m68k_dreg (fNewRegisters, 0);
}
/***********************************************************************
*
* FUNCTION: ATrap::GetA0
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
uint32 ATrap::GetA0 (void)
{
return m68k_areg (fNewRegisters, 0);
}
/***********************************************************************
*
* FUNCTION: ATrap::DoCall
*
* DESCRIPTION: Calls the pseudo-ATrap. When we return, D0 or A0 should
* hold the result, the parameters will still be on the
* stack with the SP pointing to them, and the PC will be
* restored to what it was before this function was called.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void ATrap::DoCall (uint16 trapWord)
{
// Stop all profiling activities. Stop cycle counting and stop the
// recording of function entries and exits. We want our calls to
// ROM functions to be as transparent as possible.
StDisableAllProfiling stopper;
// Assert that the function we're trying to call is implemented.
//
// Oops...bad test...this doesn't work when we're calling a library.
// Instead, since we now invoke ROM functions by creating a TRAP $F
// sequence, we'll let our TRAP $F handler deal with validating the
// function call (it does that anyway).
// EmAssert (LowMem::GetTrapAddress (trapWord));
// We call the ROM function by dummying up a sequence of 68xxx instructions
// for it. The sequence of instructions is:
//
// TRAP $F
// DC.W <dispatch number>
// TRAP $C
//
// The first two words invoke the function (calling any head- or tailpatches
// along the way). The third word allows the emulator to regain control
// after the function has returned.
//
// Note: this gets a little ugly on little-endian machines. The following
// instructions are stored on the emulator's stack. This memory is mapped
// into the emulated address space in such a fashion that no byteswapping of
// word or long values occurs. Thus, we have to put the data into Big Endian
// format when putting it into the array.
//
// However, opcodes are a special case. They are optimized in the emulator
// for fast access. Opcodes are *always* fetched a word at a time in host-
// endian order. Thus, the opcodes below have to be stored in host-endian
// order. That's why there's no call to Canonical to put them into Big
// Endian order.
uint16 code[] = { kOpcode_ROMCall, trapWord, kOpcode_ATrapReturn };
// Oh, OK, we do have to byteswap the trapWord. Opcodes are fetched with
// EmMemDoGet16, which always gets the value in host byte order. The
// trapWord is fetched with EmMemGet16, which gets values according to the
// rules of the memory bank. For the dummy bank, the defined byte order
// is Big Endian.
Canonical (code[1]);
// Map in the code stub so that the emulation code can access it.
StMemoryMapper mapper (code, sizeof (code));
// Prepare to handle the TRAP 12 exception.
EmAssert (gCPU68K);
gCPU68K->InstallHookException (kException_ATrapReturn, PrvHandleTrap12);
// Point the PC to our code.
emuptr newPC = EmBankMapped::GetEmulatedAddress (code);
m68k_setpc (newPC);
// Execute until the next break.
try
{
EmAssert (gSession);
gSession->ExecuteSubroutine ();
}
catch (EmExceptionReset& e)
{
e.SetTrapWord (trapWord);
// Remove the TRAP 12 exception handler.
EmAssert (gCPU68K);
gCPU68K->RemoveHookException (kException_ATrapReturn, PrvHandleTrap12);
throw;
}
// Remove the TRAP 12 exception handler.
EmAssert (gCPU68K);
gCPU68K->RemoveHookException (kException_ATrapReturn, PrvHandleTrap12);
}
// ---------------------------------------------------------------------------
// ATrap::GetStackBase
// ---------------------------------------------------------------------------
char* ATrap::GetStackBase ()
{
// Ensure that the stack is aligned to a longword address.
uint32 stackBase = (uint32) fStack;
stackBase += 3;
stackBase &= ~3;
return (char*) stackBase;
}
// ---------------------------------------------------------------------------
// PrvHandleTrap12
// ---------------------------------------------------------------------------
Bool PrvHandleTrap12 (ExceptionNumber)
{
EmAssert (gSession);
gSession->ScheduleSuspendSubroutineReturn ();
return true;
}
|