machine_kexec.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * based on machine_kexec.c from other architectures in linux-2.6.18
  15. */
  16. #include <linux/mm.h>
  17. #include <linux/kexec.h>
  18. #include <linux/delay.h>
  19. #include <linux/reboot.h>
  20. #include <linux/errno.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/cpumask.h>
  23. #include <linux/kernel.h>
  24. #include <linux/elf.h>
  25. #include <linux/highmem.h>
  26. #include <linux/mmu_context.h>
  27. #include <linux/io.h>
  28. #include <linux/timex.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/checksum.h>
  33. #include <hv/hypervisor.h>
  34. /*
  35. * This stuff is not in elf.h and is not in any other kernel include.
  36. * This stuff is needed below in the little boot notes parser to
  37. * extract the command line so we can pass it to the hypervisor.
  38. */
  39. struct Elf32_Bhdr {
  40. Elf32_Word b_signature;
  41. Elf32_Word b_size;
  42. Elf32_Half b_checksum;
  43. Elf32_Half b_records;
  44. };
  45. #define ELF_BOOT_MAGIC 0x0E1FB007
  46. #define EBN_COMMAND_LINE 0x00000004
  47. #define roundupsz(X) (((X) + 3) & ~3)
  48. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  49. void machine_shutdown(void)
  50. {
  51. /*
  52. * Normally we would stop all the other processors here, but
  53. * the check in machine_kexec_prepare below ensures we'll only
  54. * get this far if we've been booted with "nosmp" on the
  55. * command line or without CONFIG_SMP so there's nothing to do
  56. * here (for now).
  57. */
  58. }
  59. void machine_crash_shutdown(struct pt_regs *regs)
  60. {
  61. /*
  62. * Cannot happen. This type of kexec is disabled on this
  63. * architecture (and enforced in machine_kexec_prepare below).
  64. */
  65. }
  66. int machine_kexec_prepare(struct kimage *image)
  67. {
  68. if (num_online_cpus() > 1) {
  69. pr_warning("%s: detected attempt to kexec "
  70. "with num_online_cpus() > 1\n",
  71. __func__);
  72. return -ENOSYS;
  73. }
  74. if (image->type != KEXEC_TYPE_DEFAULT) {
  75. pr_warning("%s: detected attempt to kexec "
  76. "with unsupported type: %d\n",
  77. __func__,
  78. image->type);
  79. return -ENOSYS;
  80. }
  81. return 0;
  82. }
  83. void machine_kexec_cleanup(struct kimage *image)
  84. {
  85. /*
  86. * We did nothing in machine_kexec_prepare,
  87. * so we have nothing to do here.
  88. */
  89. }
  90. /*
  91. * If we can find elf boot notes on this page, return the command
  92. * line. Otherwise, silently return null. Somewhat kludgy, but no
  93. * good way to do this without significantly rearchitecting the
  94. * architecture-independent kexec code.
  95. */
  96. static unsigned char *kexec_bn2cl(void *pg)
  97. {
  98. struct Elf32_Bhdr *bhdrp;
  99. Elf32_Nhdr *nhdrp;
  100. unsigned char *desc;
  101. unsigned char *command_line;
  102. __sum16 csum;
  103. bhdrp = (struct Elf32_Bhdr *) pg;
  104. /*
  105. * This routine is invoked for every source page, so make
  106. * sure to quietly ignore every impossible page.
  107. */
  108. if (bhdrp->b_signature != ELF_BOOT_MAGIC ||
  109. bhdrp->b_size > PAGE_SIZE)
  110. return 0;
  111. /*
  112. * If we get a checksum mismatch, warn with the checksum
  113. * so we can diagnose better.
  114. */
  115. csum = ip_compute_csum(pg, bhdrp->b_size);
  116. if (csum != 0) {
  117. pr_warning("%s: bad checksum %#x (size %d)\n",
  118. __func__, csum, bhdrp->b_size);
  119. return 0;
  120. }
  121. nhdrp = (Elf32_Nhdr *) (bhdrp + 1);
  122. while (nhdrp->n_type != EBN_COMMAND_LINE) {
  123. desc = (unsigned char *) (nhdrp + 1);
  124. desc += roundupsz(nhdrp->n_descsz);
  125. nhdrp = (Elf32_Nhdr *) desc;
  126. /* still in bounds? */
  127. if ((unsigned char *) (nhdrp + 1) >
  128. ((unsigned char *) pg) + bhdrp->b_size) {
  129. pr_info("%s: out of bounds\n", __func__);
  130. return 0;
  131. }
  132. }
  133. command_line = (unsigned char *) (nhdrp + 1);
  134. desc = command_line;
  135. while (*desc != '\0') {
  136. desc++;
  137. if (((unsigned long)desc & PAGE_MASK) != (unsigned long)pg) {
  138. pr_info("%s: ran off end of page\n",
  139. __func__);
  140. return 0;
  141. }
  142. }
  143. return command_line;
  144. }
  145. static void kexec_find_and_set_command_line(struct kimage *image)
  146. {
  147. kimage_entry_t *ptr, entry;
  148. unsigned char *command_line = 0;
  149. unsigned char *r;
  150. HV_Errno hverr;
  151. for (ptr = &image->head;
  152. (entry = *ptr) && !(entry & IND_DONE);
  153. ptr = (entry & IND_INDIRECTION) ?
  154. phys_to_virt((entry & PAGE_MASK)) : ptr + 1) {
  155. if ((entry & IND_SOURCE)) {
  156. void *va =
  157. kmap_atomic_pfn(entry >> PAGE_SHIFT);
  158. r = kexec_bn2cl(va);
  159. if (r) {
  160. command_line = r;
  161. break;
  162. }
  163. kunmap_atomic(va);
  164. }
  165. }
  166. if (command_line != 0) {
  167. pr_info("setting new command line to \"%s\"\n",
  168. command_line);
  169. hverr = hv_set_command_line(
  170. (HV_VirtAddr) command_line, strlen(command_line));
  171. kunmap_atomic(command_line);
  172. } else {
  173. pr_info("%s: no command line found; making empty\n",
  174. __func__);
  175. hverr = hv_set_command_line((HV_VirtAddr) command_line, 0);
  176. }
  177. if (hverr)
  178. pr_warning("%s: hv_set_command_line returned error: %d\n",
  179. __func__, hverr);
  180. }
  181. /*
  182. * The kexec code range-checks all its PAs, so to avoid having it run
  183. * amok and allocate memory and then sequester it from every other
  184. * controller, we force it to come from controller zero. We also
  185. * disable the oom-killer since if we do end up running out of memory,
  186. * that almost certainly won't help.
  187. */
  188. struct page *kimage_alloc_pages_arch(gfp_t gfp_mask, unsigned int order)
  189. {
  190. gfp_mask |= __GFP_THISNODE | __GFP_NORETRY;
  191. return alloc_pages_node(0, gfp_mask, order);
  192. }
  193. static void setup_quasi_va_is_pa(void)
  194. {
  195. HV_PTE *pgtable;
  196. HV_PTE pte;
  197. int i;
  198. /*
  199. * Flush our TLB to prevent conflicts between the previous contents
  200. * and the new stuff we're about to add.
  201. */
  202. local_flush_tlb_all();
  203. /* setup VA is PA, at least up to PAGE_OFFSET */
  204. pgtable = (HV_PTE *)current->mm->pgd;
  205. pte = hv_pte(_PAGE_KERNEL | _PAGE_HUGE_PAGE);
  206. pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_NO_L3);
  207. for (i = 0; i < pgd_index(PAGE_OFFSET); i++) {
  208. unsigned long pfn = i << (HPAGE_SHIFT - PAGE_SHIFT);
  209. if (pfn_valid(pfn))
  210. __set_pte(&pgtable[i], pfn_pte(pfn, pte));
  211. }
  212. }
  213. void machine_kexec(struct kimage *image)
  214. {
  215. void *reboot_code_buffer;
  216. void (*rnk)(unsigned long, void *, unsigned long)
  217. __noreturn;
  218. /* Mask all interrupts before starting to reboot. */
  219. interrupt_mask_set_mask(~0ULL);
  220. kexec_find_and_set_command_line(image);
  221. /*
  222. * Adjust the home caching of the control page to be cached on
  223. * this cpu, and copy the assembly helper into the control
  224. * code page, which we map in the vmalloc area.
  225. */
  226. homecache_change_page_home(image->control_code_page, 0,
  227. smp_processor_id());
  228. reboot_code_buffer = vmap(&image->control_code_page, 1, 0,
  229. __pgprot(_PAGE_KERNEL | _PAGE_EXECUTABLE));
  230. memcpy(reboot_code_buffer, relocate_new_kernel,
  231. relocate_new_kernel_size);
  232. __flush_icache_range(
  233. (unsigned long) reboot_code_buffer,
  234. (unsigned long) reboot_code_buffer + relocate_new_kernel_size);
  235. setup_quasi_va_is_pa();
  236. /* now call it */
  237. rnk = reboot_code_buffer;
  238. (*rnk)(image->head, reboot_code_buffer, image->start);
  239. }