Emulator.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Emulator.h"
  27. #include "SoftCPU.h"
  28. #include <AK/LogStream.h>
  29. #include <Kernel/API/Syscall.h>
  30. #include <unistd.h>
  31. #include <stdio.h>
  32. namespace UserspaceEmulator {
  33. Emulator::Emulator()
  34. : m_cpu(*this)
  35. {
  36. }
  37. int Emulator::exec(X86::SimpleInstructionStream& stream, u32 base)
  38. {
  39. size_t offset = 0;
  40. while (!m_shutdown) {
  41. auto insn = X86::Instruction::from_stream(stream, true, true);
  42. out() << "\033[33;1m" << insn.to_string(base + offset) << "\033[0m";
  43. // FIXME: Remove this hack once it's no longer needed :^)
  44. if (insn.mnemonic() == "RET")
  45. break;
  46. (m_cpu.*insn.handler())(insn);
  47. m_cpu.dump();
  48. offset += insn.length();
  49. }
  50. return m_exit_status;
  51. }
  52. u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3)
  53. {
  54. (void) arg2;
  55. (void) arg3;
  56. printf("Syscall: %s (%x)\n", Syscall::to_string((Syscall::Function)function), function);
  57. switch (function) {
  58. case SC_getuid:
  59. return virt$getuid();
  60. case SC_exit:
  61. virt$exit((int)arg1);
  62. return 0;
  63. default:
  64. warn() << "Unimplemented syscall!";
  65. TODO();
  66. }
  67. }
  68. uid_t Emulator::virt$getuid()
  69. {
  70. return getuid();
  71. }
  72. void Emulator::virt$exit(int status)
  73. {
  74. out() << "exit(" << status << "), shutting down!";
  75. m_exit_status = status;
  76. m_shutdown = true;
  77. }
  78. }