Emulator.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <stdio.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. namespace UserspaceEmulator {
  34. static constexpr u32 stack_location = 0x10000000;
  35. static constexpr size_t stack_size = 64 * KB;
  36. class SimpleRegion final : public SoftMMU::Region {
  37. public:
  38. SimpleRegion(u32 base, u32 size)
  39. : Region(base, size)
  40. {
  41. m_data = (u8*)malloc(size);
  42. }
  43. ~SimpleRegion()
  44. {
  45. free(m_data);
  46. }
  47. virtual u8 read8(u32 offset) override
  48. {
  49. ASSERT(offset < size());
  50. return *reinterpret_cast<const u8*>(m_data + offset);
  51. }
  52. virtual u16 read16(u32 offset) override
  53. {
  54. ASSERT(offset + 1 < size());
  55. return *reinterpret_cast<const u16*>(m_data + offset);
  56. }
  57. virtual u32 read32(u32 offset) override
  58. {
  59. ASSERT(offset + 3 < size());
  60. return *reinterpret_cast<const u32*>(m_data + offset);
  61. }
  62. virtual void write8(u32 offset, u8 value) override
  63. {
  64. ASSERT(offset < size());
  65. *reinterpret_cast<u8*>(m_data + offset) = value;
  66. }
  67. virtual void write16(u32 offset, u16 value) override
  68. {
  69. ASSERT(offset + 1 < size());
  70. *reinterpret_cast<u16*>(m_data + offset) = value;
  71. }
  72. virtual void write32(u32 offset, u32 value) override
  73. {
  74. ASSERT(offset + 3 < size());
  75. *reinterpret_cast<u32*>(m_data + offset) = value;
  76. }
  77. u8* data() { return m_data; }
  78. private:
  79. u8* m_data { nullptr };
  80. };
  81. Emulator::Emulator()
  82. : m_cpu(*this)
  83. {
  84. setup_stack();
  85. }
  86. void Emulator::setup_stack()
  87. {
  88. auto stack_region = make<SimpleRegion>(stack_location, stack_size);
  89. m_mmu.add_region(move(stack_region));
  90. m_cpu.set_esp(stack_location + stack_size);
  91. m_cpu.push32(0);
  92. m_cpu.push32(0);
  93. m_cpu.push32(0);
  94. m_cpu.push32(0);
  95. }
  96. bool Emulator::load_elf(const ELF::Loader& elf)
  97. {
  98. elf.image().for_each_program_header([&](const ELF::Image::ProgramHeader& program_header) {
  99. if (program_header.type() != PT_LOAD)
  100. return;
  101. auto region = make<SimpleRegion>(program_header.vaddr().get(), program_header.size_in_memory());
  102. memcpy(region->data(), program_header.raw_data(), program_header.size_in_image());
  103. mmu().add_region(move(region));
  104. });
  105. m_cpu.set_eip(elf.image().entry().get());
  106. return true;
  107. }
  108. int Emulator::exec()
  109. {
  110. while (!m_shutdown) {
  111. auto base_eip = m_cpu.eip();
  112. auto insn = X86::Instruction::from_stream(m_cpu, true, true);
  113. out() << (const void*)base_eip << " \033[33;1m" << insn.to_string(base_eip) << "\033[0m";
  114. // FIXME: Remove this hack once it's no longer needed :^)
  115. if (insn.mnemonic() == "RET")
  116. break;
  117. (m_cpu.*insn.handler())(insn);
  118. m_cpu.dump();
  119. }
  120. return m_exit_status;
  121. }
  122. u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3)
  123. {
  124. (void)arg2;
  125. (void)arg3;
  126. printf("Syscall: %s (%x)\n", Syscall::to_string((Syscall::Function)function), function);
  127. switch (function) {
  128. case SC_getuid:
  129. return virt$getuid();
  130. case SC_exit:
  131. virt$exit((int)arg1);
  132. return 0;
  133. default:
  134. warn() << "Unimplemented syscall!";
  135. TODO();
  136. }
  137. }
  138. uid_t Emulator::virt$getuid()
  139. {
  140. return getuid();
  141. }
  142. void Emulator::virt$exit(int status)
  143. {
  144. out() << "exit(" << status << "), shutting down!";
  145. m_exit_status = status;
  146. m_shutdown = true;
  147. }
  148. }