kexec.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773
  1. /*
  2. * kexec.c - kexec system call
  3. * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
  4. *
  5. * This source code is licensed under the GNU General Public License,
  6. * Version 2. See the file COPYING for more details.
  7. */
  8. #define pr_fmt(fmt) "kexec: " fmt
  9. #include <linux/capability.h>
  10. #include <linux/mm.h>
  11. #include <linux/file.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include <linux/kexec.h>
  15. #include <linux/mutex.h>
  16. #include <linux/list.h>
  17. #include <linux/highmem.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/reboot.h>
  20. #include <linux/ioport.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/elf.h>
  23. #include <linux/elfcore.h>
  24. #include <linux/utsname.h>
  25. #include <linux/numa.h>
  26. #include <linux/suspend.h>
  27. #include <linux/device.h>
  28. #include <linux/freezer.h>
  29. #include <linux/pm.h>
  30. #include <linux/cpu.h>
  31. #include <linux/console.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/swap.h>
  34. #include <linux/syscore_ops.h>
  35. #include <linux/compiler.h>
  36. #include <linux/hugetlb.h>
  37. #include <asm/page.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/io.h>
  40. #include <asm/sections.h>
  41. #include <crypto/hash.h>
  42. #include <crypto/sha.h>
  43. /* Per cpu memory for storing cpu states in case of system crash. */
  44. note_buf_t __percpu *crash_notes;
  45. /* vmcoreinfo stuff */
  46. static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
  47. u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
  48. size_t vmcoreinfo_size;
  49. size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
  50. /* Flag to indicate we are going to kexec a new kernel */
  51. bool kexec_in_progress = false;
  52. /*
  53. * Declare these symbols weak so that if architecture provides a purgatory,
  54. * these will be overridden.
  55. */
  56. char __weak kexec_purgatory[0];
  57. size_t __weak kexec_purgatory_size = 0;
  58. static int kexec_calculate_store_digests(struct kimage *image);
  59. /* Location of the reserved area for the crash kernel */
  60. struct resource crashk_res = {
  61. .name = "Crash kernel",
  62. .start = 0,
  63. .end = 0,
  64. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  65. };
  66. struct resource crashk_low_res = {
  67. .name = "Crash kernel",
  68. .start = 0,
  69. .end = 0,
  70. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  71. };
  72. int kexec_should_crash(struct task_struct *p)
  73. {
  74. if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
  75. return 1;
  76. return 0;
  77. }
  78. /*
  79. * When kexec transitions to the new kernel there is a one-to-one
  80. * mapping between physical and virtual addresses. On processors
  81. * where you can disable the MMU this is trivial, and easy. For
  82. * others it is still a simple predictable page table to setup.
  83. *
  84. * In that environment kexec copies the new kernel to its final
  85. * resting place. This means I can only support memory whose
  86. * physical address can fit in an unsigned long. In particular
  87. * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
  88. * If the assembly stub has more restrictive requirements
  89. * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
  90. * defined more restrictively in <asm/kexec.h>.
  91. *
  92. * The code for the transition from the current kernel to the
  93. * the new kernel is placed in the control_code_buffer, whose size
  94. * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
  95. * page of memory is necessary, but some architectures require more.
  96. * Because this memory must be identity mapped in the transition from
  97. * virtual to physical addresses it must live in the range
  98. * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
  99. * modifiable.
  100. *
  101. * The assembly stub in the control code buffer is passed a linked list
  102. * of descriptor pages detailing the source pages of the new kernel,
  103. * and the destination addresses of those source pages. As this data
  104. * structure is not used in the context of the current OS, it must
  105. * be self-contained.
  106. *
  107. * The code has been made to work with highmem pages and will use a
  108. * destination page in its final resting place (if it happens
  109. * to allocate it). The end product of this is that most of the
  110. * physical address space, and most of RAM can be used.
  111. *
  112. * Future directions include:
  113. * - allocating a page table with the control code buffer identity
  114. * mapped, to simplify machine_kexec and make kexec_on_panic more
  115. * reliable.
  116. */
  117. /*
  118. * KIMAGE_NO_DEST is an impossible destination address..., for
  119. * allocating pages whose destination address we do not care about.
  120. */
  121. #define KIMAGE_NO_DEST (-1UL)
  122. static int kimage_is_destination_range(struct kimage *image,
  123. unsigned long start, unsigned long end);
  124. static struct page *kimage_alloc_page(struct kimage *image,
  125. gfp_t gfp_mask,
  126. unsigned long dest);
  127. static int copy_user_segment_list(struct kimage *image,
  128. unsigned long nr_segments,
  129. struct kexec_segment __user *segments)
  130. {
  131. int ret;
  132. size_t segment_bytes;
  133. /* Read in the segments */
  134. image->nr_segments = nr_segments;
  135. segment_bytes = nr_segments * sizeof(*segments);
  136. ret = copy_from_user(image->segment, segments, segment_bytes);
  137. if (ret)
  138. ret = -EFAULT;
  139. return ret;
  140. }
  141. static int sanity_check_segment_list(struct kimage *image)
  142. {
  143. int result, i;
  144. unsigned long nr_segments = image->nr_segments;
  145. /*
  146. * Verify we have good destination addresses. The caller is
  147. * responsible for making certain we don't attempt to load
  148. * the new image into invalid or reserved areas of RAM. This
  149. * just verifies it is an address we can use.
  150. *
  151. * Since the kernel does everything in page size chunks ensure
  152. * the destination addresses are page aligned. Too many
  153. * special cases crop of when we don't do this. The most
  154. * insidious is getting overlapping destination addresses
  155. * simply because addresses are changed to page size
  156. * granularity.
  157. */
  158. result = -EADDRNOTAVAIL;
  159. for (i = 0; i < nr_segments; i++) {
  160. unsigned long mstart, mend;
  161. mstart = image->segment[i].mem;
  162. mend = mstart + image->segment[i].memsz;
  163. if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
  164. return result;
  165. if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
  166. return result;
  167. }
  168. /* Verify our destination addresses do not overlap.
  169. * If we alloed overlapping destination addresses
  170. * through very weird things can happen with no
  171. * easy explanation as one segment stops on another.
  172. */
  173. result = -EINVAL;
  174. for (i = 0; i < nr_segments; i++) {
  175. unsigned long mstart, mend;
  176. unsigned long j;
  177. mstart = image->segment[i].mem;
  178. mend = mstart + image->segment[i].memsz;
  179. for (j = 0; j < i; j++) {
  180. unsigned long pstart, pend;
  181. pstart = image->segment[j].mem;
  182. pend = pstart + image->segment[j].memsz;
  183. /* Do the segments overlap ? */
  184. if ((mend > pstart) && (mstart < pend))
  185. return result;
  186. }
  187. }
  188. /* Ensure our buffer sizes are strictly less than
  189. * our memory sizes. This should always be the case,
  190. * and it is easier to check up front than to be surprised
  191. * later on.
  192. */
  193. result = -EINVAL;
  194. for (i = 0; i < nr_segments; i++) {
  195. if (image->segment[i].bufsz > image->segment[i].memsz)
  196. return result;
  197. }
  198. /*
  199. * Verify we have good destination addresses. Normally
  200. * the caller is responsible for making certain we don't
  201. * attempt to load the new image into invalid or reserved
  202. * areas of RAM. But crash kernels are preloaded into a
  203. * reserved area of ram. We must ensure the addresses
  204. * are in the reserved area otherwise preloading the
  205. * kernel could corrupt things.
  206. */
  207. if (image->type == KEXEC_TYPE_CRASH) {
  208. result = -EADDRNOTAVAIL;
  209. for (i = 0; i < nr_segments; i++) {
  210. unsigned long mstart, mend;
  211. mstart = image->segment[i].mem;
  212. mend = mstart + image->segment[i].memsz - 1;
  213. /* Ensure we are within the crash kernel limits */
  214. if ((mstart < crashk_res.start) ||
  215. (mend > crashk_res.end))
  216. return result;
  217. }
  218. }
  219. return 0;
  220. }
  221. static struct kimage *do_kimage_alloc_init(void)
  222. {
  223. struct kimage *image;
  224. /* Allocate a controlling structure */
  225. image = kzalloc(sizeof(*image), GFP_KERNEL);
  226. if (!image)
  227. return NULL;
  228. image->head = 0;
  229. image->entry = &image->head;
  230. image->last_entry = &image->head;
  231. image->control_page = ~0; /* By default this does not apply */
  232. image->type = KEXEC_TYPE_DEFAULT;
  233. /* Initialize the list of control pages */
  234. INIT_LIST_HEAD(&image->control_pages);
  235. /* Initialize the list of destination pages */
  236. INIT_LIST_HEAD(&image->dest_pages);
  237. /* Initialize the list of unusable pages */
  238. INIT_LIST_HEAD(&image->unusable_pages);
  239. return image;
  240. }
  241. static void kimage_free_page_list(struct list_head *list);
  242. static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
  243. unsigned long nr_segments,
  244. struct kexec_segment __user *segments,
  245. unsigned long flags)
  246. {
  247. int ret;
  248. struct kimage *image;
  249. bool kexec_on_panic = flags & KEXEC_ON_CRASH;
  250. if (kexec_on_panic) {
  251. /* Verify we have a valid entry point */
  252. if ((entry < crashk_res.start) || (entry > crashk_res.end))
  253. return -EADDRNOTAVAIL;
  254. }
  255. /* Allocate and initialize a controlling structure */
  256. image = do_kimage_alloc_init();
  257. if (!image)
  258. return -ENOMEM;
  259. image->start = entry;
  260. ret = copy_user_segment_list(image, nr_segments, segments);
  261. if (ret)
  262. goto out_free_image;
  263. ret = sanity_check_segment_list(image);
  264. if (ret)
  265. goto out_free_image;
  266. /* Enable the special crash kernel control page allocation policy. */
  267. if (kexec_on_panic) {
  268. image->control_page = crashk_res.start;
  269. image->type = KEXEC_TYPE_CRASH;
  270. }
  271. /*
  272. * Find a location for the control code buffer, and add it
  273. * the vector of segments so that it's pages will also be
  274. * counted as destination pages.
  275. */
  276. ret = -ENOMEM;
  277. image->control_code_page = kimage_alloc_control_pages(image,
  278. get_order(KEXEC_CONTROL_PAGE_SIZE));
  279. if (!image->control_code_page) {
  280. pr_err("Could not allocate control_code_buffer\n");
  281. goto out_free_image;
  282. }
  283. if (!kexec_on_panic) {
  284. image->swap_page = kimage_alloc_control_pages(image, 0);
  285. if (!image->swap_page) {
  286. pr_err("Could not allocate swap buffer\n");
  287. goto out_free_control_pages;
  288. }
  289. }
  290. *rimage = image;
  291. return 0;
  292. out_free_control_pages:
  293. kimage_free_page_list(&image->control_pages);
  294. out_free_image:
  295. kfree(image);
  296. return ret;
  297. }
  298. static int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len)
  299. {
  300. struct fd f = fdget(fd);
  301. int ret;
  302. struct kstat stat;
  303. loff_t pos;
  304. ssize_t bytes = 0;
  305. if (!f.file)
  306. return -EBADF;
  307. ret = vfs_getattr(&f.file->f_path, &stat);
  308. if (ret)
  309. goto out;
  310. if (stat.size > INT_MAX) {
  311. ret = -EFBIG;
  312. goto out;
  313. }
  314. /* Don't hand 0 to vmalloc, it whines. */
  315. if (stat.size == 0) {
  316. ret = -EINVAL;
  317. goto out;
  318. }
  319. *buf = vmalloc(stat.size);
  320. if (!*buf) {
  321. ret = -ENOMEM;
  322. goto out;
  323. }
  324. pos = 0;
  325. while (pos < stat.size) {
  326. bytes = kernel_read(f.file, pos, (char *)(*buf) + pos,
  327. stat.size - pos);
  328. if (bytes < 0) {
  329. vfree(*buf);
  330. ret = bytes;
  331. goto out;
  332. }
  333. if (bytes == 0)
  334. break;
  335. pos += bytes;
  336. }
  337. if (pos != stat.size) {
  338. ret = -EBADF;
  339. vfree(*buf);
  340. goto out;
  341. }
  342. *buf_len = pos;
  343. out:
  344. fdput(f);
  345. return ret;
  346. }
  347. /* Architectures can provide this probe function */
  348. int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
  349. unsigned long buf_len)
  350. {
  351. return -ENOEXEC;
  352. }
  353. void * __weak arch_kexec_kernel_image_load(struct kimage *image)
  354. {
  355. return ERR_PTR(-ENOEXEC);
  356. }
  357. void __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
  358. {
  359. }
  360. int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
  361. unsigned long buf_len)
  362. {
  363. return -EKEYREJECTED;
  364. }
  365. /* Apply relocations of type RELA */
  366. int __weak
  367. arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  368. unsigned int relsec)
  369. {
  370. pr_err("RELA relocation unsupported.\n");
  371. return -ENOEXEC;
  372. }
  373. /* Apply relocations of type REL */
  374. int __weak
  375. arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  376. unsigned int relsec)
  377. {
  378. pr_err("REL relocation unsupported.\n");
  379. return -ENOEXEC;
  380. }
  381. /*
  382. * Free up memory used by kernel, initrd, and comand line. This is temporary
  383. * memory allocation which is not needed any more after these buffers have
  384. * been loaded into separate segments and have been copied elsewhere.
  385. */
  386. static void kimage_file_post_load_cleanup(struct kimage *image)
  387. {
  388. struct purgatory_info *pi = &image->purgatory_info;
  389. vfree(image->kernel_buf);
  390. image->kernel_buf = NULL;
  391. vfree(image->initrd_buf);
  392. image->initrd_buf = NULL;
  393. kfree(image->cmdline_buf);
  394. image->cmdline_buf = NULL;
  395. vfree(pi->purgatory_buf);
  396. pi->purgatory_buf = NULL;
  397. vfree(pi->sechdrs);
  398. pi->sechdrs = NULL;
  399. /* See if architecture has anything to cleanup post load */
  400. arch_kimage_file_post_load_cleanup(image);
  401. /*
  402. * Above call should have called into bootloader to free up
  403. * any data stored in kimage->image_loader_data. It should
  404. * be ok now to free it up.
  405. */
  406. kfree(image->image_loader_data);
  407. image->image_loader_data = NULL;
  408. }
  409. /*
  410. * In file mode list of segments is prepared by kernel. Copy relevant
  411. * data from user space, do error checking, prepare segment list
  412. */
  413. static int
  414. kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
  415. const char __user *cmdline_ptr,
  416. unsigned long cmdline_len, unsigned flags)
  417. {
  418. int ret = 0;
  419. void *ldata;
  420. ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,
  421. &image->kernel_buf_len);
  422. if (ret)
  423. return ret;
  424. /* Call arch image probe handlers */
  425. ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
  426. image->kernel_buf_len);
  427. if (ret)
  428. goto out;
  429. #ifdef CONFIG_KEXEC_VERIFY_SIG
  430. ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
  431. image->kernel_buf_len);
  432. if (ret) {
  433. pr_debug("kernel signature verification failed.\n");
  434. goto out;
  435. }
  436. pr_debug("kernel signature verification successful.\n");
  437. #endif
  438. /* It is possible that there no initramfs is being loaded */
  439. if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
  440. ret = copy_file_from_fd(initrd_fd, &image->initrd_buf,
  441. &image->initrd_buf_len);
  442. if (ret)
  443. goto out;
  444. }
  445. if (cmdline_len) {
  446. image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
  447. if (!image->cmdline_buf) {
  448. ret = -ENOMEM;
  449. goto out;
  450. }
  451. ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
  452. cmdline_len);
  453. if (ret) {
  454. ret = -EFAULT;
  455. goto out;
  456. }
  457. image->cmdline_buf_len = cmdline_len;
  458. /* command line should be a string with last byte null */
  459. if (image->cmdline_buf[cmdline_len - 1] != '\0') {
  460. ret = -EINVAL;
  461. goto out;
  462. }
  463. }
  464. /* Call arch image load handlers */
  465. ldata = arch_kexec_kernel_image_load(image);
  466. if (IS_ERR(ldata)) {
  467. ret = PTR_ERR(ldata);
  468. goto out;
  469. }
  470. image->image_loader_data = ldata;
  471. out:
  472. /* In case of error, free up all allocated memory in this function */
  473. if (ret)
  474. kimage_file_post_load_cleanup(image);
  475. return ret;
  476. }
  477. static int
  478. kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
  479. int initrd_fd, const char __user *cmdline_ptr,
  480. unsigned long cmdline_len, unsigned long flags)
  481. {
  482. int ret;
  483. struct kimage *image;
  484. bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
  485. image = do_kimage_alloc_init();
  486. if (!image)
  487. return -ENOMEM;
  488. image->file_mode = 1;
  489. if (kexec_on_panic) {
  490. /* Enable special crash kernel control page alloc policy. */
  491. image->control_page = crashk_res.start;
  492. image->type = KEXEC_TYPE_CRASH;
  493. }
  494. ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
  495. cmdline_ptr, cmdline_len, flags);
  496. if (ret)
  497. goto out_free_image;
  498. ret = sanity_check_segment_list(image);
  499. if (ret)
  500. goto out_free_post_load_bufs;
  501. ret = -ENOMEM;
  502. image->control_code_page = kimage_alloc_control_pages(image,
  503. get_order(KEXEC_CONTROL_PAGE_SIZE));
  504. if (!image->control_code_page) {
  505. pr_err("Could not allocate control_code_buffer\n");
  506. goto out_free_post_load_bufs;
  507. }
  508. if (!kexec_on_panic) {
  509. image->swap_page = kimage_alloc_control_pages(image, 0);
  510. if (!image->swap_page) {
  511. pr_err(KERN_ERR "Could not allocate swap buffer\n");
  512. goto out_free_control_pages;
  513. }
  514. }
  515. *rimage = image;
  516. return 0;
  517. out_free_control_pages:
  518. kimage_free_page_list(&image->control_pages);
  519. out_free_post_load_bufs:
  520. kimage_file_post_load_cleanup(image);
  521. out_free_image:
  522. kfree(image);
  523. return ret;
  524. }
  525. static int kimage_is_destination_range(struct kimage *image,
  526. unsigned long start,
  527. unsigned long end)
  528. {
  529. unsigned long i;
  530. for (i = 0; i < image->nr_segments; i++) {
  531. unsigned long mstart, mend;
  532. mstart = image->segment[i].mem;
  533. mend = mstart + image->segment[i].memsz;
  534. if ((end > mstart) && (start < mend))
  535. return 1;
  536. }
  537. return 0;
  538. }
  539. static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
  540. {
  541. struct page *pages;
  542. pages = alloc_pages(gfp_mask, order);
  543. if (pages) {
  544. unsigned int count, i;
  545. pages->mapping = NULL;
  546. set_page_private(pages, order);
  547. count = 1 << order;
  548. for (i = 0; i < count; i++)
  549. SetPageReserved(pages + i);
  550. }
  551. return pages;
  552. }
  553. static void kimage_free_pages(struct page *page)
  554. {
  555. unsigned int order, count, i;
  556. order = page_private(page);
  557. count = 1 << order;
  558. for (i = 0; i < count; i++)
  559. ClearPageReserved(page + i);
  560. __free_pages(page, order);
  561. }
  562. static void kimage_free_page_list(struct list_head *list)
  563. {
  564. struct list_head *pos, *next;
  565. list_for_each_safe(pos, next, list) {
  566. struct page *page;
  567. page = list_entry(pos, struct page, lru);
  568. list_del(&page->lru);
  569. kimage_free_pages(page);
  570. }
  571. }
  572. static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
  573. unsigned int order)
  574. {
  575. /* Control pages are special, they are the intermediaries
  576. * that are needed while we copy the rest of the pages
  577. * to their final resting place. As such they must
  578. * not conflict with either the destination addresses
  579. * or memory the kernel is already using.
  580. *
  581. * The only case where we really need more than one of
  582. * these are for architectures where we cannot disable
  583. * the MMU and must instead generate an identity mapped
  584. * page table for all of the memory.
  585. *
  586. * At worst this runs in O(N) of the image size.
  587. */
  588. struct list_head extra_pages;
  589. struct page *pages;
  590. unsigned int count;
  591. count = 1 << order;
  592. INIT_LIST_HEAD(&extra_pages);
  593. /* Loop while I can allocate a page and the page allocated
  594. * is a destination page.
  595. */
  596. do {
  597. unsigned long pfn, epfn, addr, eaddr;
  598. pages = kimage_alloc_pages(GFP_KERNEL, order);
  599. if (!pages)
  600. break;
  601. pfn = page_to_pfn(pages);
  602. epfn = pfn + count;
  603. addr = pfn << PAGE_SHIFT;
  604. eaddr = epfn << PAGE_SHIFT;
  605. if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
  606. kimage_is_destination_range(image, addr, eaddr)) {
  607. list_add(&pages->lru, &extra_pages);
  608. pages = NULL;
  609. }
  610. } while (!pages);
  611. if (pages) {
  612. /* Remember the allocated page... */
  613. list_add(&pages->lru, &image->control_pages);
  614. /* Because the page is already in it's destination
  615. * location we will never allocate another page at
  616. * that address. Therefore kimage_alloc_pages
  617. * will not return it (again) and we don't need
  618. * to give it an entry in image->segment[].
  619. */
  620. }
  621. /* Deal with the destination pages I have inadvertently allocated.
  622. *
  623. * Ideally I would convert multi-page allocations into single
  624. * page allocations, and add everything to image->dest_pages.
  625. *
  626. * For now it is simpler to just free the pages.
  627. */
  628. kimage_free_page_list(&extra_pages);
  629. return pages;
  630. }
  631. static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
  632. unsigned int order)
  633. {
  634. /* Control pages are special, they are the intermediaries
  635. * that are needed while we copy the rest of the pages
  636. * to their final resting place. As such they must
  637. * not conflict with either the destination addresses
  638. * or memory the kernel is already using.
  639. *
  640. * Control pages are also the only pags we must allocate
  641. * when loading a crash kernel. All of the other pages
  642. * are specified by the segments and we just memcpy
  643. * into them directly.
  644. *
  645. * The only case where we really need more than one of
  646. * these are for architectures where we cannot disable
  647. * the MMU and must instead generate an identity mapped
  648. * page table for all of the memory.
  649. *
  650. * Given the low demand this implements a very simple
  651. * allocator that finds the first hole of the appropriate
  652. * size in the reserved memory region, and allocates all
  653. * of the memory up to and including the hole.
  654. */
  655. unsigned long hole_start, hole_end, size;
  656. struct page *pages;
  657. pages = NULL;
  658. size = (1 << order) << PAGE_SHIFT;
  659. hole_start = (image->control_page + (size - 1)) & ~(size - 1);
  660. hole_end = hole_start + size - 1;
  661. while (hole_end <= crashk_res.end) {
  662. unsigned long i;
  663. if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
  664. break;
  665. /* See if I overlap any of the segments */
  666. for (i = 0; i < image->nr_segments; i++) {
  667. unsigned long mstart, mend;
  668. mstart = image->segment[i].mem;
  669. mend = mstart + image->segment[i].memsz - 1;
  670. if ((hole_end >= mstart) && (hole_start <= mend)) {
  671. /* Advance the hole to the end of the segment */
  672. hole_start = (mend + (size - 1)) & ~(size - 1);
  673. hole_end = hole_start + size - 1;
  674. break;
  675. }
  676. }
  677. /* If I don't overlap any segments I have found my hole! */
  678. if (i == image->nr_segments) {
  679. pages = pfn_to_page(hole_start >> PAGE_SHIFT);
  680. break;
  681. }
  682. }
  683. if (pages)
  684. image->control_page = hole_end;
  685. return pages;
  686. }
  687. struct page *kimage_alloc_control_pages(struct kimage *image,
  688. unsigned int order)
  689. {
  690. struct page *pages = NULL;
  691. switch (image->type) {
  692. case KEXEC_TYPE_DEFAULT:
  693. pages = kimage_alloc_normal_control_pages(image, order);
  694. break;
  695. case KEXEC_TYPE_CRASH:
  696. pages = kimage_alloc_crash_control_pages(image, order);
  697. break;
  698. }
  699. return pages;
  700. }
  701. static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
  702. {
  703. if (*image->entry != 0)
  704. image->entry++;
  705. if (image->entry == image->last_entry) {
  706. kimage_entry_t *ind_page;
  707. struct page *page;
  708. page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
  709. if (!page)
  710. return -ENOMEM;
  711. ind_page = page_address(page);
  712. *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
  713. image->entry = ind_page;
  714. image->last_entry = ind_page +
  715. ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
  716. }
  717. *image->entry = entry;
  718. image->entry++;
  719. *image->entry = 0;
  720. return 0;
  721. }
  722. static int kimage_set_destination(struct kimage *image,
  723. unsigned long destination)
  724. {
  725. int result;
  726. destination &= PAGE_MASK;
  727. result = kimage_add_entry(image, destination | IND_DESTINATION);
  728. if (result == 0)
  729. image->destination = destination;
  730. return result;
  731. }
  732. static int kimage_add_page(struct kimage *image, unsigned long page)
  733. {
  734. int result;
  735. page &= PAGE_MASK;
  736. result = kimage_add_entry(image, page | IND_SOURCE);
  737. if (result == 0)
  738. image->destination += PAGE_SIZE;
  739. return result;
  740. }
  741. static void kimage_free_extra_pages(struct kimage *image)
  742. {
  743. /* Walk through and free any extra destination pages I may have */
  744. kimage_free_page_list(&image->dest_pages);
  745. /* Walk through and free any unusable pages I have cached */
  746. kimage_free_page_list(&image->unusable_pages);
  747. }
  748. static void kimage_terminate(struct kimage *image)
  749. {
  750. if (*image->entry != 0)
  751. image->entry++;
  752. *image->entry = IND_DONE;
  753. }
  754. #define for_each_kimage_entry(image, ptr, entry) \
  755. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  756. ptr = (entry & IND_INDIRECTION) ? \
  757. phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
  758. static void kimage_free_entry(kimage_entry_t entry)
  759. {
  760. struct page *page;
  761. page = pfn_to_page(entry >> PAGE_SHIFT);
  762. kimage_free_pages(page);
  763. }
  764. static void kimage_free(struct kimage *image)
  765. {
  766. kimage_entry_t *ptr, entry;
  767. kimage_entry_t ind = 0;
  768. if (!image)
  769. return;
  770. kimage_free_extra_pages(image);
  771. for_each_kimage_entry(image, ptr, entry) {
  772. if (entry & IND_INDIRECTION) {
  773. /* Free the previous indirection page */
  774. if (ind & IND_INDIRECTION)
  775. kimage_free_entry(ind);
  776. /* Save this indirection page until we are
  777. * done with it.
  778. */
  779. ind = entry;
  780. } else if (entry & IND_SOURCE)
  781. kimage_free_entry(entry);
  782. }
  783. /* Free the final indirection page */
  784. if (ind & IND_INDIRECTION)
  785. kimage_free_entry(ind);
  786. /* Handle any machine specific cleanup */
  787. machine_kexec_cleanup(image);
  788. /* Free the kexec control pages... */
  789. kimage_free_page_list(&image->control_pages);
  790. /*
  791. * Free up any temporary buffers allocated. This might hit if
  792. * error occurred much later after buffer allocation.
  793. */
  794. if (image->file_mode)
  795. kimage_file_post_load_cleanup(image);
  796. kfree(image);
  797. }
  798. static kimage_entry_t *kimage_dst_used(struct kimage *image,
  799. unsigned long page)
  800. {
  801. kimage_entry_t *ptr, entry;
  802. unsigned long destination = 0;
  803. for_each_kimage_entry(image, ptr, entry) {
  804. if (entry & IND_DESTINATION)
  805. destination = entry & PAGE_MASK;
  806. else if (entry & IND_SOURCE) {
  807. if (page == destination)
  808. return ptr;
  809. destination += PAGE_SIZE;
  810. }
  811. }
  812. return NULL;
  813. }
  814. static struct page *kimage_alloc_page(struct kimage *image,
  815. gfp_t gfp_mask,
  816. unsigned long destination)
  817. {
  818. /*
  819. * Here we implement safeguards to ensure that a source page
  820. * is not copied to its destination page before the data on
  821. * the destination page is no longer useful.
  822. *
  823. * To do this we maintain the invariant that a source page is
  824. * either its own destination page, or it is not a
  825. * destination page at all.
  826. *
  827. * That is slightly stronger than required, but the proof
  828. * that no problems will not occur is trivial, and the
  829. * implementation is simply to verify.
  830. *
  831. * When allocating all pages normally this algorithm will run
  832. * in O(N) time, but in the worst case it will run in O(N^2)
  833. * time. If the runtime is a problem the data structures can
  834. * be fixed.
  835. */
  836. struct page *page;
  837. unsigned long addr;
  838. /*
  839. * Walk through the list of destination pages, and see if I
  840. * have a match.
  841. */
  842. list_for_each_entry(page, &image->dest_pages, lru) {
  843. addr = page_to_pfn(page) << PAGE_SHIFT;
  844. if (addr == destination) {
  845. list_del(&page->lru);
  846. return page;
  847. }
  848. }
  849. page = NULL;
  850. while (1) {
  851. kimage_entry_t *old;
  852. /* Allocate a page, if we run out of memory give up */
  853. page = kimage_alloc_pages(gfp_mask, 0);
  854. if (!page)
  855. return NULL;
  856. /* If the page cannot be used file it away */
  857. if (page_to_pfn(page) >
  858. (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
  859. list_add(&page->lru, &image->unusable_pages);
  860. continue;
  861. }
  862. addr = page_to_pfn(page) << PAGE_SHIFT;
  863. /* If it is the destination page we want use it */
  864. if (addr == destination)
  865. break;
  866. /* If the page is not a destination page use it */
  867. if (!kimage_is_destination_range(image, addr,
  868. addr + PAGE_SIZE))
  869. break;
  870. /*
  871. * I know that the page is someones destination page.
  872. * See if there is already a source page for this
  873. * destination page. And if so swap the source pages.
  874. */
  875. old = kimage_dst_used(image, addr);
  876. if (old) {
  877. /* If so move it */
  878. unsigned long old_addr;
  879. struct page *old_page;
  880. old_addr = *old & PAGE_MASK;
  881. old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
  882. copy_highpage(page, old_page);
  883. *old = addr | (*old & ~PAGE_MASK);
  884. /* The old page I have found cannot be a
  885. * destination page, so return it if it's
  886. * gfp_flags honor the ones passed in.
  887. */
  888. if (!(gfp_mask & __GFP_HIGHMEM) &&
  889. PageHighMem(old_page)) {
  890. kimage_free_pages(old_page);
  891. continue;
  892. }
  893. addr = old_addr;
  894. page = old_page;
  895. break;
  896. } else {
  897. /* Place the page on the destination list I
  898. * will use it later.
  899. */
  900. list_add(&page->lru, &image->dest_pages);
  901. }
  902. }
  903. return page;
  904. }
  905. static int kimage_load_normal_segment(struct kimage *image,
  906. struct kexec_segment *segment)
  907. {
  908. unsigned long maddr;
  909. size_t ubytes, mbytes;
  910. int result;
  911. unsigned char __user *buf = NULL;
  912. unsigned char *kbuf = NULL;
  913. result = 0;
  914. if (image->file_mode)
  915. kbuf = segment->kbuf;
  916. else
  917. buf = segment->buf;
  918. ubytes = segment->bufsz;
  919. mbytes = segment->memsz;
  920. maddr = segment->mem;
  921. result = kimage_set_destination(image, maddr);
  922. if (result < 0)
  923. goto out;
  924. while (mbytes) {
  925. struct page *page;
  926. char *ptr;
  927. size_t uchunk, mchunk;
  928. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  929. if (!page) {
  930. result = -ENOMEM;
  931. goto out;
  932. }
  933. result = kimage_add_page(image, page_to_pfn(page)
  934. << PAGE_SHIFT);
  935. if (result < 0)
  936. goto out;
  937. ptr = kmap(page);
  938. /* Start with a clear page */
  939. clear_page(ptr);
  940. ptr += maddr & ~PAGE_MASK;
  941. mchunk = min_t(size_t, mbytes,
  942. PAGE_SIZE - (maddr & ~PAGE_MASK));
  943. uchunk = min(ubytes, mchunk);
  944. /* For file based kexec, source pages are in kernel memory */
  945. if (image->file_mode)
  946. memcpy(ptr, kbuf, uchunk);
  947. else
  948. result = copy_from_user(ptr, buf, uchunk);
  949. kunmap(page);
  950. if (result) {
  951. result = -EFAULT;
  952. goto out;
  953. }
  954. ubytes -= uchunk;
  955. maddr += mchunk;
  956. if (image->file_mode)
  957. kbuf += mchunk;
  958. else
  959. buf += mchunk;
  960. mbytes -= mchunk;
  961. }
  962. out:
  963. return result;
  964. }
  965. static int kimage_load_crash_segment(struct kimage *image,
  966. struct kexec_segment *segment)
  967. {
  968. /* For crash dumps kernels we simply copy the data from
  969. * user space to it's destination.
  970. * We do things a page at a time for the sake of kmap.
  971. */
  972. unsigned long maddr;
  973. size_t ubytes, mbytes;
  974. int result;
  975. unsigned char __user *buf = NULL;
  976. unsigned char *kbuf = NULL;
  977. result = 0;
  978. if (image->file_mode)
  979. kbuf = segment->kbuf;
  980. else
  981. buf = segment->buf;
  982. ubytes = segment->bufsz;
  983. mbytes = segment->memsz;
  984. maddr = segment->mem;
  985. while (mbytes) {
  986. struct page *page;
  987. char *ptr;
  988. size_t uchunk, mchunk;
  989. page = pfn_to_page(maddr >> PAGE_SHIFT);
  990. if (!page) {
  991. result = -ENOMEM;
  992. goto out;
  993. }
  994. ptr = kmap(page);
  995. ptr += maddr & ~PAGE_MASK;
  996. mchunk = min_t(size_t, mbytes,
  997. PAGE_SIZE - (maddr & ~PAGE_MASK));
  998. uchunk = min(ubytes, mchunk);
  999. if (mchunk > uchunk) {
  1000. /* Zero the trailing part of the page */
  1001. memset(ptr + uchunk, 0, mchunk - uchunk);
  1002. }
  1003. /* For file based kexec, source pages are in kernel memory */
  1004. if (image->file_mode)
  1005. memcpy(ptr, kbuf, uchunk);
  1006. else
  1007. result = copy_from_user(ptr, buf, uchunk);
  1008. kexec_flush_icache_page(page);
  1009. kunmap(page);
  1010. if (result) {
  1011. result = -EFAULT;
  1012. goto out;
  1013. }
  1014. ubytes -= uchunk;
  1015. maddr += mchunk;
  1016. if (image->file_mode)
  1017. kbuf += mchunk;
  1018. else
  1019. buf += mchunk;
  1020. mbytes -= mchunk;
  1021. }
  1022. out:
  1023. return result;
  1024. }
  1025. static int kimage_load_segment(struct kimage *image,
  1026. struct kexec_segment *segment)
  1027. {
  1028. int result = -ENOMEM;
  1029. switch (image->type) {
  1030. case KEXEC_TYPE_DEFAULT:
  1031. result = kimage_load_normal_segment(image, segment);
  1032. break;
  1033. case KEXEC_TYPE_CRASH:
  1034. result = kimage_load_crash_segment(image, segment);
  1035. break;
  1036. }
  1037. return result;
  1038. }
  1039. /*
  1040. * Exec Kernel system call: for obvious reasons only root may call it.
  1041. *
  1042. * This call breaks up into three pieces.
  1043. * - A generic part which loads the new kernel from the current
  1044. * address space, and very carefully places the data in the
  1045. * allocated pages.
  1046. *
  1047. * - A generic part that interacts with the kernel and tells all of
  1048. * the devices to shut down. Preventing on-going dmas, and placing
  1049. * the devices in a consistent state so a later kernel can
  1050. * reinitialize them.
  1051. *
  1052. * - A machine specific part that includes the syscall number
  1053. * and then copies the image to it's final destination. And
  1054. * jumps into the image at entry.
  1055. *
  1056. * kexec does not sync, or unmount filesystems so if you need
  1057. * that to happen you need to do that yourself.
  1058. */
  1059. struct kimage *kexec_image;
  1060. struct kimage *kexec_crash_image;
  1061. int kexec_load_disabled;
  1062. static DEFINE_MUTEX(kexec_mutex);
  1063. SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
  1064. struct kexec_segment __user *, segments, unsigned long, flags)
  1065. {
  1066. struct kimage **dest_image, *image;
  1067. int result;
  1068. /* We only trust the superuser with rebooting the system. */
  1069. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  1070. return -EPERM;
  1071. /*
  1072. * Verify we have a legal set of flags
  1073. * This leaves us room for future extensions.
  1074. */
  1075. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  1076. return -EINVAL;
  1077. /* Verify we are on the appropriate architecture */
  1078. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  1079. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  1080. return -EINVAL;
  1081. /* Put an artificial cap on the number
  1082. * of segments passed to kexec_load.
  1083. */
  1084. if (nr_segments > KEXEC_SEGMENT_MAX)
  1085. return -EINVAL;
  1086. image = NULL;
  1087. result = 0;
  1088. /* Because we write directly to the reserved memory
  1089. * region when loading crash kernels we need a mutex here to
  1090. * prevent multiple crash kernels from attempting to load
  1091. * simultaneously, and to prevent a crash kernel from loading
  1092. * over the top of a in use crash kernel.
  1093. *
  1094. * KISS: always take the mutex.
  1095. */
  1096. if (!mutex_trylock(&kexec_mutex))
  1097. return -EBUSY;
  1098. dest_image = &kexec_image;
  1099. if (flags & KEXEC_ON_CRASH)
  1100. dest_image = &kexec_crash_image;
  1101. if (nr_segments > 0) {
  1102. unsigned long i;
  1103. /* Loading another kernel to reboot into */
  1104. if ((flags & KEXEC_ON_CRASH) == 0)
  1105. result = kimage_alloc_init(&image, entry, nr_segments,
  1106. segments, flags);
  1107. /* Loading another kernel to switch to if this one crashes */
  1108. else if (flags & KEXEC_ON_CRASH) {
  1109. /* Free any current crash dump kernel before
  1110. * we corrupt it.
  1111. */
  1112. kimage_free(xchg(&kexec_crash_image, NULL));
  1113. result = kimage_alloc_init(&image, entry, nr_segments,
  1114. segments, flags);
  1115. crash_map_reserved_pages();
  1116. }
  1117. if (result)
  1118. goto out;
  1119. if (flags & KEXEC_PRESERVE_CONTEXT)
  1120. image->preserve_context = 1;
  1121. result = machine_kexec_prepare(image);
  1122. if (result)
  1123. goto out;
  1124. for (i = 0; i < nr_segments; i++) {
  1125. result = kimage_load_segment(image, &image->segment[i]);
  1126. if (result)
  1127. goto out;
  1128. }
  1129. kimage_terminate(image);
  1130. if (flags & KEXEC_ON_CRASH)
  1131. crash_unmap_reserved_pages();
  1132. }
  1133. /* Install the new kernel, and Uninstall the old */
  1134. image = xchg(dest_image, image);
  1135. out:
  1136. mutex_unlock(&kexec_mutex);
  1137. kimage_free(image);
  1138. return result;
  1139. }
  1140. /*
  1141. * Add and remove page tables for crashkernel memory
  1142. *
  1143. * Provide an empty default implementation here -- architecture
  1144. * code may override this
  1145. */
  1146. void __weak crash_map_reserved_pages(void)
  1147. {}
  1148. void __weak crash_unmap_reserved_pages(void)
  1149. {}
  1150. #ifdef CONFIG_COMPAT
  1151. COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
  1152. compat_ulong_t, nr_segments,
  1153. struct compat_kexec_segment __user *, segments,
  1154. compat_ulong_t, flags)
  1155. {
  1156. struct compat_kexec_segment in;
  1157. struct kexec_segment out, __user *ksegments;
  1158. unsigned long i, result;
  1159. /* Don't allow clients that don't understand the native
  1160. * architecture to do anything.
  1161. */
  1162. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  1163. return -EINVAL;
  1164. if (nr_segments > KEXEC_SEGMENT_MAX)
  1165. return -EINVAL;
  1166. ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
  1167. for (i = 0; i < nr_segments; i++) {
  1168. result = copy_from_user(&in, &segments[i], sizeof(in));
  1169. if (result)
  1170. return -EFAULT;
  1171. out.buf = compat_ptr(in.buf);
  1172. out.bufsz = in.bufsz;
  1173. out.mem = in.mem;
  1174. out.memsz = in.memsz;
  1175. result = copy_to_user(&ksegments[i], &out, sizeof(out));
  1176. if (result)
  1177. return -EFAULT;
  1178. }
  1179. return sys_kexec_load(entry, nr_segments, ksegments, flags);
  1180. }
  1181. #endif
  1182. SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
  1183. unsigned long, cmdline_len, const char __user *, cmdline_ptr,
  1184. unsigned long, flags)
  1185. {
  1186. int ret = 0, i;
  1187. struct kimage **dest_image, *image;
  1188. /* We only trust the superuser with rebooting the system. */
  1189. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  1190. return -EPERM;
  1191. /* Make sure we have a legal set of flags */
  1192. if (flags != (flags & KEXEC_FILE_FLAGS))
  1193. return -EINVAL;
  1194. image = NULL;
  1195. if (!mutex_trylock(&kexec_mutex))
  1196. return -EBUSY;
  1197. dest_image = &kexec_image;
  1198. if (flags & KEXEC_FILE_ON_CRASH)
  1199. dest_image = &kexec_crash_image;
  1200. if (flags & KEXEC_FILE_UNLOAD)
  1201. goto exchange;
  1202. /*
  1203. * In case of crash, new kernel gets loaded in reserved region. It is
  1204. * same memory where old crash kernel might be loaded. Free any
  1205. * current crash dump kernel before we corrupt it.
  1206. */
  1207. if (flags & KEXEC_FILE_ON_CRASH)
  1208. kimage_free(xchg(&kexec_crash_image, NULL));
  1209. ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
  1210. cmdline_len, flags);
  1211. if (ret)
  1212. goto out;
  1213. ret = machine_kexec_prepare(image);
  1214. if (ret)
  1215. goto out;
  1216. ret = kexec_calculate_store_digests(image);
  1217. if (ret)
  1218. goto out;
  1219. for (i = 0; i < image->nr_segments; i++) {
  1220. struct kexec_segment *ksegment;
  1221. ksegment = &image->segment[i];
  1222. pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
  1223. i, ksegment->buf, ksegment->bufsz, ksegment->mem,
  1224. ksegment->memsz);
  1225. ret = kimage_load_segment(image, &image->segment[i]);
  1226. if (ret)
  1227. goto out;
  1228. }
  1229. kimage_terminate(image);
  1230. /*
  1231. * Free up any temporary buffers allocated which are not needed
  1232. * after image has been loaded
  1233. */
  1234. kimage_file_post_load_cleanup(image);
  1235. exchange:
  1236. image = xchg(dest_image, image);
  1237. out:
  1238. mutex_unlock(&kexec_mutex);
  1239. kimage_free(image);
  1240. return ret;
  1241. }
  1242. void crash_kexec(struct pt_regs *regs)
  1243. {
  1244. /* Take the kexec_mutex here to prevent sys_kexec_load
  1245. * running on one cpu from replacing the crash kernel
  1246. * we are using after a panic on a different cpu.
  1247. *
  1248. * If the crash kernel was not located in a fixed area
  1249. * of memory the xchg(&kexec_crash_image) would be
  1250. * sufficient. But since I reuse the memory...
  1251. */
  1252. if (mutex_trylock(&kexec_mutex)) {
  1253. if (kexec_crash_image) {
  1254. struct pt_regs fixed_regs;
  1255. crash_setup_regs(&fixed_regs, regs);
  1256. crash_save_vmcoreinfo();
  1257. machine_crash_shutdown(&fixed_regs);
  1258. machine_kexec(kexec_crash_image);
  1259. }
  1260. mutex_unlock(&kexec_mutex);
  1261. }
  1262. }
  1263. size_t crash_get_memory_size(void)
  1264. {
  1265. size_t size = 0;
  1266. mutex_lock(&kexec_mutex);
  1267. if (crashk_res.end != crashk_res.start)
  1268. size = resource_size(&crashk_res);
  1269. mutex_unlock(&kexec_mutex);
  1270. return size;
  1271. }
  1272. void __weak crash_free_reserved_phys_range(unsigned long begin,
  1273. unsigned long end)
  1274. {
  1275. unsigned long addr;
  1276. for (addr = begin; addr < end; addr += PAGE_SIZE)
  1277. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  1278. }
  1279. int crash_shrink_memory(unsigned long new_size)
  1280. {
  1281. int ret = 0;
  1282. unsigned long start, end;
  1283. unsigned long old_size;
  1284. struct resource *ram_res;
  1285. mutex_lock(&kexec_mutex);
  1286. if (kexec_crash_image) {
  1287. ret = -ENOENT;
  1288. goto unlock;
  1289. }
  1290. start = crashk_res.start;
  1291. end = crashk_res.end;
  1292. old_size = (end == 0) ? 0 : end - start + 1;
  1293. if (new_size >= old_size) {
  1294. ret = (new_size == old_size) ? 0 : -EINVAL;
  1295. goto unlock;
  1296. }
  1297. ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
  1298. if (!ram_res) {
  1299. ret = -ENOMEM;
  1300. goto unlock;
  1301. }
  1302. start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
  1303. end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
  1304. crash_map_reserved_pages();
  1305. crash_free_reserved_phys_range(end, crashk_res.end);
  1306. if ((start == end) && (crashk_res.parent != NULL))
  1307. release_resource(&crashk_res);
  1308. ram_res->start = end;
  1309. ram_res->end = crashk_res.end;
  1310. ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  1311. ram_res->name = "System RAM";
  1312. crashk_res.end = end - 1;
  1313. insert_resource(&iomem_resource, ram_res);
  1314. crash_unmap_reserved_pages();
  1315. unlock:
  1316. mutex_unlock(&kexec_mutex);
  1317. return ret;
  1318. }
  1319. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  1320. size_t data_len)
  1321. {
  1322. struct elf_note note;
  1323. note.n_namesz = strlen(name) + 1;
  1324. note.n_descsz = data_len;
  1325. note.n_type = type;
  1326. memcpy(buf, &note, sizeof(note));
  1327. buf += (sizeof(note) + 3)/4;
  1328. memcpy(buf, name, note.n_namesz);
  1329. buf += (note.n_namesz + 3)/4;
  1330. memcpy(buf, data, note.n_descsz);
  1331. buf += (note.n_descsz + 3)/4;
  1332. return buf;
  1333. }
  1334. static void final_note(u32 *buf)
  1335. {
  1336. struct elf_note note;
  1337. note.n_namesz = 0;
  1338. note.n_descsz = 0;
  1339. note.n_type = 0;
  1340. memcpy(buf, &note, sizeof(note));
  1341. }
  1342. void crash_save_cpu(struct pt_regs *regs, int cpu)
  1343. {
  1344. struct elf_prstatus prstatus;
  1345. u32 *buf;
  1346. if ((cpu < 0) || (cpu >= nr_cpu_ids))
  1347. return;
  1348. /* Using ELF notes here is opportunistic.
  1349. * I need a well defined structure format
  1350. * for the data I pass, and I need tags
  1351. * on the data to indicate what information I have
  1352. * squirrelled away. ELF notes happen to provide
  1353. * all of that, so there is no need to invent something new.
  1354. */
  1355. buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
  1356. if (!buf)
  1357. return;
  1358. memset(&prstatus, 0, sizeof(prstatus));
  1359. prstatus.pr_pid = current->pid;
  1360. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  1361. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  1362. &prstatus, sizeof(prstatus));
  1363. final_note(buf);
  1364. }
  1365. static int __init crash_notes_memory_init(void)
  1366. {
  1367. /* Allocate memory for saving cpu registers. */
  1368. crash_notes = alloc_percpu(note_buf_t);
  1369. if (!crash_notes) {
  1370. pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
  1371. return -ENOMEM;
  1372. }
  1373. return 0;
  1374. }
  1375. subsys_initcall(crash_notes_memory_init);
  1376. /*
  1377. * parsing the "crashkernel" commandline
  1378. *
  1379. * this code is intended to be called from architecture specific code
  1380. */
  1381. /*
  1382. * This function parses command lines in the format
  1383. *
  1384. * crashkernel=ramsize-range:size[,...][@offset]
  1385. *
  1386. * The function returns 0 on success and -EINVAL on failure.
  1387. */
  1388. static int __init parse_crashkernel_mem(char *cmdline,
  1389. unsigned long long system_ram,
  1390. unsigned long long *crash_size,
  1391. unsigned long long *crash_base)
  1392. {
  1393. char *cur = cmdline, *tmp;
  1394. /* for each entry of the comma-separated list */
  1395. do {
  1396. unsigned long long start, end = ULLONG_MAX, size;
  1397. /* get the start of the range */
  1398. start = memparse(cur, &tmp);
  1399. if (cur == tmp) {
  1400. pr_warn("crashkernel: Memory value expected\n");
  1401. return -EINVAL;
  1402. }
  1403. cur = tmp;
  1404. if (*cur != '-') {
  1405. pr_warn("crashkernel: '-' expected\n");
  1406. return -EINVAL;
  1407. }
  1408. cur++;
  1409. /* if no ':' is here, than we read the end */
  1410. if (*cur != ':') {
  1411. end = memparse(cur, &tmp);
  1412. if (cur == tmp) {
  1413. pr_warn("crashkernel: Memory value expected\n");
  1414. return -EINVAL;
  1415. }
  1416. cur = tmp;
  1417. if (end <= start) {
  1418. pr_warn("crashkernel: end <= start\n");
  1419. return -EINVAL;
  1420. }
  1421. }
  1422. if (*cur != ':') {
  1423. pr_warn("crashkernel: ':' expected\n");
  1424. return -EINVAL;
  1425. }
  1426. cur++;
  1427. size = memparse(cur, &tmp);
  1428. if (cur == tmp) {
  1429. pr_warn("Memory value expected\n");
  1430. return -EINVAL;
  1431. }
  1432. cur = tmp;
  1433. if (size >= system_ram) {
  1434. pr_warn("crashkernel: invalid size\n");
  1435. return -EINVAL;
  1436. }
  1437. /* match ? */
  1438. if (system_ram >= start && system_ram < end) {
  1439. *crash_size = size;
  1440. break;
  1441. }
  1442. } while (*cur++ == ',');
  1443. if (*crash_size > 0) {
  1444. while (*cur && *cur != ' ' && *cur != '@')
  1445. cur++;
  1446. if (*cur == '@') {
  1447. cur++;
  1448. *crash_base = memparse(cur, &tmp);
  1449. if (cur == tmp) {
  1450. pr_warn("Memory value expected after '@'\n");
  1451. return -EINVAL;
  1452. }
  1453. }
  1454. }
  1455. return 0;
  1456. }
  1457. /*
  1458. * That function parses "simple" (old) crashkernel command lines like
  1459. *
  1460. * crashkernel=size[@offset]
  1461. *
  1462. * It returns 0 on success and -EINVAL on failure.
  1463. */
  1464. static int __init parse_crashkernel_simple(char *cmdline,
  1465. unsigned long long *crash_size,
  1466. unsigned long long *crash_base)
  1467. {
  1468. char *cur = cmdline;
  1469. *crash_size = memparse(cmdline, &cur);
  1470. if (cmdline == cur) {
  1471. pr_warn("crashkernel: memory value expected\n");
  1472. return -EINVAL;
  1473. }
  1474. if (*cur == '@')
  1475. *crash_base = memparse(cur+1, &cur);
  1476. else if (*cur != ' ' && *cur != '\0') {
  1477. pr_warn("crashkernel: unrecognized char\n");
  1478. return -EINVAL;
  1479. }
  1480. return 0;
  1481. }
  1482. #define SUFFIX_HIGH 0
  1483. #define SUFFIX_LOW 1
  1484. #define SUFFIX_NULL 2
  1485. static __initdata char *suffix_tbl[] = {
  1486. [SUFFIX_HIGH] = ",high",
  1487. [SUFFIX_LOW] = ",low",
  1488. [SUFFIX_NULL] = NULL,
  1489. };
  1490. /*
  1491. * That function parses "suffix" crashkernel command lines like
  1492. *
  1493. * crashkernel=size,[high|low]
  1494. *
  1495. * It returns 0 on success and -EINVAL on failure.
  1496. */
  1497. static int __init parse_crashkernel_suffix(char *cmdline,
  1498. unsigned long long *crash_size,
  1499. unsigned long long *crash_base,
  1500. const char *suffix)
  1501. {
  1502. char *cur = cmdline;
  1503. *crash_size = memparse(cmdline, &cur);
  1504. if (cmdline == cur) {
  1505. pr_warn("crashkernel: memory value expected\n");
  1506. return -EINVAL;
  1507. }
  1508. /* check with suffix */
  1509. if (strncmp(cur, suffix, strlen(suffix))) {
  1510. pr_warn("crashkernel: unrecognized char\n");
  1511. return -EINVAL;
  1512. }
  1513. cur += strlen(suffix);
  1514. if (*cur != ' ' && *cur != '\0') {
  1515. pr_warn("crashkernel: unrecognized char\n");
  1516. return -EINVAL;
  1517. }
  1518. return 0;
  1519. }
  1520. static __init char *get_last_crashkernel(char *cmdline,
  1521. const char *name,
  1522. const char *suffix)
  1523. {
  1524. char *p = cmdline, *ck_cmdline = NULL;
  1525. /* find crashkernel and use the last one if there are more */
  1526. p = strstr(p, name);
  1527. while (p) {
  1528. char *end_p = strchr(p, ' ');
  1529. char *q;
  1530. if (!end_p)
  1531. end_p = p + strlen(p);
  1532. if (!suffix) {
  1533. int i;
  1534. /* skip the one with any known suffix */
  1535. for (i = 0; suffix_tbl[i]; i++) {
  1536. q = end_p - strlen(suffix_tbl[i]);
  1537. if (!strncmp(q, suffix_tbl[i],
  1538. strlen(suffix_tbl[i])))
  1539. goto next;
  1540. }
  1541. ck_cmdline = p;
  1542. } else {
  1543. q = end_p - strlen(suffix);
  1544. if (!strncmp(q, suffix, strlen(suffix)))
  1545. ck_cmdline = p;
  1546. }
  1547. next:
  1548. p = strstr(p+1, name);
  1549. }
  1550. if (!ck_cmdline)
  1551. return NULL;
  1552. return ck_cmdline;
  1553. }
  1554. static int __init __parse_crashkernel(char *cmdline,
  1555. unsigned long long system_ram,
  1556. unsigned long long *crash_size,
  1557. unsigned long long *crash_base,
  1558. const char *name,
  1559. const char *suffix)
  1560. {
  1561. char *first_colon, *first_space;
  1562. char *ck_cmdline;
  1563. BUG_ON(!crash_size || !crash_base);
  1564. *crash_size = 0;
  1565. *crash_base = 0;
  1566. ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
  1567. if (!ck_cmdline)
  1568. return -EINVAL;
  1569. ck_cmdline += strlen(name);
  1570. if (suffix)
  1571. return parse_crashkernel_suffix(ck_cmdline, crash_size,
  1572. crash_base, suffix);
  1573. /*
  1574. * if the commandline contains a ':', then that's the extended
  1575. * syntax -- if not, it must be the classic syntax
  1576. */
  1577. first_colon = strchr(ck_cmdline, ':');
  1578. first_space = strchr(ck_cmdline, ' ');
  1579. if (first_colon && (!first_space || first_colon < first_space))
  1580. return parse_crashkernel_mem(ck_cmdline, system_ram,
  1581. crash_size, crash_base);
  1582. return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
  1583. }
  1584. /*
  1585. * That function is the entry point for command line parsing and should be
  1586. * called from the arch-specific code.
  1587. */
  1588. int __init parse_crashkernel(char *cmdline,
  1589. unsigned long long system_ram,
  1590. unsigned long long *crash_size,
  1591. unsigned long long *crash_base)
  1592. {
  1593. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1594. "crashkernel=", NULL);
  1595. }
  1596. int __init parse_crashkernel_high(char *cmdline,
  1597. unsigned long long system_ram,
  1598. unsigned long long *crash_size,
  1599. unsigned long long *crash_base)
  1600. {
  1601. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1602. "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
  1603. }
  1604. int __init parse_crashkernel_low(char *cmdline,
  1605. unsigned long long system_ram,
  1606. unsigned long long *crash_size,
  1607. unsigned long long *crash_base)
  1608. {
  1609. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1610. "crashkernel=", suffix_tbl[SUFFIX_LOW]);
  1611. }
  1612. static void update_vmcoreinfo_note(void)
  1613. {
  1614. u32 *buf = vmcoreinfo_note;
  1615. if (!vmcoreinfo_size)
  1616. return;
  1617. buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
  1618. vmcoreinfo_size);
  1619. final_note(buf);
  1620. }
  1621. void crash_save_vmcoreinfo(void)
  1622. {
  1623. vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
  1624. update_vmcoreinfo_note();
  1625. }
  1626. void vmcoreinfo_append_str(const char *fmt, ...)
  1627. {
  1628. va_list args;
  1629. char buf[0x50];
  1630. size_t r;
  1631. va_start(args, fmt);
  1632. r = vscnprintf(buf, sizeof(buf), fmt, args);
  1633. va_end(args);
  1634. r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
  1635. memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
  1636. vmcoreinfo_size += r;
  1637. }
  1638. /*
  1639. * provide an empty default implementation here -- architecture
  1640. * code may override this
  1641. */
  1642. void __weak arch_crash_save_vmcoreinfo(void)
  1643. {}
  1644. unsigned long __weak paddr_vmcoreinfo_note(void)
  1645. {
  1646. return __pa((unsigned long)(char *)&vmcoreinfo_note);
  1647. }
  1648. static int __init crash_save_vmcoreinfo_init(void)
  1649. {
  1650. VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
  1651. VMCOREINFO_PAGESIZE(PAGE_SIZE);
  1652. VMCOREINFO_SYMBOL(init_uts_ns);
  1653. VMCOREINFO_SYMBOL(node_online_map);
  1654. #ifdef CONFIG_MMU
  1655. VMCOREINFO_SYMBOL(swapper_pg_dir);
  1656. #endif
  1657. VMCOREINFO_SYMBOL(_stext);
  1658. VMCOREINFO_SYMBOL(vmap_area_list);
  1659. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1660. VMCOREINFO_SYMBOL(mem_map);
  1661. VMCOREINFO_SYMBOL(contig_page_data);
  1662. #endif
  1663. #ifdef CONFIG_SPARSEMEM
  1664. VMCOREINFO_SYMBOL(mem_section);
  1665. VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
  1666. VMCOREINFO_STRUCT_SIZE(mem_section);
  1667. VMCOREINFO_OFFSET(mem_section, section_mem_map);
  1668. #endif
  1669. VMCOREINFO_STRUCT_SIZE(page);
  1670. VMCOREINFO_STRUCT_SIZE(pglist_data);
  1671. VMCOREINFO_STRUCT_SIZE(zone);
  1672. VMCOREINFO_STRUCT_SIZE(free_area);
  1673. VMCOREINFO_STRUCT_SIZE(list_head);
  1674. VMCOREINFO_SIZE(nodemask_t);
  1675. VMCOREINFO_OFFSET(page, flags);
  1676. VMCOREINFO_OFFSET(page, _count);
  1677. VMCOREINFO_OFFSET(page, mapping);
  1678. VMCOREINFO_OFFSET(page, lru);
  1679. VMCOREINFO_OFFSET(page, _mapcount);
  1680. VMCOREINFO_OFFSET(page, private);
  1681. VMCOREINFO_OFFSET(pglist_data, node_zones);
  1682. VMCOREINFO_OFFSET(pglist_data, nr_zones);
  1683. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1684. VMCOREINFO_OFFSET(pglist_data, node_mem_map);
  1685. #endif
  1686. VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
  1687. VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
  1688. VMCOREINFO_OFFSET(pglist_data, node_id);
  1689. VMCOREINFO_OFFSET(zone, free_area);
  1690. VMCOREINFO_OFFSET(zone, vm_stat);
  1691. VMCOREINFO_OFFSET(zone, spanned_pages);
  1692. VMCOREINFO_OFFSET(free_area, free_list);
  1693. VMCOREINFO_OFFSET(list_head, next);
  1694. VMCOREINFO_OFFSET(list_head, prev);
  1695. VMCOREINFO_OFFSET(vmap_area, va_start);
  1696. VMCOREINFO_OFFSET(vmap_area, list);
  1697. VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
  1698. log_buf_kexec_setup();
  1699. VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
  1700. VMCOREINFO_NUMBER(NR_FREE_PAGES);
  1701. VMCOREINFO_NUMBER(PG_lru);
  1702. VMCOREINFO_NUMBER(PG_private);
  1703. VMCOREINFO_NUMBER(PG_swapcache);
  1704. VMCOREINFO_NUMBER(PG_slab);
  1705. #ifdef CONFIG_MEMORY_FAILURE
  1706. VMCOREINFO_NUMBER(PG_hwpoison);
  1707. #endif
  1708. VMCOREINFO_NUMBER(PG_head_mask);
  1709. VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
  1710. #ifdef CONFIG_HUGETLBFS
  1711. VMCOREINFO_SYMBOL(free_huge_page);
  1712. #endif
  1713. arch_crash_save_vmcoreinfo();
  1714. update_vmcoreinfo_note();
  1715. return 0;
  1716. }
  1717. subsys_initcall(crash_save_vmcoreinfo_init);
  1718. static int __kexec_add_segment(struct kimage *image, char *buf,
  1719. unsigned long bufsz, unsigned long mem,
  1720. unsigned long memsz)
  1721. {
  1722. struct kexec_segment *ksegment;
  1723. ksegment = &image->segment[image->nr_segments];
  1724. ksegment->kbuf = buf;
  1725. ksegment->bufsz = bufsz;
  1726. ksegment->mem = mem;
  1727. ksegment->memsz = memsz;
  1728. image->nr_segments++;
  1729. return 0;
  1730. }
  1731. static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
  1732. struct kexec_buf *kbuf)
  1733. {
  1734. struct kimage *image = kbuf->image;
  1735. unsigned long temp_start, temp_end;
  1736. temp_end = min(end, kbuf->buf_max);
  1737. temp_start = temp_end - kbuf->memsz;
  1738. do {
  1739. /* align down start */
  1740. temp_start = temp_start & (~(kbuf->buf_align - 1));
  1741. if (temp_start < start || temp_start < kbuf->buf_min)
  1742. return 0;
  1743. temp_end = temp_start + kbuf->memsz - 1;
  1744. /*
  1745. * Make sure this does not conflict with any of existing
  1746. * segments
  1747. */
  1748. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  1749. temp_start = temp_start - PAGE_SIZE;
  1750. continue;
  1751. }
  1752. /* We found a suitable memory range */
  1753. break;
  1754. } while (1);
  1755. /* If we are here, we found a suitable memory range */
  1756. __kexec_add_segment(image, kbuf->buffer, kbuf->bufsz, temp_start,
  1757. kbuf->memsz);
  1758. /* Success, stop navigating through remaining System RAM ranges */
  1759. return 1;
  1760. }
  1761. static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
  1762. struct kexec_buf *kbuf)
  1763. {
  1764. struct kimage *image = kbuf->image;
  1765. unsigned long temp_start, temp_end;
  1766. temp_start = max(start, kbuf->buf_min);
  1767. do {
  1768. temp_start = ALIGN(temp_start, kbuf->buf_align);
  1769. temp_end = temp_start + kbuf->memsz - 1;
  1770. if (temp_end > end || temp_end > kbuf->buf_max)
  1771. return 0;
  1772. /*
  1773. * Make sure this does not conflict with any of existing
  1774. * segments
  1775. */
  1776. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  1777. temp_start = temp_start + PAGE_SIZE;
  1778. continue;
  1779. }
  1780. /* We found a suitable memory range */
  1781. break;
  1782. } while (1);
  1783. /* If we are here, we found a suitable memory range */
  1784. __kexec_add_segment(image, kbuf->buffer, kbuf->bufsz, temp_start,
  1785. kbuf->memsz);
  1786. /* Success, stop navigating through remaining System RAM ranges */
  1787. return 1;
  1788. }
  1789. static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
  1790. {
  1791. struct kexec_buf *kbuf = (struct kexec_buf *)arg;
  1792. unsigned long sz = end - start + 1;
  1793. /* Returning 0 will take to next memory range */
  1794. if (sz < kbuf->memsz)
  1795. return 0;
  1796. if (end < kbuf->buf_min || start > kbuf->buf_max)
  1797. return 0;
  1798. /*
  1799. * Allocate memory top down with-in ram range. Otherwise bottom up
  1800. * allocation.
  1801. */
  1802. if (kbuf->top_down)
  1803. return locate_mem_hole_top_down(start, end, kbuf);
  1804. return locate_mem_hole_bottom_up(start, end, kbuf);
  1805. }
  1806. /*
  1807. * Helper function for placing a buffer in a kexec segment. This assumes
  1808. * that kexec_mutex is held.
  1809. */
  1810. int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
  1811. unsigned long memsz, unsigned long buf_align,
  1812. unsigned long buf_min, unsigned long buf_max,
  1813. bool top_down, unsigned long *load_addr)
  1814. {
  1815. struct kexec_segment *ksegment;
  1816. struct kexec_buf buf, *kbuf;
  1817. int ret;
  1818. /* Currently adding segment this way is allowed only in file mode */
  1819. if (!image->file_mode)
  1820. return -EINVAL;
  1821. if (image->nr_segments >= KEXEC_SEGMENT_MAX)
  1822. return -EINVAL;
  1823. /*
  1824. * Make sure we are not trying to add buffer after allocating
  1825. * control pages. All segments need to be placed first before
  1826. * any control pages are allocated. As control page allocation
  1827. * logic goes through list of segments to make sure there are
  1828. * no destination overlaps.
  1829. */
  1830. if (!list_empty(&image->control_pages)) {
  1831. WARN_ON(1);
  1832. return -EINVAL;
  1833. }
  1834. memset(&buf, 0, sizeof(struct kexec_buf));
  1835. kbuf = &buf;
  1836. kbuf->image = image;
  1837. kbuf->buffer = buffer;
  1838. kbuf->bufsz = bufsz;
  1839. kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
  1840. kbuf->buf_align = max(buf_align, PAGE_SIZE);
  1841. kbuf->buf_min = buf_min;
  1842. kbuf->buf_max = buf_max;
  1843. kbuf->top_down = top_down;
  1844. /* Walk the RAM ranges and allocate a suitable range for the buffer */
  1845. if (image->type == KEXEC_TYPE_CRASH)
  1846. ret = walk_iomem_res("Crash kernel",
  1847. IORESOURCE_MEM | IORESOURCE_BUSY,
  1848. crashk_res.start, crashk_res.end, kbuf,
  1849. locate_mem_hole_callback);
  1850. else
  1851. ret = walk_system_ram_res(0, -1, kbuf,
  1852. locate_mem_hole_callback);
  1853. if (ret != 1) {
  1854. /* A suitable memory range could not be found for buffer */
  1855. return -EADDRNOTAVAIL;
  1856. }
  1857. /* Found a suitable memory range */
  1858. ksegment = &image->segment[image->nr_segments - 1];
  1859. *load_addr = ksegment->mem;
  1860. return 0;
  1861. }
  1862. /* Calculate and store the digest of segments */
  1863. static int kexec_calculate_store_digests(struct kimage *image)
  1864. {
  1865. struct crypto_shash *tfm;
  1866. struct shash_desc *desc;
  1867. int ret = 0, i, j, zero_buf_sz, sha_region_sz;
  1868. size_t desc_size, nullsz;
  1869. char *digest;
  1870. void *zero_buf;
  1871. struct kexec_sha_region *sha_regions;
  1872. struct purgatory_info *pi = &image->purgatory_info;
  1873. zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
  1874. zero_buf_sz = PAGE_SIZE;
  1875. tfm = crypto_alloc_shash("sha256", 0, 0);
  1876. if (IS_ERR(tfm)) {
  1877. ret = PTR_ERR(tfm);
  1878. goto out;
  1879. }
  1880. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  1881. desc = kzalloc(desc_size, GFP_KERNEL);
  1882. if (!desc) {
  1883. ret = -ENOMEM;
  1884. goto out_free_tfm;
  1885. }
  1886. sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
  1887. sha_regions = vzalloc(sha_region_sz);
  1888. if (!sha_regions)
  1889. goto out_free_desc;
  1890. desc->tfm = tfm;
  1891. desc->flags = 0;
  1892. ret = crypto_shash_init(desc);
  1893. if (ret < 0)
  1894. goto out_free_sha_regions;
  1895. digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  1896. if (!digest) {
  1897. ret = -ENOMEM;
  1898. goto out_free_sha_regions;
  1899. }
  1900. for (j = i = 0; i < image->nr_segments; i++) {
  1901. struct kexec_segment *ksegment;
  1902. ksegment = &image->segment[i];
  1903. /*
  1904. * Skip purgatory as it will be modified once we put digest
  1905. * info in purgatory.
  1906. */
  1907. if (ksegment->kbuf == pi->purgatory_buf)
  1908. continue;
  1909. ret = crypto_shash_update(desc, ksegment->kbuf,
  1910. ksegment->bufsz);
  1911. if (ret)
  1912. break;
  1913. /*
  1914. * Assume rest of the buffer is filled with zero and
  1915. * update digest accordingly.
  1916. */
  1917. nullsz = ksegment->memsz - ksegment->bufsz;
  1918. while (nullsz) {
  1919. unsigned long bytes = nullsz;
  1920. if (bytes > zero_buf_sz)
  1921. bytes = zero_buf_sz;
  1922. ret = crypto_shash_update(desc, zero_buf, bytes);
  1923. if (ret)
  1924. break;
  1925. nullsz -= bytes;
  1926. }
  1927. if (ret)
  1928. break;
  1929. sha_regions[j].start = ksegment->mem;
  1930. sha_regions[j].len = ksegment->memsz;
  1931. j++;
  1932. }
  1933. if (!ret) {
  1934. ret = crypto_shash_final(desc, digest);
  1935. if (ret)
  1936. goto out_free_digest;
  1937. ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
  1938. sha_regions, sha_region_sz, 0);
  1939. if (ret)
  1940. goto out_free_digest;
  1941. ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
  1942. digest, SHA256_DIGEST_SIZE, 0);
  1943. if (ret)
  1944. goto out_free_digest;
  1945. }
  1946. out_free_digest:
  1947. kfree(digest);
  1948. out_free_sha_regions:
  1949. vfree(sha_regions);
  1950. out_free_desc:
  1951. kfree(desc);
  1952. out_free_tfm:
  1953. kfree(tfm);
  1954. out:
  1955. return ret;
  1956. }
  1957. /* Actually load purgatory. Lot of code taken from kexec-tools */
  1958. static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
  1959. unsigned long max, int top_down)
  1960. {
  1961. struct purgatory_info *pi = &image->purgatory_info;
  1962. unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
  1963. unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
  1964. unsigned char *buf_addr, *src;
  1965. int i, ret = 0, entry_sidx = -1;
  1966. const Elf_Shdr *sechdrs_c;
  1967. Elf_Shdr *sechdrs = NULL;
  1968. void *purgatory_buf = NULL;
  1969. /*
  1970. * sechdrs_c points to section headers in purgatory and are read
  1971. * only. No modifications allowed.
  1972. */
  1973. sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
  1974. /*
  1975. * We can not modify sechdrs_c[] and its fields. It is read only.
  1976. * Copy it over to a local copy where one can store some temporary
  1977. * data and free it at the end. We need to modify ->sh_addr and
  1978. * ->sh_offset fields to keep track of permanent and temporary
  1979. * locations of sections.
  1980. */
  1981. sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  1982. if (!sechdrs)
  1983. return -ENOMEM;
  1984. memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  1985. /*
  1986. * We seem to have multiple copies of sections. First copy is which
  1987. * is embedded in kernel in read only section. Some of these sections
  1988. * will be copied to a temporary buffer and relocated. And these
  1989. * sections will finally be copied to their final destination at
  1990. * segment load time.
  1991. *
  1992. * Use ->sh_offset to reflect section address in memory. It will
  1993. * point to original read only copy if section is not allocatable.
  1994. * Otherwise it will point to temporary copy which will be relocated.
  1995. *
  1996. * Use ->sh_addr to contain final address of the section where it
  1997. * will go during execution time.
  1998. */
  1999. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2000. if (sechdrs[i].sh_type == SHT_NOBITS)
  2001. continue;
  2002. sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
  2003. sechdrs[i].sh_offset;
  2004. }
  2005. /*
  2006. * Identify entry point section and make entry relative to section
  2007. * start.
  2008. */
  2009. entry = pi->ehdr->e_entry;
  2010. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2011. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  2012. continue;
  2013. if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
  2014. continue;
  2015. /* Make entry section relative */
  2016. if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
  2017. ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
  2018. pi->ehdr->e_entry)) {
  2019. entry_sidx = i;
  2020. entry -= sechdrs[i].sh_addr;
  2021. break;
  2022. }
  2023. }
  2024. /* Determine how much memory is needed to load relocatable object. */
  2025. buf_align = 1;
  2026. bss_align = 1;
  2027. buf_sz = 0;
  2028. bss_sz = 0;
  2029. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2030. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  2031. continue;
  2032. align = sechdrs[i].sh_addralign;
  2033. if (sechdrs[i].sh_type != SHT_NOBITS) {
  2034. if (buf_align < align)
  2035. buf_align = align;
  2036. buf_sz = ALIGN(buf_sz, align);
  2037. buf_sz += sechdrs[i].sh_size;
  2038. } else {
  2039. /* bss section */
  2040. if (bss_align < align)
  2041. bss_align = align;
  2042. bss_sz = ALIGN(bss_sz, align);
  2043. bss_sz += sechdrs[i].sh_size;
  2044. }
  2045. }
  2046. /* Determine the bss padding required to align bss properly */
  2047. bss_pad = 0;
  2048. if (buf_sz & (bss_align - 1))
  2049. bss_pad = bss_align - (buf_sz & (bss_align - 1));
  2050. memsz = buf_sz + bss_pad + bss_sz;
  2051. /* Allocate buffer for purgatory */
  2052. purgatory_buf = vzalloc(buf_sz);
  2053. if (!purgatory_buf) {
  2054. ret = -ENOMEM;
  2055. goto out;
  2056. }
  2057. if (buf_align < bss_align)
  2058. buf_align = bss_align;
  2059. /* Add buffer to segment list */
  2060. ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
  2061. buf_align, min, max, top_down,
  2062. &pi->purgatory_load_addr);
  2063. if (ret)
  2064. goto out;
  2065. /* Load SHF_ALLOC sections */
  2066. buf_addr = purgatory_buf;
  2067. load_addr = curr_load_addr = pi->purgatory_load_addr;
  2068. bss_addr = load_addr + buf_sz + bss_pad;
  2069. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2070. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  2071. continue;
  2072. align = sechdrs[i].sh_addralign;
  2073. if (sechdrs[i].sh_type != SHT_NOBITS) {
  2074. curr_load_addr = ALIGN(curr_load_addr, align);
  2075. offset = curr_load_addr - load_addr;
  2076. /* We already modifed ->sh_offset to keep src addr */
  2077. src = (char *) sechdrs[i].sh_offset;
  2078. memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
  2079. /* Store load address and source address of section */
  2080. sechdrs[i].sh_addr = curr_load_addr;
  2081. /*
  2082. * This section got copied to temporary buffer. Update
  2083. * ->sh_offset accordingly.
  2084. */
  2085. sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
  2086. /* Advance to the next address */
  2087. curr_load_addr += sechdrs[i].sh_size;
  2088. } else {
  2089. bss_addr = ALIGN(bss_addr, align);
  2090. sechdrs[i].sh_addr = bss_addr;
  2091. bss_addr += sechdrs[i].sh_size;
  2092. }
  2093. }
  2094. /* Update entry point based on load address of text section */
  2095. if (entry_sidx >= 0)
  2096. entry += sechdrs[entry_sidx].sh_addr;
  2097. /* Make kernel jump to purgatory after shutdown */
  2098. image->start = entry;
  2099. /* Used later to get/set symbol values */
  2100. pi->sechdrs = sechdrs;
  2101. /*
  2102. * Used later to identify which section is purgatory and skip it
  2103. * from checksumming.
  2104. */
  2105. pi->purgatory_buf = purgatory_buf;
  2106. return ret;
  2107. out:
  2108. vfree(sechdrs);
  2109. vfree(purgatory_buf);
  2110. return ret;
  2111. }
  2112. static int kexec_apply_relocations(struct kimage *image)
  2113. {
  2114. int i, ret;
  2115. struct purgatory_info *pi = &image->purgatory_info;
  2116. Elf_Shdr *sechdrs = pi->sechdrs;
  2117. /* Apply relocations */
  2118. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2119. Elf_Shdr *section, *symtab;
  2120. if (sechdrs[i].sh_type != SHT_RELA &&
  2121. sechdrs[i].sh_type != SHT_REL)
  2122. continue;
  2123. /*
  2124. * For section of type SHT_RELA/SHT_REL,
  2125. * ->sh_link contains section header index of associated
  2126. * symbol table. And ->sh_info contains section header
  2127. * index of section to which relocations apply.
  2128. */
  2129. if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
  2130. sechdrs[i].sh_link >= pi->ehdr->e_shnum)
  2131. return -ENOEXEC;
  2132. section = &sechdrs[sechdrs[i].sh_info];
  2133. symtab = &sechdrs[sechdrs[i].sh_link];
  2134. if (!(section->sh_flags & SHF_ALLOC))
  2135. continue;
  2136. /*
  2137. * symtab->sh_link contain section header index of associated
  2138. * string table.
  2139. */
  2140. if (symtab->sh_link >= pi->ehdr->e_shnum)
  2141. /* Invalid section number? */
  2142. continue;
  2143. /*
  2144. * Respective archicture needs to provide support for applying
  2145. * relocations of type SHT_RELA/SHT_REL.
  2146. */
  2147. if (sechdrs[i].sh_type == SHT_RELA)
  2148. ret = arch_kexec_apply_relocations_add(pi->ehdr,
  2149. sechdrs, i);
  2150. else if (sechdrs[i].sh_type == SHT_REL)
  2151. ret = arch_kexec_apply_relocations(pi->ehdr,
  2152. sechdrs, i);
  2153. if (ret)
  2154. return ret;
  2155. }
  2156. return 0;
  2157. }
  2158. /* Load relocatable purgatory object and relocate it appropriately */
  2159. int kexec_load_purgatory(struct kimage *image, unsigned long min,
  2160. unsigned long max, int top_down,
  2161. unsigned long *load_addr)
  2162. {
  2163. struct purgatory_info *pi = &image->purgatory_info;
  2164. int ret;
  2165. if (kexec_purgatory_size <= 0)
  2166. return -EINVAL;
  2167. if (kexec_purgatory_size < sizeof(Elf_Ehdr))
  2168. return -ENOEXEC;
  2169. pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
  2170. if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
  2171. || pi->ehdr->e_type != ET_REL
  2172. || !elf_check_arch(pi->ehdr)
  2173. || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
  2174. return -ENOEXEC;
  2175. if (pi->ehdr->e_shoff >= kexec_purgatory_size
  2176. || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
  2177. kexec_purgatory_size - pi->ehdr->e_shoff))
  2178. return -ENOEXEC;
  2179. ret = __kexec_load_purgatory(image, min, max, top_down);
  2180. if (ret)
  2181. return ret;
  2182. ret = kexec_apply_relocations(image);
  2183. if (ret)
  2184. goto out;
  2185. *load_addr = pi->purgatory_load_addr;
  2186. return 0;
  2187. out:
  2188. vfree(pi->sechdrs);
  2189. vfree(pi->purgatory_buf);
  2190. return ret;
  2191. }
  2192. static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
  2193. const char *name)
  2194. {
  2195. Elf_Sym *syms;
  2196. Elf_Shdr *sechdrs;
  2197. Elf_Ehdr *ehdr;
  2198. int i, k;
  2199. const char *strtab;
  2200. if (!pi->sechdrs || !pi->ehdr)
  2201. return NULL;
  2202. sechdrs = pi->sechdrs;
  2203. ehdr = pi->ehdr;
  2204. for (i = 0; i < ehdr->e_shnum; i++) {
  2205. if (sechdrs[i].sh_type != SHT_SYMTAB)
  2206. continue;
  2207. if (sechdrs[i].sh_link >= ehdr->e_shnum)
  2208. /* Invalid strtab section number */
  2209. continue;
  2210. strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
  2211. syms = (Elf_Sym *)sechdrs[i].sh_offset;
  2212. /* Go through symbols for a match */
  2213. for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
  2214. if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
  2215. continue;
  2216. if (strcmp(strtab + syms[k].st_name, name) != 0)
  2217. continue;
  2218. if (syms[k].st_shndx == SHN_UNDEF ||
  2219. syms[k].st_shndx >= ehdr->e_shnum) {
  2220. pr_debug("Symbol: %s has bad section index %d.\n",
  2221. name, syms[k].st_shndx);
  2222. return NULL;
  2223. }
  2224. /* Found the symbol we are looking for */
  2225. return &syms[k];
  2226. }
  2227. }
  2228. return NULL;
  2229. }
  2230. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
  2231. {
  2232. struct purgatory_info *pi = &image->purgatory_info;
  2233. Elf_Sym *sym;
  2234. Elf_Shdr *sechdr;
  2235. sym = kexec_purgatory_find_symbol(pi, name);
  2236. if (!sym)
  2237. return ERR_PTR(-EINVAL);
  2238. sechdr = &pi->sechdrs[sym->st_shndx];
  2239. /*
  2240. * Returns the address where symbol will finally be loaded after
  2241. * kexec_load_segment()
  2242. */
  2243. return (void *)(sechdr->sh_addr + sym->st_value);
  2244. }
  2245. /*
  2246. * Get or set value of a symbol. If "get_value" is true, symbol value is
  2247. * returned in buf otherwise symbol value is set based on value in buf.
  2248. */
  2249. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  2250. void *buf, unsigned int size, bool get_value)
  2251. {
  2252. Elf_Sym *sym;
  2253. Elf_Shdr *sechdrs;
  2254. struct purgatory_info *pi = &image->purgatory_info;
  2255. char *sym_buf;
  2256. sym = kexec_purgatory_find_symbol(pi, name);
  2257. if (!sym)
  2258. return -EINVAL;
  2259. if (sym->st_size != size) {
  2260. pr_err("symbol %s size mismatch: expected %lu actual %u\n",
  2261. name, (unsigned long)sym->st_size, size);
  2262. return -EINVAL;
  2263. }
  2264. sechdrs = pi->sechdrs;
  2265. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  2266. pr_err("symbol %s is in a bss section. Cannot %s\n", name,
  2267. get_value ? "get" : "set");
  2268. return -EINVAL;
  2269. }
  2270. sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
  2271. sym->st_value;
  2272. if (get_value)
  2273. memcpy((void *)buf, sym_buf, size);
  2274. else
  2275. memcpy((void *)sym_buf, buf, size);
  2276. return 0;
  2277. }
  2278. /*
  2279. * Move into place and start executing a preloaded standalone
  2280. * executable. If nothing was preloaded return an error.
  2281. */
  2282. int kernel_kexec(void)
  2283. {
  2284. int error = 0;
  2285. if (!mutex_trylock(&kexec_mutex))
  2286. return -EBUSY;
  2287. if (!kexec_image) {
  2288. error = -EINVAL;
  2289. goto Unlock;
  2290. }
  2291. #ifdef CONFIG_KEXEC_JUMP
  2292. if (kexec_image->preserve_context) {
  2293. lock_system_sleep();
  2294. pm_prepare_console();
  2295. error = freeze_processes();
  2296. if (error) {
  2297. error = -EBUSY;
  2298. goto Restore_console;
  2299. }
  2300. suspend_console();
  2301. error = dpm_suspend_start(PMSG_FREEZE);
  2302. if (error)
  2303. goto Resume_console;
  2304. /* At this point, dpm_suspend_start() has been called,
  2305. * but *not* dpm_suspend_end(). We *must* call
  2306. * dpm_suspend_end() now. Otherwise, drivers for
  2307. * some devices (e.g. interrupt controllers) become
  2308. * desynchronized with the actual state of the
  2309. * hardware at resume time, and evil weirdness ensues.
  2310. */
  2311. error = dpm_suspend_end(PMSG_FREEZE);
  2312. if (error)
  2313. goto Resume_devices;
  2314. error = disable_nonboot_cpus();
  2315. if (error)
  2316. goto Enable_cpus;
  2317. local_irq_disable();
  2318. error = syscore_suspend();
  2319. if (error)
  2320. goto Enable_irqs;
  2321. } else
  2322. #endif
  2323. {
  2324. kexec_in_progress = true;
  2325. kernel_restart_prepare(NULL);
  2326. migrate_to_reboot_cpu();
  2327. /*
  2328. * migrate_to_reboot_cpu() disables CPU hotplug assuming that
  2329. * no further code needs to use CPU hotplug (which is true in
  2330. * the reboot case). However, the kexec path depends on using
  2331. * CPU hotplug again; so re-enable it here.
  2332. */
  2333. cpu_hotplug_enable();
  2334. pr_emerg("Starting new kernel\n");
  2335. machine_shutdown();
  2336. }
  2337. machine_kexec(kexec_image);
  2338. #ifdef CONFIG_KEXEC_JUMP
  2339. if (kexec_image->preserve_context) {
  2340. syscore_resume();
  2341. Enable_irqs:
  2342. local_irq_enable();
  2343. Enable_cpus:
  2344. enable_nonboot_cpus();
  2345. dpm_resume_start(PMSG_RESTORE);
  2346. Resume_devices:
  2347. dpm_resume_end(PMSG_RESTORE);
  2348. Resume_console:
  2349. resume_console();
  2350. thaw_processes();
  2351. Restore_console:
  2352. pm_restore_console();
  2353. unlock_system_sleep();
  2354. }
  2355. #endif
  2356. Unlock:
  2357. mutex_unlock(&kexec_mutex);
  2358. return error;
  2359. }