Emulator.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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*)calloc(1, 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(NonnullRefPtr<ELF::Loader> elf)
  82. : m_elf(move(elf))
  83. , m_cpu(*this)
  84. {
  85. setup_stack();
  86. }
  87. void Emulator::setup_stack()
  88. {
  89. auto stack_region = make<SimpleRegion>(stack_location, stack_size);
  90. m_mmu.add_region(move(stack_region));
  91. m_cpu.set_esp(stack_location + stack_size);
  92. m_cpu.push32(0); // char** envp = { nullptr }
  93. u32 envp = m_cpu.esp();
  94. m_cpu.push32(0); // char** argv = { nullptr }
  95. u32 argv = m_cpu.esp();
  96. m_cpu.push32(0); // (alignment)
  97. m_cpu.push32(0); // (alignment)
  98. u32 argc = 0;
  99. m_cpu.push32(envp);
  100. m_cpu.push32(argv);
  101. m_cpu.push32(argc);
  102. m_cpu.push32(0); // (alignment)
  103. }
  104. bool Emulator::load_elf()
  105. {
  106. m_elf->image().for_each_program_header([&](const ELF::Image::ProgramHeader& program_header) {
  107. if (program_header.type() != PT_LOAD)
  108. return;
  109. auto region = make<SimpleRegion>(program_header.vaddr().get(), program_header.size_in_memory());
  110. memcpy(region->data(), program_header.raw_data(), program_header.size_in_image());
  111. mmu().add_region(move(region));
  112. });
  113. m_cpu.set_eip(m_elf->image().entry().get());
  114. return true;
  115. }
  116. class ELFSymbolProvider final : public X86::SymbolProvider {
  117. public:
  118. ELFSymbolProvider(ELF::Loader& loader)
  119. : m_loader(loader)
  120. {
  121. }
  122. virtual String symbolicate(FlatPtr address, u32* offset = nullptr) const
  123. {
  124. return m_loader.symbolicate(address, offset);
  125. }
  126. private:
  127. ELF::Loader& m_loader;
  128. };
  129. int Emulator::exec()
  130. {
  131. ELFSymbolProvider symbol_provider(*m_elf);
  132. while (!m_shutdown) {
  133. auto base_eip = m_cpu.eip();
  134. auto insn = X86::Instruction::from_stream(m_cpu, true, true);
  135. out() << (const void*)base_eip << " \033[33;1m" << insn.to_string(base_eip, &symbol_provider) << "\033[0m";
  136. (m_cpu.*insn.handler())(insn);
  137. m_cpu.dump();
  138. }
  139. return m_exit_status;
  140. }
  141. u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3)
  142. {
  143. (void)arg2;
  144. (void)arg3;
  145. printf("Syscall: %s (%x)\n", Syscall::to_string((Syscall::Function)function), function);
  146. switch (function) {
  147. case SC_getuid:
  148. return virt$getuid();
  149. case SC_exit:
  150. virt$exit((int)arg1);
  151. return 0;
  152. default:
  153. warn() << "Unimplemented syscall!";
  154. TODO();
  155. }
  156. }
  157. uid_t Emulator::virt$getuid()
  158. {
  159. return getuid();
  160. }
  161. void Emulator::virt$exit(int status)
  162. {
  163. out() << "exit(" << status << "), shutting down!";
  164. m_exit_status = status;
  165. m_shutdown = true;
  166. }
  167. }