machine_kexec.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. printk(KERN_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. printk(KERN_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, it's possible that this is
  113. * just a false positive, but relatively unlikely. We dump
  114. * out the contents of the section so we can diagnose better.
  115. */
  116. csum = ip_compute_csum(pg, bhdrp->b_size);
  117. if (csum != 0) {
  118. int i;
  119. unsigned char *p = pg;
  120. int nbytes = min((Elf32_Word)1000, bhdrp->b_size);
  121. printk(KERN_INFO "%s: bad checksum %#x\n", __func__, csum);
  122. printk(KERN_INFO "bytes (%d):", bhdrp->b_size);
  123. for (i = 0; i < nbytes; ++i)
  124. printk(" %02x", p[i]);
  125. if (bhdrp->b_size != nbytes)
  126. printk(" ...");
  127. printk("\n");
  128. return 0;
  129. }
  130. nhdrp = (Elf32_Nhdr *) (bhdrp + 1);
  131. while (nhdrp->n_type != EBN_COMMAND_LINE) {
  132. desc = (unsigned char *) (nhdrp + 1);
  133. desc += roundupsz(nhdrp->n_descsz);
  134. nhdrp = (Elf32_Nhdr *) desc;
  135. /* still in bounds? */
  136. if ((unsigned char *) (nhdrp + 1) >
  137. ((unsigned char *) pg) + bhdrp->b_size) {
  138. printk(KERN_INFO "%s: out of bounds\n", __func__);
  139. return 0;
  140. }
  141. }
  142. command_line = (unsigned char *) (nhdrp + 1);
  143. desc = command_line;
  144. while (*desc != '\0') {
  145. desc++;
  146. if (((unsigned long)desc & PAGE_MASK) != (unsigned long)pg) {
  147. printk(KERN_INFO "%s: ran off end of page\n",
  148. __func__);
  149. return 0;
  150. }
  151. }
  152. return command_line;
  153. }
  154. static void kexec_find_and_set_command_line(struct kimage *image)
  155. {
  156. kimage_entry_t *ptr, entry;
  157. unsigned char *command_line = 0;
  158. unsigned char *r;
  159. HV_Errno hverr;
  160. for (ptr = &image->head;
  161. (entry = *ptr) && !(entry & IND_DONE);
  162. ptr = (entry & IND_INDIRECTION) ?
  163. phys_to_virt((entry & PAGE_MASK)) : ptr + 1) {
  164. if ((entry & IND_SOURCE)) {
  165. void *va =
  166. kmap_atomic_pfn(entry >> PAGE_SHIFT, KM_USER0);
  167. r = kexec_bn2cl(va);
  168. if (r) {
  169. command_line = r;
  170. break;
  171. }
  172. kunmap_atomic(va, KM_USER0);
  173. }
  174. }
  175. if (command_line != 0) {
  176. printk(KERN_INFO "setting new command line to \"%s\"\n",
  177. command_line);
  178. hverr = hv_set_command_line(
  179. (HV_VirtAddr) command_line, strlen(command_line));
  180. kunmap_atomic(command_line, KM_USER0);
  181. } else {
  182. printk(KERN_INFO "%s: no command line found; making empty\n",
  183. __func__);
  184. hverr = hv_set_command_line((HV_VirtAddr) command_line, 0);
  185. }
  186. if (hverr) {
  187. printk(KERN_WARNING
  188. "%s: call to hv_set_command_line returned error: %d\n",
  189. __func__, hverr);
  190. }
  191. }
  192. /*
  193. * The kexec code range-checks all its PAs, so to avoid having it run
  194. * amok and allocate memory and then sequester it from every other
  195. * controller, we force it to come from controller zero. We also
  196. * disable the oom-killer since if we do end up running out of memory,
  197. * that almost certainly won't help.
  198. */
  199. struct page *kimage_alloc_pages_arch(gfp_t gfp_mask, unsigned int order)
  200. {
  201. gfp_mask |= __GFP_THISNODE | __GFP_NORETRY;
  202. return alloc_pages_node(0, gfp_mask, order);
  203. }
  204. static void setup_quasi_va_is_pa(void)
  205. {
  206. HV_PTE *pgtable;
  207. HV_PTE pte;
  208. int i;
  209. /*
  210. * Flush our TLB to prevent conflicts between the previous contents
  211. * and the new stuff we're about to add.
  212. */
  213. local_flush_tlb_all();
  214. /* setup VA is PA, at least up to PAGE_OFFSET */
  215. pgtable = (HV_PTE *)current->mm->pgd;
  216. pte = hv_pte(_PAGE_KERNEL | _PAGE_HUGE_PAGE);
  217. pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_NO_L3);
  218. for (i = 0; i < pgd_index(PAGE_OFFSET); i++)
  219. pgtable[i] = pfn_pte(i << (HPAGE_SHIFT - PAGE_SHIFT), pte);
  220. }
  221. NORET_TYPE void machine_kexec(struct kimage *image)
  222. {
  223. void *reboot_code_buffer;
  224. NORET_TYPE void (*rnk)(unsigned long, void *, unsigned long)
  225. ATTRIB_NORET;
  226. /* Mask all interrupts before starting to reboot. */
  227. interrupt_mask_set_mask(~0ULL);
  228. kexec_find_and_set_command_line(image);
  229. /*
  230. * Adjust the home caching of the control page to be cached on
  231. * this cpu, and copy the assembly helper into the control
  232. * code page, which we map in the vmalloc area.
  233. */
  234. homecache_change_page_home(image->control_code_page, 0,
  235. smp_processor_id());
  236. reboot_code_buffer = vmap(&image->control_code_page, 1, 0,
  237. __pgprot(_PAGE_KERNEL | _PAGE_EXECUTABLE));
  238. memcpy(reboot_code_buffer, relocate_new_kernel,
  239. relocate_new_kernel_size);
  240. __flush_icache_range(
  241. (unsigned long) reboot_code_buffer,
  242. (unsigned long) reboot_code_buffer + relocate_new_kernel_size);
  243. setup_quasi_va_is_pa();
  244. /* now call it */
  245. rnk = reboot_code_buffer;
  246. (*rnk)(image->head, reboot_code_buffer, image->start);
  247. }