machine_kexec.c 7.8 KB

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