semihosting.cc revision 12533
1/* 2 * Copyright (c) 2018 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are 16 * met: redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer; 18 * redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution; 21 * neither the name of the copyright holders nor the names of its 22 * contributors may be used to endorse or promote products derived from 23 * this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * Authors: Andreas Sandberg 38 */ 39 40#include "arch/arm/semihosting.hh" 41 42#include <cstdio> 43 44#include "arch/arm/utility.hh" 45#include "base/logging.hh" 46#include "base/time.hh" 47#include "debug/Semihosting.hh" 48#include "dev/serial/serial.hh" 49#include "mem/physical.hh" 50#include "mem/port_proxy.hh" 51#include "params/ArmSemihosting.hh" 52#include "sim/byteswap.hh" 53#include "sim/sim_exit.hh" 54#include "sim/system.hh" 55 56const std::map<uint32_t, ArmSemihosting::SemiCall> ArmSemihosting::calls{ 57 { 0x01, { "SYS_OPEN", &ArmSemihosting::callOpen, 3, 3 } }, 58 { 0x02, { "SYS_CLOSE", &ArmSemihosting::callClose, 1, 1 } }, 59 60 // Write(C|0) are special since we want to read the character 61 // manually. We therefore declare them as having 0 params. 62 { 0x03, { "SYS_WRITEC", &ArmSemihosting::callWriteC, 0, 0 } }, 63 { 0x04, { "SYS_WRITE0", &ArmSemihosting::callWrite0, 1, 1 } }, 64 65 { 0x05, { "SYS_WRITE", &ArmSemihosting::callWrite, 3, 3 } }, 66 { 0x06, { "SYS_READ", &ArmSemihosting::callRead, 3, 3 } }, 67 { 0x07, { "SYS_READC", &ArmSemihosting::callReadC, 0, 0 } }, 68 { 0x08, { "SYS_ISERROR", &ArmSemihosting::callIsError, 1, 1 } }, 69 { 0x09, { "SYS_ISTTY", &ArmSemihosting::callIsTTY, 1, 1 } }, 70 { 0x0A, { "SYS_SEEK", &ArmSemihosting::callSeek, 2, 2 } }, 71 { 0x0C, { "SYS_FLEN", &ArmSemihosting::callFLen, 1, 1 } }, 72 { 0x0D, { "SYS_TMPNAM", &ArmSemihosting::callTmpNam, 3, 3 } }, 73 { 0x0E, { "SYS_REMOVE", &ArmSemihosting::callRemove, 2, 2} }, 74 { 0x0F, { "SYS_RENAME", &ArmSemihosting::callRename, 4, 4} }, 75 { 0x10, { "SYS_CLOCK", &ArmSemihosting::callClock, 0, 0} }, 76 { 0x11, { "SYS_TIME", &ArmSemihosting::callTime, 0, 0} }, 77 { 0x12, { "SYS_SYSTEM", &ArmSemihosting::callSystem, 2, 2} }, 78 { 0x13, { "SYS_ERRNO", &ArmSemihosting::callErrno, 0, 0 } }, 79 { 0x15, { "SYS_GET_CMDLINE", &ArmSemihosting::callGetCmdLine, 1, 1} }, 80 { 0x16, { "SYS_HEAPINFO", &ArmSemihosting::callHeapInfo, 1, 1} }, 81 82 // Exit is special and requires custom handling in aarch32. 83 { 0x18, { "SYS_EXIT", &ArmSemihosting::callExit, 0, 2 } }, 84 { 0x20, { "SYS_EXIT_EXTENDED", &ArmSemihosting::callExitExtended, 2, 2 } }, 85 86 { 0x30, { "SYS_ELAPSED", &ArmSemihosting::callElapsed, 0, 0 } }, 87 { 0x31, { "SYS_TICKFREQ", &ArmSemihosting::callTickFreq, 0, 0 } }, 88}; 89 90const std::vector<const char *> ArmSemihosting::fmodes{ 91 "r", "rb", "r+", "r+b", 92 "w", "wb", "w+", "w+b", 93 "a", "ab", "a+", "a+b", 94}; 95 96const std::map<uint64_t, const char *> ArmSemihosting::exitCodes{ 97 { 0x20000, "semi:ADP_Stopped_BranchThroughZero" }, 98 { 0x20001, "semi:ADP_Stopped_UndefinedInstr" }, 99 { 0x20002, "semi:ADP_Stopped_SoftwareInterrupt" }, 100 { 0x20003, "semi:ADP_Stopped_PrefetchAbort" }, 101 { 0x20004, "semi:ADP_Stopped_DataAbort" }, 102 { 0x20005, "semi:ADP_Stopped_AddressException" }, 103 { 0x20006, "semi:ADP_Stopped_IRQ" }, 104 { 0x20007, "semi:ADP_Stopped_FIQ" }, 105 106 { 0x20020, "semi:ADP_Stopped_BreakPoint" }, 107 { 0x20021, "semi:ADP_Stopped_WatchPoint" }, 108 { 0x20022, "semi:ADP_Stopped_StepComplete" }, 109 { 0x20023, "semi:ADP_Stopped_RunTimeErrorUnknown" }, 110 { 0x20024, "semi:ADP_Stopped_InternalError" }, 111 { 0x20025, "semi:ADP_Stopped_UserInterruption" }, 112 { 0x20026, "semi:ADP_Stopped_ApplicationExit" }, 113 { 0x20027, "semi:ADP_Stopped_StackOverflow" }, 114 { 0x20028, "semi:ADP_Stopped_DivisionByZero" }, 115 { 0x20029, "semi:ADP_Stopped_DivisionByZero" }, 116}; 117 118 119const std::vector<uint8_t> ArmSemihosting::features{ 120 0x53, 0x48, 0x46, 0x42, // Magic 121 0x3, // EXT_EXIT_EXTENDED, EXT_STDOUT_STDERR 122}; 123 124ArmSemihosting::ArmSemihosting(const ArmSemihostingParams *p) 125 : SimObject(p), 126 cmdLine(p->cmd_line), 127 memReserve(p->mem_reserve), 128 stackSize(p->stack_size), 129 timeBase([p]{ struct tm t = p->time; return mkutctime(&t); }()), 130 tickShift(calcTickShift()), 131 semiErrno(0) 132{ 133 // Create an empty place-holder file for position 0 as semi-hosting 134 // calls typically expect non-zero file handles. 135 files.push_back(nullptr); 136 137 if (tickShift > 0) 138 inform("Semihosting: Shifting elapsed ticks by %i bits.", 139 tickShift); 140} 141 142uint64_t 143ArmSemihosting::call64(ThreadContext *tc, uint32_t op, uint64_t param) 144{ 145 const SemiCall *call = getCall(op, true); 146 if (!call) { 147 warn("Unknown aarch64 semihosting call: op = 0x%x, param = 0x%x", 148 op, param); 149 150 return (uint64_t)-1; 151 } else if (!call->implemented64()) { 152 warn("Unimplemented aarch64 semihosting call: " 153 "%s (op = 0x%x, param = 0x%x)", 154 call->name, op, param); 155 156 return (uint64_t)-1; 157 } 158 159 std::vector<uint64_t> argv(call->argc64 + 1); 160 PortProxy &proxy = physProxy(tc); 161 ByteOrder endian = ArmISA::byteOrder(tc); 162 163 DPRINTF(Semihosting, "Semihosting call64: %s(0x%x)\n", call->name, param); 164 argv[0] = param; 165 for (int i = 0; i < call->argc64; ++i) { 166 argv[i + 1] = proxy.readGtoH<uint64_t>(param + i * 8, endian); 167 DPRINTF(Semihosting, "\t: 0x%x\n", argv[i + 1]); 168 } 169 170 auto ret_errno = (this->*call->call)(tc, true, argv); 171 semiErrno = ret_errno.second; 172 DPRINTF(Semihosting, "\t ->: 0x%x, %i\n", 173 ret_errno.first, ret_errno.second); 174 return ret_errno.first; 175} 176 177uint32_t 178ArmSemihosting::call32(ThreadContext *tc, uint32_t op, uint32_t param) 179{ 180 const SemiCall *call = getCall(op, false); 181 if (!call) { 182 warn("Unknown aarch32 semihosting call: op = 0x%x, param = 0x%x", 183 op, param); 184 185 return (uint32_t)-1; 186 } else if (!call->implemented32()) { 187 warn("Unimplemented aarch32 semihosting call: " 188 "%s (op = 0x%x, param = 0x%x)", 189 call->name, op, param); 190 191 return (uint32_t)-1; 192 } 193 194 std::vector<uint64_t> argv(call->argc32 + 1); 195 PortProxy &proxy = physProxy(tc); 196 ByteOrder endian = ArmISA::byteOrder(tc); 197 198 DPRINTF(Semihosting, "Semihosting call32: %s(0x%x)\n", call->name, param); 199 argv[0] = param; 200 for (int i = 0; i < call->argc32; ++i) { 201 argv[i + 1] = proxy.readGtoH<uint32_t>(param + i * 4, endian); 202 DPRINTF(Semihosting, "\t: 0x%x\n", argv[i + 1]); 203 } 204 205 auto ret_errno = (this->*call->call)(tc, false, argv); 206 semiErrno = ret_errno.second; 207 DPRINTF(Semihosting, "\t ->: 0x%x, %i\n", 208 ret_errno.first, ret_errno.second); 209 return ret_errno.first; 210} 211 212void 213ArmSemihosting::serialize(CheckpointOut &cp) const 214{ 215 SERIALIZE_SCALAR(semiErrno); 216 217 paramOut(cp, "num_files", files.size()); 218 for (int i = 0; i < files.size(); i++) { 219 // File closed? 220 if (!files[i]) 221 continue; 222 223 files[i]->serializeSection(cp, csprintf("file%i", i)); 224 } 225} 226 227void 228ArmSemihosting::unserialize(CheckpointIn &cp) 229{ 230 UNSERIALIZE_SCALAR(semiErrno); 231 232 size_t num_files; 233 paramIn(cp, "num_files", num_files); 234 files.resize(num_files); 235 for (int i = 0; i < num_files; i++) 236 files[i] = FileBase::create(*this, cp, csprintf("file%i", i)); 237} 238 239PortProxy & 240ArmSemihosting::physProxy(ThreadContext *tc) 241{ 242 if (ArmISA::inSecureState(tc)) { 243 if (!physProxyS) { 244 System *sys = tc->getSystemPtr(); 245 physProxyS.reset(new SecurePortProxy( 246 sys->getSystemPort(), 247 sys->cacheLineSize())); 248 } 249 return *physProxyS; 250 } else { 251 return tc->getPhysProxy(); 252 } 253} 254 255 256std::string 257ArmSemihosting::readString(ThreadContext *tc, Addr ptr, size_t len) 258{ 259 std::vector<char> buf(len + 1); 260 261 buf[len] = '\0'; 262 physProxy(tc).readBlob(ptr, (uint8_t *)buf.data(), len); 263 264 return std::string(buf.data()); 265} 266 267ArmSemihosting::RetErrno 268ArmSemihosting::callOpen(ThreadContext *tc, bool aarch64, 269 std::vector<uint64_t> &argv) 270{ 271 const Addr name_base = argv[1]; 272 const char *mode = argv[2] < fmodes.size() ? fmodes[argv[2]] : nullptr; 273 const Addr name_size = argv[3]; 274 275 DPRINTF(Semihosting, "Semihosting SYS_OPEN(0x%x, %i[%s], %i)\n", 276 name_base, argv[2], mode ? mode : "-", name_size); 277 if (!mode || !name_base) 278 return retError(EINVAL); 279 280 std::string fname = readString(tc, name_base, name_size); 281 282 std::unique_ptr<ArmSemihosting::FileBase> file = 283 FileBase::create(*this, fname, mode); 284 int64_t ret = file->open(); 285 DPRINTF(Semihosting, "Semihosting SYS_OPEN(\"%s\", %i[%s]): %i\n", 286 fname, argv[2], mode, ret); 287 if (ret < 0) { 288 return retError(-ret); 289 } else { 290 files.push_back(std::move(file)); 291 return retOK(files.size() - 1); 292 } 293} 294 295ArmSemihosting::RetErrno 296ArmSemihosting::callClose(ThreadContext *tc, bool aarch64, 297 std::vector<uint64_t> &argv) 298{ 299 if (argv[1] > files.size()) { 300 DPRINTF(Semihosting, "Semihosting SYS_CLOSE(%i): Illegal file\n"); 301 return retError(EBADF); 302 } 303 304 std::unique_ptr<FileBase> &file = files[argv[1]]; 305 int64_t error = file->close(); 306 DPRINTF(Semihosting, "Semihosting SYS_CLOSE(%i[%s]): %i\n", 307 argv[1], file->fileName(), error); 308 if (error < 0) { 309 return retError(-error); 310 } else { 311 // Zap the pointer and free the entry in the file table as 312 // well. 313 files[argv[1]].reset(); 314 return retOK(0); 315 } 316} 317 318ArmSemihosting::RetErrno 319ArmSemihosting::callWriteC(ThreadContext *tc, bool aarch64, 320 std::vector<uint64_t> &argv) 321{ 322 const char c = physProxy(tc).read<char>(argv[0]); 323 324 DPRINTF(Semihosting, "Semihosting SYS_WRITEC('%c')\n", c); 325 std::cout.put(c); 326 327 return retOK(0); 328} 329 330ArmSemihosting::RetErrno 331ArmSemihosting::callWrite0(ThreadContext *tc, bool aarch64, 332 std::vector<uint64_t> &argv) 333{ 334 DPRINTF(Semihosting, "Semihosting SYS_WRITE0(...)\n"); 335 PortProxy &proxy = physProxy(tc); 336 for (Addr addr = (Addr)argv[0]; ; ++addr) { 337 char data = proxy.read<char>(addr); 338 if (data == 0) 339 break; 340 341 std::cout.put(data); 342 } 343 344 return retOK(0); 345} 346 347ArmSemihosting::RetErrno 348ArmSemihosting::callWrite(ThreadContext *tc, bool aarch64, 349 std::vector<uint64_t> &argv) 350{ 351 if (argv[1] > files.size() || !files[argv[1]]) 352 return RetErrno(argv[3], EBADF); 353 354 std::vector<uint8_t> buffer(argv[3]); 355 physProxy(tc).readBlob(argv[2], buffer.data(), buffer.size()); 356 357 int64_t ret = files[argv[1]]->write(buffer.data(), buffer.size()); 358 if (ret < 0) { 359 // No bytes written (we're returning the number of bytes not 360 // written) 361 return RetErrno(argv[3], -ret); 362 } else { 363 // Return the number of bytes not written 364 return RetErrno(argv[3] - ret, 0); 365 } 366} 367 368ArmSemihosting::RetErrno 369ArmSemihosting::callRead(ThreadContext *tc, bool aarch64, 370 std::vector<uint64_t> &argv) 371{ 372 if (argv[1] > files.size() || !files[argv[1]]) 373 return RetErrno(argv[3], EBADF); 374 375 std::vector<uint8_t> buffer(argv[3]); 376 int64_t ret = files[argv[1]]->read(buffer.data(), buffer.size()); 377 if (ret < 0) { 378 return RetErrno(argv[3], -ret); 379 } else { 380 panic_if(ret > buffer.size(), "Read longer than buffer size."); 381 382 physProxy(tc).writeBlob(argv[2], buffer.data(), ret); 383 384 // Return the number of bytes not written 385 return retOK(argv[3] - ret); 386 } 387} 388 389ArmSemihosting::RetErrno 390ArmSemihosting::callReadC(ThreadContext *tc, bool aarch64, 391 std::vector<uint64_t> &argv) 392{ 393 return retOK((char)std::cin.get()); 394} 395 396ArmSemihosting::RetErrno 397ArmSemihosting::callIsError(ThreadContext *tc, bool aarch64, 398 std::vector<uint64_t> &argv) 399{ 400 // Sign extend from a 32 bit integer in aarch32 since the argument 401 // reader zero extends to a uint64_t. 402 const int64_t status = (int64_t)(aarch64 ? argv[1] :sext<32>(argv[1])); 403 // Assume there was an error if the status value is negative. 404 return retOK(status < 0 ? 1 : 0); 405} 406 407ArmSemihosting::RetErrno 408ArmSemihosting::callIsTTY(ThreadContext *tc, bool aarch64, 409 std::vector<uint64_t> &argv) 410{ 411 if (argv[1] > files.size() || !files[argv[1]]) 412 return retError(EBADF); 413 414 int64_t ret = files[argv[1]]->isTTY(); 415 if (ret < 0) { 416 return retError(-ret); 417 } else { 418 return retOK(ret ? 1 : 0); 419 } 420} 421 422ArmSemihosting::RetErrno 423ArmSemihosting::callSeek(ThreadContext *tc, bool aarch64, 424 std::vector<uint64_t> &argv) 425{ 426 if (argv[1] > files.size() || !files[argv[1]]) 427 return retError(EBADF); 428 429 int64_t ret = files[argv[1]]->seek(argv[2]); 430 if (ret < 0) { 431 return retError(-ret); 432 } else { 433 return retOK(0); 434 } 435} 436 437ArmSemihosting::RetErrno 438ArmSemihosting::callFLen(ThreadContext *tc, bool aarch64, 439 std::vector<uint64_t> &argv) 440{ 441 if (argv[1] > files.size() || !files[argv[1]]) 442 return retError(EBADF); 443 444 int64_t ret = files[argv[1]]->isTTY(); 445 if (ret < 0) { 446 return retError(-ret); 447 } else { 448 return retOK(0); 449 } 450} 451 452ArmSemihosting::RetErrno 453ArmSemihosting::callTmpNam(ThreadContext *tc, bool aarch64, 454 std::vector<uint64_t> &argv) 455{ 456 const Addr guest_buf = argv[1]; 457 //const uint64_t id = argv[2]; 458 const uint64_t max_len = argv[3]; 459 460 std::vector<char> buf(L_tmpnam); 461 char *path = tmpnam(buf.data()); 462 if (!path) 463 return retError(EINVAL); 464 465 const size_t path_len = strlen(path); 466 if (path_len >= max_len) 467 return retError(ENOSPC); 468 469 physProxy(tc).writeBlob( 470 guest_buf, (const uint8_t *)path, path_len + 1); 471 return retOK(0); 472} 473 474ArmSemihosting::RetErrno 475ArmSemihosting::callRemove(ThreadContext *tc, bool aarch64, 476 std::vector<uint64_t> &argv) 477{ 478 std::string fname = readString(tc, argv[1], argv[2]); 479 480 if (remove(fname.c_str()) != 0) { 481 return retError(errno); 482 } else { 483 return retOK(0); 484 } 485} 486 487ArmSemihosting::RetErrno 488ArmSemihosting::callRename(ThreadContext *tc, bool aarch64, 489 std::vector<uint64_t> &argv) 490{ 491 std::string from = readString(tc, argv[1], argv[2]); 492 std::string to = readString(tc, argv[3], argv[4]); 493 494 if (rename(from.c_str(), to.c_str()) != 0) { 495 return retError(errno); 496 } else { 497 return retOK(0); 498 } 499} 500 501ArmSemihosting::RetErrno 502ArmSemihosting::callClock(ThreadContext *tc, bool aarch64, 503 std::vector<uint64_t> &argv) 504{ 505 return retOK(curTick() / (SimClock::Int::s / 100)); 506} 507 508ArmSemihosting::RetErrno 509ArmSemihosting::callTime(ThreadContext *tc, bool aarch64, 510 std::vector<uint64_t> &argv) 511{ 512 return retOK(timeBase + round(curTick() / SimClock::Float::s)); 513} 514 515ArmSemihosting::RetErrno 516ArmSemihosting::callSystem(ThreadContext *tc, bool aarch64, 517 std::vector<uint64_t> &argv) 518{ 519 const std::string cmd = readString(tc, argv[1], argv[2]); 520 warn("Semihosting: SYS_SYSTEM not implemented. Guest tried to run: %s\n", 521 cmd); 522 return retError(EINVAL); 523 524} 525 526ArmSemihosting::RetErrno 527ArmSemihosting::callErrno(ThreadContext *tc, bool aarch64, 528 std::vector<uint64_t> &argv) 529{ 530 // Preserve errno by returning it in errno as well. 531 return RetErrno(semiErrno, semiErrno); 532} 533 534ArmSemihosting::RetErrno 535ArmSemihosting::callGetCmdLine(ThreadContext *tc, bool aarch64, 536 std::vector<uint64_t> &argv) 537{ 538 if (cmdLine.size() + 1 < argv[2]) { 539 PortProxy &proxy = physProxy(tc); 540 ByteOrder endian = ArmISA::byteOrder(tc); 541 proxy.writeBlob( 542 (Addr)argv[1], 543 (const uint8_t *)cmdLine.c_str(), cmdLine.size() + 1); 544 545 if (aarch64) 546 proxy.writeHtoG<uint64_t>(argv[0] + 1 * 8, cmdLine.size(), endian); 547 else 548 proxy.writeHtoG<uint32_t>(argv[0] + 1 * 4, cmdLine.size(), endian); 549 return retOK(0); 550 } else { 551 return retError(0); 552 } 553} 554 555ArmSemihosting::RetErrno 556ArmSemihosting::callHeapInfo(ThreadContext *tc, bool aarch64, 557 std::vector<uint64_t> &argv) 558{ 559 const PhysicalMemory &phys = tc->getSystemPtr()->getPhysMem(); 560 const AddrRangeList memories = phys.getConfAddrRanges(); 561 fatal_if(memories.size() < 1, "No memories reported from System"); 562 warn_if(memories.size() > 1, "Multiple physical memory ranges available. " 563 "Using first range heap/stack."); 564 const AddrRange memory = *memories.begin(); 565 const Addr mem_start = memory.start() + memReserve; 566 Addr mem_end = memory.end(); 567 568 // Make sure that 32-bit guests can access their memory. 569 if (!aarch64) { 570 const Addr phys_max = (1ULL << 32) - 1; 571 panic_if(mem_start > phys_max, 572 "Physical memory out of range for a 32-bit guest."); 573 if (mem_end > phys_max) { 574 warn("Some physical memory out of range for a 32-bit guest."); 575 mem_end = phys_max; 576 } 577 } 578 579 fatal_if(mem_start + stackSize >= mem_end, 580 "Physical memory too small to fit desired stack and a heap."); 581 582 const Addr heap_base = mem_start; 583 const Addr heap_limit = mem_end - stackSize + 1; 584 const Addr stack_base = (mem_end + 1) & ~0x7ULL; // 8 byte stack alignment 585 const Addr stack_limit = heap_limit; 586 587 588 inform("Reporting heap/stack info to guest:\n" 589 "\tHeap base: 0x%x\n" 590 "\tHeap limit: 0x%x\n" 591 "\tStack base: 0x%x\n" 592 "\tStack limit: 0x%x\n", 593 heap_base, heap_limit, stack_base, stack_limit); 594 595 Addr base = argv[1]; 596 PortProxy &proxy = physProxy(tc); 597 ByteOrder endian = ArmISA::byteOrder(tc); 598 if (aarch64) { 599 proxy.writeHtoG<uint64_t>(base + 0 * 8, heap_base, endian); 600 proxy.writeHtoG<uint64_t>(base + 1 * 8, heap_limit, endian); 601 proxy.writeHtoG<uint64_t>(base + 2 * 8, stack_base, endian); 602 proxy.writeHtoG<uint64_t>(base + 3 * 8, stack_limit, endian); 603 } else { 604 proxy.writeHtoG<uint32_t>(base + 0 * 4, heap_base, endian); 605 proxy.writeHtoG<uint32_t>(base + 1 * 4, heap_limit, endian); 606 proxy.writeHtoG<uint32_t>(base + 2 * 4, stack_base, endian); 607 proxy.writeHtoG<uint32_t>(base + 3 * 4, stack_limit, endian); 608 } 609 610 return retOK(0); 611} 612 613ArmSemihosting::RetErrno 614ArmSemihosting::callExit(ThreadContext *tc, bool aarch64, 615 std::vector<uint64_t> &argv) 616{ 617 if (aarch64) { 618 semiExit(argv[1], argv[2]); 619 } else { 620 semiExit(argv[0], 0); 621 } 622 623 return retOK(0); 624} 625 626ArmSemihosting::RetErrno 627ArmSemihosting::callExitExtended(ThreadContext *tc, bool aarch64, 628 std::vector<uint64_t> &argv) 629{ 630 semiExit(argv[1], argv[2]); 631 632 return retOK(0); 633} 634 635void 636ArmSemihosting::semiExit(uint64_t code, uint64_t subcode) 637{ 638 auto it = exitCodes.find(code); 639 if (it != exitCodes.end()) { 640 exitSimLoop(it->second, subcode); 641 } else { 642 exitSimLoop(csprintf("semi:0x%x", code), subcode); 643 } 644} 645 646 647ArmSemihosting::RetErrno 648ArmSemihosting::callElapsed(ThreadContext *tc, bool aarch64, 649 std::vector<uint64_t> &argv) 650{ 651 PortProxy &proxy = physProxy(tc); 652 ByteOrder endian = ArmISA::byteOrder(tc); 653 const uint64_t tick = semiTick(curTick()); 654 655 if (aarch64) { 656 proxy.writeHtoG<uint64_t>(argv[0], tick, endian); 657 } else { 658 proxy.writeHtoG<uint32_t>(argv[0] + 0 * 4, tick, endian); 659 proxy.writeHtoG<uint32_t>(argv[0] + 1 * 4, tick >> 32, endian); 660 } 661 662 return retOK(0); 663} 664 665 666ArmSemihosting::RetErrno 667ArmSemihosting::callTickFreq(ThreadContext *tc, bool aarch64, 668 std::vector<uint64_t> &argv) 669{ 670 return retOK(semiTick(SimClock::Frequency)); 671} 672 673const ArmSemihosting::SemiCall * 674ArmSemihosting::getCall(uint32_t op, bool aarch64) 675{ 676 auto it = calls.find(op); 677 if (it == calls.end()) 678 return nullptr; 679 else { 680 return &it->second; 681 } 682} 683 684std::unique_ptr<ArmSemihosting::FileBase> 685ArmSemihosting::FileBase::create( 686 ArmSemihosting &parent, const std::string &fname, const char *mode) 687{ 688 std::unique_ptr<FileBase> file; 689 if (fname == ":semihosting-features") { 690 file.reset(new FileFeatures(parent, fname.c_str(), mode)); 691 } else { 692 file.reset(new File(parent, fname.c_str(), mode)); 693 } 694 695 return file; 696} 697 698std::unique_ptr<ArmSemihosting::FileBase> 699ArmSemihosting::FileBase::create(ArmSemihosting &parent, 700 CheckpointIn &cp, const std::string &sec) 701{ 702 std::unique_ptr<FileBase> file; 703 ScopedCheckpointSection _sec(cp, sec); 704 705 // Was the file open when the checkpoint was created? 706 if (!cp.sectionExists(Serializable::currentSection())) 707 return file; 708 709 std::string fname, mode; 710 paramIn(cp, "name", fname); 711 paramIn(cp, "mode", mode); 712 file = create(parent, fname, mode.c_str()); 713 assert(file); 714 file->unserialize(cp); 715 716 return file; 717} 718 719void 720ArmSemihosting::FileBase::serialize(CheckpointOut &cp) const 721{ 722 paramOut(cp, "name", _name); 723 SERIALIZE_SCALAR(mode); 724} 725 726void 727ArmSemihosting::FileBase::unserialize(CheckpointIn &cp) 728{ 729 /* Unserialization of name and mode happens in 730 * ArmSemihosting::FileBase::create() */ 731} 732 733int64_t 734ArmSemihosting::FileBase::read(uint8_t *buffer, uint64_t size) 735{ 736 return -EINVAL; 737} 738 739int64_t 740ArmSemihosting::FileBase::write(const uint8_t *buffer, uint64_t size) 741{ 742 return -EINVAL; 743} 744 745int64_t 746ArmSemihosting::FileBase::seek(uint64_t pos) 747{ 748 return -EINVAL; 749} 750 751int64_t 752ArmSemihosting::FileBase::flen() 753{ 754 return -EINVAL; 755} 756 757 758ArmSemihosting::FileFeatures::FileFeatures( 759 ArmSemihosting &_parent, const char *_name, const char *_mode) 760 : FileBase(_parent, _name, _mode) 761{ 762} 763 764int64_t 765ArmSemihosting::FileFeatures::read(uint8_t *buffer, uint64_t size) 766{ 767 int64_t len = 0; 768 769 for (; pos < size && pos < ArmSemihosting::features.size(); pos++) 770 buffer[len++] = ArmSemihosting::features[pos]; 771 772 return len; 773} 774 775int64_t 776ArmSemihosting::FileFeatures::seek(uint64_t _pos) 777{ 778 if (_pos < ArmSemihosting::features.size()) { 779 pos = _pos; 780 return 0; 781 } else { 782 return -ENXIO; 783 } 784} 785 786void 787ArmSemihosting::FileFeatures::serialize(CheckpointOut &cp) const 788{ 789 FileBase::serialize(cp); 790 SERIALIZE_SCALAR(pos); 791} 792 793void 794ArmSemihosting::FileFeatures::unserialize(CheckpointIn &cp) 795{ 796 FileBase::unserialize(cp); 797 UNSERIALIZE_SCALAR(pos); 798} 799 800 801 802ArmSemihosting::File::File(ArmSemihosting &_parent, 803 const char *_name, const char *_perms) 804 : FileBase(_parent, _name, _perms), 805 file(nullptr) 806{ 807} 808 809ArmSemihosting::File::~File() 810{ 811 if (file) 812 close(); 813} 814 815int64_t 816ArmSemihosting::File::openImpl(bool in_cpt) 817{ 818 panic_if(file, "Trying to open an already open file.\n"); 819 820 if (_name == ":tt") { 821 if (mode[0] == 'r') { 822 file = stdin; 823 } else if (mode[0] == 'w') { 824 file = stdout; 825 } else if (mode[0] == 'a') { 826 file = stderr; 827 } else { 828 warn("Unknown file mode for the ':tt' special file"); 829 return -EINVAL; 830 } 831 } else { 832 std::string real_mode(this->mode); 833 // Avoid truncating the file if we are restoring from a 834 // checkpoint. 835 if (in_cpt && real_mode[0] == 'w') 836 real_mode[0] = 'a'; 837 838 file = fopen(_name.c_str(), real_mode.c_str()); 839 } 840 841 return file ? 0 : -errno; 842} 843 844int64_t 845ArmSemihosting::File::close() 846{ 847 panic_if(!file, "Trying to close an already closed file.\n"); 848 849 if (needClose()) { 850 fclose(file); 851 } 852 file = nullptr; 853 854 return 0; 855} 856 857bool 858ArmSemihosting::File::isTTY() const 859{ 860 return file == stdout || file == stderr || file == stdin; 861} 862 863int64_t 864ArmSemihosting::File::read(uint8_t *buffer, uint64_t size) 865{ 866 panic_if(!file, "Trying to read from a closed file"); 867 868 size_t ret = fread(buffer, 1, size, file); 869 if (ret == 0) { 870 // Error or EOF. Assume errors are due to invalid file 871 // operations (e.g., reading a write-only stream). 872 return ferror(file) ? -EINVAL : 0; 873 } else { 874 return ret; 875 } 876} 877 878int64_t 879ArmSemihosting::File::write(const uint8_t *buffer, uint64_t size) 880{ 881 panic_if(!file, "Trying to write to a closed file"); 882 883 884 size_t ret = fwrite(buffer, 1, size, file); 885 if (ret == 0) { 886 // Assume errors are due to invalid file operations (e.g., 887 // writing a read-only stream). 888 return -EINVAL; 889 } else { 890 return ret; 891 } 892} 893 894int64_t 895ArmSemihosting::File::seek(uint64_t _pos) 896{ 897 panic_if(!file, "Trying to seek in a closed file"); 898 899 errno = 0; 900 if (fseek(file, _pos, SEEK_SET) == 0) 901 return 0; 902 else 903 return -errno; 904} 905 906int64_t 907ArmSemihosting::File::flen() 908{ 909 errno = 0; 910 long pos = ftell(file); 911 if (pos < 0) 912 return -errno; 913 914 if (fseek(file, 0, SEEK_END) != 0) 915 return -errno; 916 917 long len = ftell(file); 918 if (len < 0) 919 return -errno; 920 921 if (fseek(file, pos, SEEK_SET) != 0) 922 return -errno; 923 924 return len; 925} 926 927 928void 929ArmSemihosting::File::serialize(CheckpointOut &cp) const 930{ 931 FileBase::serialize(cp); 932 933 if (!isTTY()) { 934 long pos = file ? ftell(file) : 0; 935 panic_if(pos < 0, "Failed to get file position."); 936 SERIALIZE_SCALAR(pos); 937 } 938} 939 940void 941ArmSemihosting::File::unserialize(CheckpointIn &cp) 942{ 943 FileBase::unserialize(cp); 944 945 if (openImpl(true) < 0) { 946 fatal("Failed to open file: %s", _name); 947 } 948 949 if (!isTTY()) { 950 long pos = 0; 951 UNSERIALIZE_SCALAR(pos); 952 if (fseek(file, pos, SEEK_SET) != 0) { 953 fatal("Failed seek to current position (%i) in '%s'", pos, _name); 954 } 955 } 956} 957 958 959ArmSemihosting * 960ArmSemihostingParams::create() 961{ 962 return new ArmSemihosting(this); 963} 964