vmm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * VMM startup file.
  3. *
  4. * COPYRIGHT (C) 2013-2014, Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * This file is part of RT-Thread (http://www.rt-thread.org)
  7. * Maintainer: bernard.xiong <bernard.xiong at gmail.com>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * Change Logs:
  26. * Date Author Notes
  27. * 2013-06-15 Bernard the first verion
  28. */
  29. #include <rthw.h>
  30. #include <rtthread.h>
  31. #include "board.h"
  32. #include "vmm.h"
  33. #include "vmm_context.h"
  34. extern void rt_hw_interrupt_init(void);
  35. extern void rt_application_init(void);
  36. void vmm_entry(struct vmm_entry_param* param) SECTION(".vmm_init");
  37. #ifdef RT_USING_LOGTRACE
  38. #include <log_trace.h>
  39. static struct log_trace_session _lgs = {
  40. .id = {.name = "vmm"},
  41. .lvl = LOG_TRACE_LEVEL_VERBOSE,
  42. };
  43. #endif
  44. struct rt_thread vmm_thread SECTION(".bss.share.vmm");
  45. extern rt_uint8_t vmm_stack_start;
  46. extern rt_uint8_t vmm_stack_end;
  47. void vmm_thread_init(struct rt_thread *thread, const char *name)
  48. {
  49. extern struct rt_thread *rt_current_thread;
  50. rt_thread_init(thread, name, RT_NULL, RT_NULL,
  51. &vmm_stack_start, &vmm_stack_end - &vmm_stack_start,
  52. RT_THREAD_PRIORITY_MAX - 1, 10);
  53. /* set thread to ready status but not switch to */
  54. rt_thread_startup(thread);
  55. /* set current thread as vmm thread */
  56. rt_current_thread = thread;
  57. }
  58. #ifdef VMM_VERIFY_GUEST
  59. static void _verify_guest(void *p)
  60. {
  61. while (1)
  62. {
  63. rt_thread_delay(RT_TICK_PER_SECOND/4);
  64. vmm_verify_guest_status(vmm_thread.sp);
  65. }
  66. }
  67. static void vmm_create_monitor(void)
  68. {
  69. rt_thread_t tid;
  70. tid = rt_thread_create("vmon",
  71. _verify_guest, RT_NULL,
  72. 1024, 8, 20);
  73. if (tid)
  74. rt_thread_startup(tid);
  75. }
  76. #endif
  77. #ifdef RT_VMM_USING_DOMAIN
  78. extern unsigned long guest_domain_val;
  79. extern unsigned long vmm_domain_val;
  80. #endif
  81. static void vmm_entry_glue(rt_uint32_t level,
  82. unsigned int vmm_domain,
  83. unsigned int kernel_domain)
  84. /* inline would make the section setting meaningless */
  85. __attribute__((noinline))
  86. SECTION(".vmm_glue");
  87. static void vmm_entry_glue(rt_uint32_t level,
  88. unsigned int vmm_domain,
  89. unsigned int kernel_domain)
  90. {
  91. rt_schedule();
  92. #ifdef RT_VMM_USING_DOMAIN
  93. /* protect us from the guest code, but leave the shared region permission
  94. */
  95. guest_domain_val &= ~(0x3 << (vmm_domain * 2));
  96. /* don't touch the guest kernel space */
  97. vmm_domain_val &= ~(0x3 << (kernel_domain * 2));
  98. #endif
  99. rt_hw_interrupt_enable(level);
  100. }
  101. void vmm_entry(struct vmm_entry_param *param)
  102. {
  103. rt_uint32_t level;
  104. level = rt_hw_interrupt_disable();
  105. /* set iomap */
  106. vmm_iomap_init(param->iomap);
  107. /* set VMM context address */
  108. vmm_context_init(&RT_VMM_SHARE->ctx);
  109. /* init hardware interrupt */
  110. rt_hw_interrupt_init();
  111. vmm_vector_init();
  112. /* init board */
  113. rt_hw_board_init();
  114. #ifdef RT_USING_LOGTRACE
  115. /* Some parts of VMM use log_trace, so we need to init it right after
  116. * board_init. */
  117. log_trace_init();
  118. log_trace_set_device(RT_CONSOLE_DEVICE_NAME);
  119. log_trace_register_session(&_lgs);
  120. #endif
  121. /* show version */
  122. rt_show_version();
  123. rt_kprintf("share ctx: %p(%x)\n",
  124. &RT_VMM_SHARE->ctx, sizeof(RT_VMM_SHARE->ctx));
  125. /* init timer system */
  126. rt_system_timer_init();
  127. {
  128. rt_uint32_t ttbr;
  129. asm volatile ("mrc p15, 0, %0, c2, c0, 0\n"
  130. : "=r"(ttbr));
  131. rt_kprintf("Linux TTBR: 0x%08x\n", ttbr);
  132. /*
  133. *rt_hw_cpu_dump_page_table((void*)((ttbr & (0xffffc000))
  134. * - 0x80000000 + 0xC0000000));
  135. */
  136. /*rt_hw_cpu_dump_page_table((void*)(0xc0004000));*/
  137. }
  138. #ifdef RT_VMM_USING_DOMAIN
  139. vmm_context_init_domain(param->domain);
  140. #endif
  141. rt_kprintf("heap: 0x%p - 0x%p, %dKi bytes\n",
  142. (void*)HEAP_BEGIN, (void*)HEAP_END,
  143. ((int)HEAP_END - (int)HEAP_BEGIN) / 1024);
  144. /* init heap memory system */
  145. rt_system_heap_init((void*)HEAP_BEGIN, (void*)HEAP_END);
  146. /* init scheduler system */
  147. rt_system_scheduler_init();
  148. rt_kprintf("user application init.\n");
  149. /* init application */
  150. rt_application_init();
  151. #ifdef VMM_VERIFY_GUEST
  152. vmm_create_monitor();
  153. #endif
  154. rt_system_timer_thread_init();
  155. vmm_thread_init(&vmm_thread, "vmm");
  156. #ifdef RT_VMM_USING_DOMAIN
  157. rt_kprintf("domain protect present\n");
  158. #endif
  159. /* start scheduler */
  160. rt_kprintf("do the first scheduling...\n");
  161. vmm_entry_glue(level,
  162. param->domain->vmm,
  163. param->domain->kernel);
  164. }