kexec.c 68 KB

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