machine_kexec.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 <asm/tlbflush.h>
  34. #include <asm/homecache.h>
  35. #include <hv/hypervisor.h>
  36. /*
  37. * This stuff is not in elf.h and is not in any other kernel include.
  38. * This stuff is needed below in the little boot notes parser to
  39. * extract the command line so we can pass it to the hypervisor.
  40. */
  41. struct Elf32_Bhdr {
  42. Elf32_Word b_signature;
  43. Elf32_Word b_size;
  44. Elf32_Half b_checksum;
  45. Elf32_Half b_records;
  46. };
  47. #define ELF_BOOT_MAGIC 0x0E1FB007
  48. #define EBN_COMMAND_LINE 0x00000004
  49. #define roundupsz(X) (((X) + 3) & ~3)
  50. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  51. void machine_shutdown(void)
  52. {
  53. /*
  54. * Normally we would stop all the other processors here, but
  55. * the check in machine_kexec_prepare below ensures we'll only
  56. * get this far if we've been booted with "nosmp" on the
  57. * command line or without CONFIG_SMP so there's nothing to do
  58. * here (for now).
  59. */
  60. }
  61. void machine_crash_shutdown(struct pt_regs *regs)
  62. {
  63. /*
  64. * Cannot happen. This type of kexec is disabled on this
  65. * architecture (and enforced in machine_kexec_prepare below).
  66. */
  67. }
  68. int machine_kexec_prepare(struct kimage *image)
  69. {
  70. if (num_online_cpus() > 1) {
  71. pr_warning("%s: detected attempt to kexec "
  72. "with num_online_cpus() > 1\n",
  73. __func__);
  74. return -ENOSYS;
  75. }
  76. if (image->type != KEXEC_TYPE_DEFAULT) {
  77. pr_warning("%s: detected attempt to kexec "
  78. "with unsupported type: %d\n",
  79. __func__,
  80. image->type);
  81. return -ENOSYS;
  82. }
  83. return 0;
  84. }
  85. void machine_kexec_cleanup(struct kimage *image)
  86. {
  87. /*
  88. * We did nothing in machine_kexec_prepare,
  89. * so we have nothing to do here.
  90. */
  91. }
  92. /*
  93. * If we can find elf boot notes on this page, return the command
  94. * line. Otherwise, silently return null. Somewhat kludgy, but no
  95. * good way to do this without significantly rearchitecting the
  96. * architecture-independent kexec code.
  97. */
  98. static unsigned char *kexec_bn2cl(void *pg)
  99. {
  100. struct Elf32_Bhdr *bhdrp;
  101. Elf32_Nhdr *nhdrp;
  102. unsigned char *desc;
  103. unsigned char *command_line;
  104. __sum16 csum;
  105. bhdrp = (struct Elf32_Bhdr *) pg;
  106. /*
  107. * This routine is invoked for every source page, so make
  108. * sure to quietly ignore every impossible page.
  109. */
  110. if (bhdrp->b_signature != ELF_BOOT_MAGIC ||
  111. bhdrp->b_size > PAGE_SIZE)
  112. return 0;
  113. /*
  114. * If we get a checksum mismatch, warn with the checksum
  115. * so we can diagnose better.
  116. */
  117. csum = ip_compute_csum(pg, bhdrp->b_size);
  118. if (csum != 0) {
  119. pr_warning("%s: bad checksum %#x (size %d)\n",
  120. __func__, csum, bhdrp->b_size);
  121. return 0;
  122. }
  123. nhdrp = (Elf32_Nhdr *) (bhdrp + 1);
  124. while (nhdrp->n_type != EBN_COMMAND_LINE) {
  125. desc = (unsigned char *) (nhdrp + 1);
  126. desc += roundupsz(nhdrp->n_descsz);
  127. nhdrp = (Elf32_Nhdr *) desc;
  128. /* still in bounds? */
  129. if ((unsigned char *) (nhdrp + 1) >
  130. ((unsigned char *) pg) + bhdrp->b_size) {
  131. pr_info("%s: out of bounds\n", __func__);
  132. return 0;
  133. }
  134. }
  135. command_line = (unsigned char *) (nhdrp + 1);
  136. desc = command_line;
  137. while (*desc != '\0') {
  138. desc++;
  139. if (((unsigned long)desc & PAGE_MASK) != (unsigned long)pg) {
  140. pr_info("%s: ran off end of page\n",
  141. __func__);
  142. return 0;
  143. }
  144. }
  145. return command_line;
  146. }
  147. static void kexec_find_and_set_command_line(struct kimage *image)
  148. {
  149. kimage_entry_t *ptr, entry;
  150. unsigned char *command_line = 0;
  151. unsigned char *r;
  152. HV_Errno hverr;
  153. for (ptr = &image->head;
  154. (entry = *ptr) && !(entry & IND_DONE);
  155. ptr = (entry & IND_INDIRECTION) ?
  156. phys_to_virt((entry & PAGE_MASK)) : ptr + 1) {
  157. if ((entry & IND_SOURCE)) {
  158. void *va =
  159. kmap_atomic_pfn(entry >> PAGE_SHIFT);
  160. r = kexec_bn2cl(va);
  161. if (r) {
  162. command_line = r;
  163. break;
  164. }
  165. kunmap_atomic(va);
  166. }
  167. }
  168. if (command_line != 0) {
  169. pr_info("setting new command line to \"%s\"\n",
  170. command_line);
  171. hverr = hv_set_command_line(
  172. (HV_VirtAddr) command_line, strlen(command_line));
  173. kunmap_atomic(command_line);
  174. } else {
  175. pr_info("%s: no command line found; making empty\n",
  176. __func__);
  177. hverr = hv_set_command_line((HV_VirtAddr) command_line, 0);
  178. }
  179. if (hverr)
  180. pr_warning("%s: hv_set_command_line returned error: %d\n",
  181. __func__, hverr);
  182. }
  183. /*
  184. * The kexec code range-checks all its PAs, so to avoid having it run
  185. * amok and allocate memory and then sequester it from every other
  186. * controller, we force it to come from controller zero. We also
  187. * disable the oom-killer since if we do end up running out of memory,
  188. * that almost certainly won't help.
  189. */
  190. struct page *kimage_alloc_pages_arch(gfp_t gfp_mask, unsigned int order)
  191. {
  192. gfp_mask |= __GFP_THISNODE | __GFP_NORETRY;
  193. return alloc_pages_node(0, gfp_mask, order);
  194. }
  195. /*
  196. * Address range in which pa=va mapping is set in setup_quasi_va_is_pa().
  197. * For tilepro, PAGE_OFFSET is used since this is the largest possbile value
  198. * for tilepro, while for tilegx, we limit it to entire middle level page
  199. * table which we assume has been allocated and is undoubtedly large enough.
  200. */
  201. #ifndef __tilegx__
  202. #define QUASI_VA_IS_PA_ADDR_RANGE PAGE_OFFSET
  203. #else
  204. #define QUASI_VA_IS_PA_ADDR_RANGE PGDIR_SIZE
  205. #endif
  206. static void setup_quasi_va_is_pa(void)
  207. {
  208. HV_PTE pte;
  209. unsigned long i;
  210. /*
  211. * Flush our TLB to prevent conflicts between the previous contents
  212. * and the new stuff we're about to add.
  213. */
  214. local_flush_tlb_all();
  215. /*
  216. * setup VA is PA, at least up to QUASI_VA_IS_PA_ADDR_RANGE.
  217. * Note here we assume that level-1 page table is defined by
  218. * HPAGE_SIZE.
  219. */
  220. pte = hv_pte(_PAGE_KERNEL | _PAGE_HUGE_PAGE);
  221. pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_NO_L3);
  222. for (i = 0; i < (QUASI_VA_IS_PA_ADDR_RANGE >> HPAGE_SHIFT); i++) {
  223. unsigned long vaddr = i << HPAGE_SHIFT;
  224. pgd_t *pgd = pgd_offset(current->mm, vaddr);
  225. pud_t *pud = pud_offset(pgd, vaddr);
  226. pte_t *ptep = (pte_t *) pmd_offset(pud, vaddr);
  227. unsigned long pfn = i << (HPAGE_SHIFT - PAGE_SHIFT);
  228. if (pfn_valid(pfn))
  229. __set_pte(ptep, pfn_pte(pfn, pte));
  230. }
  231. }
  232. void machine_kexec(struct kimage *image)
  233. {
  234. void *reboot_code_buffer;
  235. pte_t *ptep;
  236. void (*rnk)(unsigned long, void *, unsigned long)
  237. __noreturn;
  238. /* Mask all interrupts before starting to reboot. */
  239. interrupt_mask_set_mask(~0ULL);
  240. kexec_find_and_set_command_line(image);
  241. /*
  242. * Adjust the home caching of the control page to be cached on
  243. * this cpu, and copy the assembly helper into the control
  244. * code page, which we map in the vmalloc area.
  245. */
  246. homecache_change_page_home(image->control_code_page, 0,
  247. smp_processor_id());
  248. reboot_code_buffer = page_address(image->control_code_page);
  249. BUG_ON(reboot_code_buffer == NULL);
  250. ptep = virt_to_pte(NULL, (unsigned long)reboot_code_buffer);
  251. __set_pte(ptep, pte_mkexec(*ptep));
  252. memcpy(reboot_code_buffer, relocate_new_kernel,
  253. relocate_new_kernel_size);
  254. __flush_icache_range(
  255. (unsigned long) reboot_code_buffer,
  256. (unsigned long) reboot_code_buffer + relocate_new_kernel_size);
  257. setup_quasi_va_is_pa();
  258. /* now call it */
  259. rnk = reboot_code_buffer;
  260. (*rnk)(image->head, reboot_code_buffer, image->start);
  261. }