kexec.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769
  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 command 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("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. return result;
  735. }
  736. static int kimage_add_page(struct kimage *image, unsigned long page)
  737. {
  738. int result;
  739. page &= PAGE_MASK;
  740. result = kimage_add_entry(image, page | IND_SOURCE);
  741. return result;
  742. }
  743. static void kimage_free_extra_pages(struct kimage *image)
  744. {
  745. /* Walk through and free any extra destination pages I may have */
  746. kimage_free_page_list(&image->dest_pages);
  747. /* Walk through and free any unusable pages I have cached */
  748. kimage_free_page_list(&image->unusable_pages);
  749. }
  750. static void kimage_terminate(struct kimage *image)
  751. {
  752. if (*image->entry != 0)
  753. image->entry++;
  754. *image->entry = IND_DONE;
  755. }
  756. #define for_each_kimage_entry(image, ptr, entry) \
  757. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  758. ptr = (entry & IND_INDIRECTION) ? \
  759. phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
  760. static void kimage_free_entry(kimage_entry_t entry)
  761. {
  762. struct page *page;
  763. page = pfn_to_page(entry >> PAGE_SHIFT);
  764. kimage_free_pages(page);
  765. }
  766. static void kimage_free(struct kimage *image)
  767. {
  768. kimage_entry_t *ptr, entry;
  769. kimage_entry_t ind = 0;
  770. if (!image)
  771. return;
  772. kimage_free_extra_pages(image);
  773. for_each_kimage_entry(image, ptr, entry) {
  774. if (entry & IND_INDIRECTION) {
  775. /* Free the previous indirection page */
  776. if (ind & IND_INDIRECTION)
  777. kimage_free_entry(ind);
  778. /* Save this indirection page until we are
  779. * done with it.
  780. */
  781. ind = entry;
  782. } else if (entry & IND_SOURCE)
  783. kimage_free_entry(entry);
  784. }
  785. /* Free the final indirection page */
  786. if (ind & IND_INDIRECTION)
  787. kimage_free_entry(ind);
  788. /* Handle any machine specific cleanup */
  789. machine_kexec_cleanup(image);
  790. /* Free the kexec control pages... */
  791. kimage_free_page_list(&image->control_pages);
  792. /*
  793. * Free up any temporary buffers allocated. This might hit if
  794. * error occurred much later after buffer allocation.
  795. */
  796. if (image->file_mode)
  797. kimage_file_post_load_cleanup(image);
  798. kfree(image);
  799. }
  800. static kimage_entry_t *kimage_dst_used(struct kimage *image,
  801. unsigned long page)
  802. {
  803. kimage_entry_t *ptr, entry;
  804. unsigned long destination = 0;
  805. for_each_kimage_entry(image, ptr, entry) {
  806. if (entry & IND_DESTINATION)
  807. destination = entry & PAGE_MASK;
  808. else if (entry & IND_SOURCE) {
  809. if (page == destination)
  810. return ptr;
  811. destination += PAGE_SIZE;
  812. }
  813. }
  814. return NULL;
  815. }
  816. static struct page *kimage_alloc_page(struct kimage *image,
  817. gfp_t gfp_mask,
  818. unsigned long destination)
  819. {
  820. /*
  821. * Here we implement safeguards to ensure that a source page
  822. * is not copied to its destination page before the data on
  823. * the destination page is no longer useful.
  824. *
  825. * To do this we maintain the invariant that a source page is
  826. * either its own destination page, or it is not a
  827. * destination page at all.
  828. *
  829. * That is slightly stronger than required, but the proof
  830. * that no problems will not occur is trivial, and the
  831. * implementation is simply to verify.
  832. *
  833. * When allocating all pages normally this algorithm will run
  834. * in O(N) time, but in the worst case it will run in O(N^2)
  835. * time. If the runtime is a problem the data structures can
  836. * be fixed.
  837. */
  838. struct page *page;
  839. unsigned long addr;
  840. /*
  841. * Walk through the list of destination pages, and see if I
  842. * have a match.
  843. */
  844. list_for_each_entry(page, &image->dest_pages, lru) {
  845. addr = page_to_pfn(page) << PAGE_SHIFT;
  846. if (addr == destination) {
  847. list_del(&page->lru);
  848. return page;
  849. }
  850. }
  851. page = NULL;
  852. while (1) {
  853. kimage_entry_t *old;
  854. /* Allocate a page, if we run out of memory give up */
  855. page = kimage_alloc_pages(gfp_mask, 0);
  856. if (!page)
  857. return NULL;
  858. /* If the page cannot be used file it away */
  859. if (page_to_pfn(page) >
  860. (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
  861. list_add(&page->lru, &image->unusable_pages);
  862. continue;
  863. }
  864. addr = page_to_pfn(page) << PAGE_SHIFT;
  865. /* If it is the destination page we want use it */
  866. if (addr == destination)
  867. break;
  868. /* If the page is not a destination page use it */
  869. if (!kimage_is_destination_range(image, addr,
  870. addr + PAGE_SIZE))
  871. break;
  872. /*
  873. * I know that the page is someones destination page.
  874. * See if there is already a source page for this
  875. * destination page. And if so swap the source pages.
  876. */
  877. old = kimage_dst_used(image, addr);
  878. if (old) {
  879. /* If so move it */
  880. unsigned long old_addr;
  881. struct page *old_page;
  882. old_addr = *old & PAGE_MASK;
  883. old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
  884. copy_highpage(page, old_page);
  885. *old = addr | (*old & ~PAGE_MASK);
  886. /* The old page I have found cannot be a
  887. * destination page, so return it if it's
  888. * gfp_flags honor the ones passed in.
  889. */
  890. if (!(gfp_mask & __GFP_HIGHMEM) &&
  891. PageHighMem(old_page)) {
  892. kimage_free_pages(old_page);
  893. continue;
  894. }
  895. addr = old_addr;
  896. page = old_page;
  897. break;
  898. } else {
  899. /* Place the page on the destination list I
  900. * will use it later.
  901. */
  902. list_add(&page->lru, &image->dest_pages);
  903. }
  904. }
  905. return page;
  906. }
  907. static int kimage_load_normal_segment(struct kimage *image,
  908. struct kexec_segment *segment)
  909. {
  910. unsigned long maddr;
  911. size_t ubytes, mbytes;
  912. int result;
  913. unsigned char __user *buf = NULL;
  914. unsigned char *kbuf = NULL;
  915. result = 0;
  916. if (image->file_mode)
  917. kbuf = segment->kbuf;
  918. else
  919. buf = segment->buf;
  920. ubytes = segment->bufsz;
  921. mbytes = segment->memsz;
  922. maddr = segment->mem;
  923. result = kimage_set_destination(image, maddr);
  924. if (result < 0)
  925. goto out;
  926. while (mbytes) {
  927. struct page *page;
  928. char *ptr;
  929. size_t uchunk, mchunk;
  930. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  931. if (!page) {
  932. result = -ENOMEM;
  933. goto out;
  934. }
  935. result = kimage_add_page(image, page_to_pfn(page)
  936. << PAGE_SHIFT);
  937. if (result < 0)
  938. goto out;
  939. ptr = kmap(page);
  940. /* Start with a clear page */
  941. clear_page(ptr);
  942. ptr += maddr & ~PAGE_MASK;
  943. mchunk = min_t(size_t, mbytes,
  944. PAGE_SIZE - (maddr & ~PAGE_MASK));
  945. uchunk = min(ubytes, mchunk);
  946. /* For file based kexec, source pages are in kernel memory */
  947. if (image->file_mode)
  948. memcpy(ptr, kbuf, uchunk);
  949. else
  950. result = copy_from_user(ptr, buf, uchunk);
  951. kunmap(page);
  952. if (result) {
  953. result = -EFAULT;
  954. goto out;
  955. }
  956. ubytes -= uchunk;
  957. maddr += mchunk;
  958. if (image->file_mode)
  959. kbuf += mchunk;
  960. else
  961. buf += mchunk;
  962. mbytes -= mchunk;
  963. }
  964. out:
  965. return result;
  966. }
  967. static int kimage_load_crash_segment(struct kimage *image,
  968. struct kexec_segment *segment)
  969. {
  970. /* For crash dumps kernels we simply copy the data from
  971. * user space to it's destination.
  972. * We do things a page at a time for the sake of kmap.
  973. */
  974. unsigned long maddr;
  975. size_t ubytes, mbytes;
  976. int result;
  977. unsigned char __user *buf = NULL;
  978. unsigned char *kbuf = NULL;
  979. result = 0;
  980. if (image->file_mode)
  981. kbuf = segment->kbuf;
  982. else
  983. buf = segment->buf;
  984. ubytes = segment->bufsz;
  985. mbytes = segment->memsz;
  986. maddr = segment->mem;
  987. while (mbytes) {
  988. struct page *page;
  989. char *ptr;
  990. size_t uchunk, mchunk;
  991. page = pfn_to_page(maddr >> PAGE_SHIFT);
  992. if (!page) {
  993. result = -ENOMEM;
  994. goto out;
  995. }
  996. ptr = kmap(page);
  997. ptr += maddr & ~PAGE_MASK;
  998. mchunk = min_t(size_t, mbytes,
  999. PAGE_SIZE - (maddr & ~PAGE_MASK));
  1000. uchunk = min(ubytes, mchunk);
  1001. if (mchunk > uchunk) {
  1002. /* Zero the trailing part of the page */
  1003. memset(ptr + uchunk, 0, mchunk - uchunk);
  1004. }
  1005. /* For file based kexec, source pages are in kernel memory */
  1006. if (image->file_mode)
  1007. memcpy(ptr, kbuf, uchunk);
  1008. else
  1009. result = copy_from_user(ptr, buf, uchunk);
  1010. kexec_flush_icache_page(page);
  1011. kunmap(page);
  1012. if (result) {
  1013. result = -EFAULT;
  1014. goto out;
  1015. }
  1016. ubytes -= uchunk;
  1017. maddr += mchunk;
  1018. if (image->file_mode)
  1019. kbuf += mchunk;
  1020. else
  1021. buf += mchunk;
  1022. mbytes -= mchunk;
  1023. }
  1024. out:
  1025. return result;
  1026. }
  1027. static int kimage_load_segment(struct kimage *image,
  1028. struct kexec_segment *segment)
  1029. {
  1030. int result = -ENOMEM;
  1031. switch (image->type) {
  1032. case KEXEC_TYPE_DEFAULT:
  1033. result = kimage_load_normal_segment(image, segment);
  1034. break;
  1035. case KEXEC_TYPE_CRASH:
  1036. result = kimage_load_crash_segment(image, segment);
  1037. break;
  1038. }
  1039. return result;
  1040. }
  1041. /*
  1042. * Exec Kernel system call: for obvious reasons only root may call it.
  1043. *
  1044. * This call breaks up into three pieces.
  1045. * - A generic part which loads the new kernel from the current
  1046. * address space, and very carefully places the data in the
  1047. * allocated pages.
  1048. *
  1049. * - A generic part that interacts with the kernel and tells all of
  1050. * the devices to shut down. Preventing on-going dmas, and placing
  1051. * the devices in a consistent state so a later kernel can
  1052. * reinitialize them.
  1053. *
  1054. * - A machine specific part that includes the syscall number
  1055. * and then copies the image to it's final destination. And
  1056. * jumps into the image at entry.
  1057. *
  1058. * kexec does not sync, or unmount filesystems so if you need
  1059. * that to happen you need to do that yourself.
  1060. */
  1061. struct kimage *kexec_image;
  1062. struct kimage *kexec_crash_image;
  1063. int kexec_load_disabled;
  1064. static DEFINE_MUTEX(kexec_mutex);
  1065. SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
  1066. struct kexec_segment __user *, segments, unsigned long, flags)
  1067. {
  1068. struct kimage **dest_image, *image;
  1069. int result;
  1070. /* We only trust the superuser with rebooting the system. */
  1071. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  1072. return -EPERM;
  1073. /*
  1074. * Verify we have a legal set of flags
  1075. * This leaves us room for future extensions.
  1076. */
  1077. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  1078. return -EINVAL;
  1079. /* Verify we are on the appropriate architecture */
  1080. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  1081. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  1082. return -EINVAL;
  1083. /* Put an artificial cap on the number
  1084. * of segments passed to kexec_load.
  1085. */
  1086. if (nr_segments > KEXEC_SEGMENT_MAX)
  1087. return -EINVAL;
  1088. image = NULL;
  1089. result = 0;
  1090. /* Because we write directly to the reserved memory
  1091. * region when loading crash kernels we need a mutex here to
  1092. * prevent multiple crash kernels from attempting to load
  1093. * simultaneously, and to prevent a crash kernel from loading
  1094. * over the top of a in use crash kernel.
  1095. *
  1096. * KISS: always take the mutex.
  1097. */
  1098. if (!mutex_trylock(&kexec_mutex))
  1099. return -EBUSY;
  1100. dest_image = &kexec_image;
  1101. if (flags & KEXEC_ON_CRASH)
  1102. dest_image = &kexec_crash_image;
  1103. if (nr_segments > 0) {
  1104. unsigned long i;
  1105. if (flags & KEXEC_ON_CRASH) {
  1106. /*
  1107. * Loading another kernel to switch to if this one
  1108. * crashes. Free any current crash dump kernel before
  1109. * we corrupt it.
  1110. */
  1111. kimage_free(xchg(&kexec_crash_image, NULL));
  1112. result = kimage_alloc_init(&image, entry, nr_segments,
  1113. segments, flags);
  1114. crash_map_reserved_pages();
  1115. } else {
  1116. /* Loading another kernel to reboot into. */
  1117. result = kimage_alloc_init(&image, entry, nr_segments,
  1118. segments, flags);
  1119. }
  1120. if (result)
  1121. goto out;
  1122. if (flags & KEXEC_PRESERVE_CONTEXT)
  1123. image->preserve_context = 1;
  1124. result = machine_kexec_prepare(image);
  1125. if (result)
  1126. goto out;
  1127. for (i = 0; i < nr_segments; i++) {
  1128. result = kimage_load_segment(image, &image->segment[i]);
  1129. if (result)
  1130. goto out;
  1131. }
  1132. kimage_terminate(image);
  1133. if (flags & KEXEC_ON_CRASH)
  1134. crash_unmap_reserved_pages();
  1135. }
  1136. /* Install the new kernel, and Uninstall the old */
  1137. image = xchg(dest_image, image);
  1138. out:
  1139. mutex_unlock(&kexec_mutex);
  1140. kimage_free(image);
  1141. return result;
  1142. }
  1143. /*
  1144. * Add and remove page tables for crashkernel memory
  1145. *
  1146. * Provide an empty default implementation here -- architecture
  1147. * code may override this
  1148. */
  1149. void __weak crash_map_reserved_pages(void)
  1150. {}
  1151. void __weak crash_unmap_reserved_pages(void)
  1152. {}
  1153. #ifdef CONFIG_COMPAT
  1154. COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
  1155. compat_ulong_t, nr_segments,
  1156. struct compat_kexec_segment __user *, segments,
  1157. compat_ulong_t, flags)
  1158. {
  1159. struct compat_kexec_segment in;
  1160. struct kexec_segment out, __user *ksegments;
  1161. unsigned long i, result;
  1162. /* Don't allow clients that don't understand the native
  1163. * architecture to do anything.
  1164. */
  1165. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  1166. return -EINVAL;
  1167. if (nr_segments > KEXEC_SEGMENT_MAX)
  1168. return -EINVAL;
  1169. ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
  1170. for (i = 0; i < nr_segments; i++) {
  1171. result = copy_from_user(&in, &segments[i], sizeof(in));
  1172. if (result)
  1173. return -EFAULT;
  1174. out.buf = compat_ptr(in.buf);
  1175. out.bufsz = in.bufsz;
  1176. out.mem = in.mem;
  1177. out.memsz = in.memsz;
  1178. result = copy_to_user(&ksegments[i], &out, sizeof(out));
  1179. if (result)
  1180. return -EFAULT;
  1181. }
  1182. return sys_kexec_load(entry, nr_segments, ksegments, flags);
  1183. }
  1184. #endif
  1185. #ifdef CONFIG_KEXEC_FILE
  1186. SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
  1187. unsigned long, cmdline_len, const char __user *, cmdline_ptr,
  1188. unsigned long, flags)
  1189. {
  1190. int ret = 0, i;
  1191. struct kimage **dest_image, *image;
  1192. /* We only trust the superuser with rebooting the system. */
  1193. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  1194. return -EPERM;
  1195. /* Make sure we have a legal set of flags */
  1196. if (flags != (flags & KEXEC_FILE_FLAGS))
  1197. return -EINVAL;
  1198. image = NULL;
  1199. if (!mutex_trylock(&kexec_mutex))
  1200. return -EBUSY;
  1201. dest_image = &kexec_image;
  1202. if (flags & KEXEC_FILE_ON_CRASH)
  1203. dest_image = &kexec_crash_image;
  1204. if (flags & KEXEC_FILE_UNLOAD)
  1205. goto exchange;
  1206. /*
  1207. * In case of crash, new kernel gets loaded in reserved region. It is
  1208. * same memory where old crash kernel might be loaded. Free any
  1209. * current crash dump kernel before we corrupt it.
  1210. */
  1211. if (flags & KEXEC_FILE_ON_CRASH)
  1212. kimage_free(xchg(&kexec_crash_image, NULL));
  1213. ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
  1214. cmdline_len, flags);
  1215. if (ret)
  1216. goto out;
  1217. ret = machine_kexec_prepare(image);
  1218. if (ret)
  1219. goto out;
  1220. ret = kexec_calculate_store_digests(image);
  1221. if (ret)
  1222. goto out;
  1223. for (i = 0; i < image->nr_segments; i++) {
  1224. struct kexec_segment *ksegment;
  1225. ksegment = &image->segment[i];
  1226. pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
  1227. i, ksegment->buf, ksegment->bufsz, ksegment->mem,
  1228. ksegment->memsz);
  1229. ret = kimage_load_segment(image, &image->segment[i]);
  1230. if (ret)
  1231. goto out;
  1232. }
  1233. kimage_terminate(image);
  1234. /*
  1235. * Free up any temporary buffers allocated which are not needed
  1236. * after image has been loaded
  1237. */
  1238. kimage_file_post_load_cleanup(image);
  1239. exchange:
  1240. image = xchg(dest_image, image);
  1241. out:
  1242. mutex_unlock(&kexec_mutex);
  1243. kimage_free(image);
  1244. return ret;
  1245. }
  1246. #endif /* CONFIG_KEXEC_FILE */
  1247. void crash_kexec(struct pt_regs *regs)
  1248. {
  1249. /* Take the kexec_mutex here to prevent sys_kexec_load
  1250. * running on one cpu from replacing the crash kernel
  1251. * we are using after a panic on a different cpu.
  1252. *
  1253. * If the crash kernel was not located in a fixed area
  1254. * of memory the xchg(&kexec_crash_image) would be
  1255. * sufficient. But since I reuse the memory...
  1256. */
  1257. if (mutex_trylock(&kexec_mutex)) {
  1258. if (kexec_crash_image) {
  1259. struct pt_regs fixed_regs;
  1260. crash_setup_regs(&fixed_regs, regs);
  1261. crash_save_vmcoreinfo();
  1262. machine_crash_shutdown(&fixed_regs);
  1263. machine_kexec(kexec_crash_image);
  1264. }
  1265. mutex_unlock(&kexec_mutex);
  1266. }
  1267. }
  1268. size_t crash_get_memory_size(void)
  1269. {
  1270. size_t size = 0;
  1271. mutex_lock(&kexec_mutex);
  1272. if (crashk_res.end != crashk_res.start)
  1273. size = resource_size(&crashk_res);
  1274. mutex_unlock(&kexec_mutex);
  1275. return size;
  1276. }
  1277. void __weak crash_free_reserved_phys_range(unsigned long begin,
  1278. unsigned long end)
  1279. {
  1280. unsigned long addr;
  1281. for (addr = begin; addr < end; addr += PAGE_SIZE)
  1282. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  1283. }
  1284. int crash_shrink_memory(unsigned long new_size)
  1285. {
  1286. int ret = 0;
  1287. unsigned long start, end;
  1288. unsigned long old_size;
  1289. struct resource *ram_res;
  1290. mutex_lock(&kexec_mutex);
  1291. if (kexec_crash_image) {
  1292. ret = -ENOENT;
  1293. goto unlock;
  1294. }
  1295. start = crashk_res.start;
  1296. end = crashk_res.end;
  1297. old_size = (end == 0) ? 0 : end - start + 1;
  1298. if (new_size >= old_size) {
  1299. ret = (new_size == old_size) ? 0 : -EINVAL;
  1300. goto unlock;
  1301. }
  1302. ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
  1303. if (!ram_res) {
  1304. ret = -ENOMEM;
  1305. goto unlock;
  1306. }
  1307. start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
  1308. end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
  1309. crash_map_reserved_pages();
  1310. crash_free_reserved_phys_range(end, crashk_res.end);
  1311. if ((start == end) && (crashk_res.parent != NULL))
  1312. release_resource(&crashk_res);
  1313. ram_res->start = end;
  1314. ram_res->end = crashk_res.end;
  1315. ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  1316. ram_res->name = "System RAM";
  1317. crashk_res.end = end - 1;
  1318. insert_resource(&iomem_resource, ram_res);
  1319. crash_unmap_reserved_pages();
  1320. unlock:
  1321. mutex_unlock(&kexec_mutex);
  1322. return ret;
  1323. }
  1324. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  1325. size_t data_len)
  1326. {
  1327. struct elf_note note;
  1328. note.n_namesz = strlen(name) + 1;
  1329. note.n_descsz = data_len;
  1330. note.n_type = type;
  1331. memcpy(buf, &note, sizeof(note));
  1332. buf += (sizeof(note) + 3)/4;
  1333. memcpy(buf, name, note.n_namesz);
  1334. buf += (note.n_namesz + 3)/4;
  1335. memcpy(buf, data, note.n_descsz);
  1336. buf += (note.n_descsz + 3)/4;
  1337. return buf;
  1338. }
  1339. static void final_note(u32 *buf)
  1340. {
  1341. struct elf_note note;
  1342. note.n_namesz = 0;
  1343. note.n_descsz = 0;
  1344. note.n_type = 0;
  1345. memcpy(buf, &note, sizeof(note));
  1346. }
  1347. void crash_save_cpu(struct pt_regs *regs, int cpu)
  1348. {
  1349. struct elf_prstatus prstatus;
  1350. u32 *buf;
  1351. if ((cpu < 0) || (cpu >= nr_cpu_ids))
  1352. return;
  1353. /* Using ELF notes here is opportunistic.
  1354. * I need a well defined structure format
  1355. * for the data I pass, and I need tags
  1356. * on the data to indicate what information I have
  1357. * squirrelled away. ELF notes happen to provide
  1358. * all of that, so there is no need to invent something new.
  1359. */
  1360. buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
  1361. if (!buf)
  1362. return;
  1363. memset(&prstatus, 0, sizeof(prstatus));
  1364. prstatus.pr_pid = current->pid;
  1365. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  1366. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  1367. &prstatus, sizeof(prstatus));
  1368. final_note(buf);
  1369. }
  1370. static int __init crash_notes_memory_init(void)
  1371. {
  1372. /* Allocate memory for saving cpu registers. */
  1373. crash_notes = alloc_percpu(note_buf_t);
  1374. if (!crash_notes) {
  1375. pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
  1376. return -ENOMEM;
  1377. }
  1378. return 0;
  1379. }
  1380. subsys_initcall(crash_notes_memory_init);
  1381. /*
  1382. * parsing the "crashkernel" commandline
  1383. *
  1384. * this code is intended to be called from architecture specific code
  1385. */
  1386. /*
  1387. * This function parses command lines in the format
  1388. *
  1389. * crashkernel=ramsize-range:size[,...][@offset]
  1390. *
  1391. * The function returns 0 on success and -EINVAL on failure.
  1392. */
  1393. static int __init parse_crashkernel_mem(char *cmdline,
  1394. unsigned long long system_ram,
  1395. unsigned long long *crash_size,
  1396. unsigned long long *crash_base)
  1397. {
  1398. char *cur = cmdline, *tmp;
  1399. /* for each entry of the comma-separated list */
  1400. do {
  1401. unsigned long long start, end = ULLONG_MAX, size;
  1402. /* get the start of the range */
  1403. start = memparse(cur, &tmp);
  1404. if (cur == tmp) {
  1405. pr_warn("crashkernel: Memory value expected\n");
  1406. return -EINVAL;
  1407. }
  1408. cur = tmp;
  1409. if (*cur != '-') {
  1410. pr_warn("crashkernel: '-' expected\n");
  1411. return -EINVAL;
  1412. }
  1413. cur++;
  1414. /* if no ':' is here, than we read the end */
  1415. if (*cur != ':') {
  1416. end = memparse(cur, &tmp);
  1417. if (cur == tmp) {
  1418. pr_warn("crashkernel: Memory value expected\n");
  1419. return -EINVAL;
  1420. }
  1421. cur = tmp;
  1422. if (end <= start) {
  1423. pr_warn("crashkernel: end <= start\n");
  1424. return -EINVAL;
  1425. }
  1426. }
  1427. if (*cur != ':') {
  1428. pr_warn("crashkernel: ':' expected\n");
  1429. return -EINVAL;
  1430. }
  1431. cur++;
  1432. size = memparse(cur, &tmp);
  1433. if (cur == tmp) {
  1434. pr_warn("Memory value expected\n");
  1435. return -EINVAL;
  1436. }
  1437. cur = tmp;
  1438. if (size >= system_ram) {
  1439. pr_warn("crashkernel: invalid size\n");
  1440. return -EINVAL;
  1441. }
  1442. /* match ? */
  1443. if (system_ram >= start && system_ram < end) {
  1444. *crash_size = size;
  1445. break;
  1446. }
  1447. } while (*cur++ == ',');
  1448. if (*crash_size > 0) {
  1449. while (*cur && *cur != ' ' && *cur != '@')
  1450. cur++;
  1451. if (*cur == '@') {
  1452. cur++;
  1453. *crash_base = memparse(cur, &tmp);
  1454. if (cur == tmp) {
  1455. pr_warn("Memory value expected after '@'\n");
  1456. return -EINVAL;
  1457. }
  1458. }
  1459. }
  1460. return 0;
  1461. }
  1462. /*
  1463. * That function parses "simple" (old) crashkernel command lines like
  1464. *
  1465. * crashkernel=size[@offset]
  1466. *
  1467. * It returns 0 on success and -EINVAL on failure.
  1468. */
  1469. static int __init parse_crashkernel_simple(char *cmdline,
  1470. unsigned long long *crash_size,
  1471. unsigned long long *crash_base)
  1472. {
  1473. char *cur = cmdline;
  1474. *crash_size = memparse(cmdline, &cur);
  1475. if (cmdline == cur) {
  1476. pr_warn("crashkernel: memory value expected\n");
  1477. return -EINVAL;
  1478. }
  1479. if (*cur == '@')
  1480. *crash_base = memparse(cur+1, &cur);
  1481. else if (*cur != ' ' && *cur != '\0') {
  1482. pr_warn("crashkernel: unrecognized char\n");
  1483. return -EINVAL;
  1484. }
  1485. return 0;
  1486. }
  1487. #define SUFFIX_HIGH 0
  1488. #define SUFFIX_LOW 1
  1489. #define SUFFIX_NULL 2
  1490. static __initdata char *suffix_tbl[] = {
  1491. [SUFFIX_HIGH] = ",high",
  1492. [SUFFIX_LOW] = ",low",
  1493. [SUFFIX_NULL] = NULL,
  1494. };
  1495. /*
  1496. * That function parses "suffix" crashkernel command lines like
  1497. *
  1498. * crashkernel=size,[high|low]
  1499. *
  1500. * It returns 0 on success and -EINVAL on failure.
  1501. */
  1502. static int __init parse_crashkernel_suffix(char *cmdline,
  1503. unsigned long long *crash_size,
  1504. const char *suffix)
  1505. {
  1506. char *cur = cmdline;
  1507. *crash_size = memparse(cmdline, &cur);
  1508. if (cmdline == cur) {
  1509. pr_warn("crashkernel: memory value expected\n");
  1510. return -EINVAL;
  1511. }
  1512. /* check with suffix */
  1513. if (strncmp(cur, suffix, strlen(suffix))) {
  1514. pr_warn("crashkernel: unrecognized char\n");
  1515. return -EINVAL;
  1516. }
  1517. cur += strlen(suffix);
  1518. if (*cur != ' ' && *cur != '\0') {
  1519. pr_warn("crashkernel: unrecognized char\n");
  1520. return -EINVAL;
  1521. }
  1522. return 0;
  1523. }
  1524. static __init char *get_last_crashkernel(char *cmdline,
  1525. const char *name,
  1526. const char *suffix)
  1527. {
  1528. char *p = cmdline, *ck_cmdline = NULL;
  1529. /* find crashkernel and use the last one if there are more */
  1530. p = strstr(p, name);
  1531. while (p) {
  1532. char *end_p = strchr(p, ' ');
  1533. char *q;
  1534. if (!end_p)
  1535. end_p = p + strlen(p);
  1536. if (!suffix) {
  1537. int i;
  1538. /* skip the one with any known suffix */
  1539. for (i = 0; suffix_tbl[i]; i++) {
  1540. q = end_p - strlen(suffix_tbl[i]);
  1541. if (!strncmp(q, suffix_tbl[i],
  1542. strlen(suffix_tbl[i])))
  1543. goto next;
  1544. }
  1545. ck_cmdline = p;
  1546. } else {
  1547. q = end_p - strlen(suffix);
  1548. if (!strncmp(q, suffix, strlen(suffix)))
  1549. ck_cmdline = p;
  1550. }
  1551. next:
  1552. p = strstr(p+1, name);
  1553. }
  1554. if (!ck_cmdline)
  1555. return NULL;
  1556. return ck_cmdline;
  1557. }
  1558. static int __init __parse_crashkernel(char *cmdline,
  1559. unsigned long long system_ram,
  1560. unsigned long long *crash_size,
  1561. unsigned long long *crash_base,
  1562. const char *name,
  1563. const char *suffix)
  1564. {
  1565. char *first_colon, *first_space;
  1566. char *ck_cmdline;
  1567. BUG_ON(!crash_size || !crash_base);
  1568. *crash_size = 0;
  1569. *crash_base = 0;
  1570. ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
  1571. if (!ck_cmdline)
  1572. return -EINVAL;
  1573. ck_cmdline += strlen(name);
  1574. if (suffix)
  1575. return parse_crashkernel_suffix(ck_cmdline, crash_size,
  1576. suffix);
  1577. /*
  1578. * if the commandline contains a ':', then that's the extended
  1579. * syntax -- if not, it must be the classic syntax
  1580. */
  1581. first_colon = strchr(ck_cmdline, ':');
  1582. first_space = strchr(ck_cmdline, ' ');
  1583. if (first_colon && (!first_space || first_colon < first_space))
  1584. return parse_crashkernel_mem(ck_cmdline, system_ram,
  1585. crash_size, crash_base);
  1586. return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
  1587. }
  1588. /*
  1589. * That function is the entry point for command line parsing and should be
  1590. * called from the arch-specific code.
  1591. */
  1592. int __init parse_crashkernel(char *cmdline,
  1593. unsigned long long system_ram,
  1594. unsigned long long *crash_size,
  1595. unsigned long long *crash_base)
  1596. {
  1597. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1598. "crashkernel=", NULL);
  1599. }
  1600. int __init parse_crashkernel_high(char *cmdline,
  1601. unsigned long long system_ram,
  1602. unsigned long long *crash_size,
  1603. unsigned long long *crash_base)
  1604. {
  1605. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1606. "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
  1607. }
  1608. int __init parse_crashkernel_low(char *cmdline,
  1609. unsigned long long system_ram,
  1610. unsigned long long *crash_size,
  1611. unsigned long long *crash_base)
  1612. {
  1613. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1614. "crashkernel=", suffix_tbl[SUFFIX_LOW]);
  1615. }
  1616. static void update_vmcoreinfo_note(void)
  1617. {
  1618. u32 *buf = vmcoreinfo_note;
  1619. if (!vmcoreinfo_size)
  1620. return;
  1621. buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
  1622. vmcoreinfo_size);
  1623. final_note(buf);
  1624. }
  1625. void crash_save_vmcoreinfo(void)
  1626. {
  1627. vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
  1628. update_vmcoreinfo_note();
  1629. }
  1630. void vmcoreinfo_append_str(const char *fmt, ...)
  1631. {
  1632. va_list args;
  1633. char buf[0x50];
  1634. size_t r;
  1635. va_start(args, fmt);
  1636. r = vscnprintf(buf, sizeof(buf), fmt, args);
  1637. va_end(args);
  1638. r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
  1639. memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
  1640. vmcoreinfo_size += r;
  1641. }
  1642. /*
  1643. * provide an empty default implementation here -- architecture
  1644. * code may override this
  1645. */
  1646. void __weak arch_crash_save_vmcoreinfo(void)
  1647. {}
  1648. unsigned long __weak paddr_vmcoreinfo_note(void)
  1649. {
  1650. return __pa((unsigned long)(char *)&vmcoreinfo_note);
  1651. }
  1652. static int __init crash_save_vmcoreinfo_init(void)
  1653. {
  1654. VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
  1655. VMCOREINFO_PAGESIZE(PAGE_SIZE);
  1656. VMCOREINFO_SYMBOL(init_uts_ns);
  1657. VMCOREINFO_SYMBOL(node_online_map);
  1658. #ifdef CONFIG_MMU
  1659. VMCOREINFO_SYMBOL(swapper_pg_dir);
  1660. #endif
  1661. VMCOREINFO_SYMBOL(_stext);
  1662. VMCOREINFO_SYMBOL(vmap_area_list);
  1663. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1664. VMCOREINFO_SYMBOL(mem_map);
  1665. VMCOREINFO_SYMBOL(contig_page_data);
  1666. #endif
  1667. #ifdef CONFIG_SPARSEMEM
  1668. VMCOREINFO_SYMBOL(mem_section);
  1669. VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
  1670. VMCOREINFO_STRUCT_SIZE(mem_section);
  1671. VMCOREINFO_OFFSET(mem_section, section_mem_map);
  1672. #endif
  1673. VMCOREINFO_STRUCT_SIZE(page);
  1674. VMCOREINFO_STRUCT_SIZE(pglist_data);
  1675. VMCOREINFO_STRUCT_SIZE(zone);
  1676. VMCOREINFO_STRUCT_SIZE(free_area);
  1677. VMCOREINFO_STRUCT_SIZE(list_head);
  1678. VMCOREINFO_SIZE(nodemask_t);
  1679. VMCOREINFO_OFFSET(page, flags);
  1680. VMCOREINFO_OFFSET(page, _count);
  1681. VMCOREINFO_OFFSET(page, mapping);
  1682. VMCOREINFO_OFFSET(page, lru);
  1683. VMCOREINFO_OFFSET(page, _mapcount);
  1684. VMCOREINFO_OFFSET(page, private);
  1685. VMCOREINFO_OFFSET(pglist_data, node_zones);
  1686. VMCOREINFO_OFFSET(pglist_data, nr_zones);
  1687. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1688. VMCOREINFO_OFFSET(pglist_data, node_mem_map);
  1689. #endif
  1690. VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
  1691. VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
  1692. VMCOREINFO_OFFSET(pglist_data, node_id);
  1693. VMCOREINFO_OFFSET(zone, free_area);
  1694. VMCOREINFO_OFFSET(zone, vm_stat);
  1695. VMCOREINFO_OFFSET(zone, spanned_pages);
  1696. VMCOREINFO_OFFSET(free_area, free_list);
  1697. VMCOREINFO_OFFSET(list_head, next);
  1698. VMCOREINFO_OFFSET(list_head, prev);
  1699. VMCOREINFO_OFFSET(vmap_area, va_start);
  1700. VMCOREINFO_OFFSET(vmap_area, list);
  1701. VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
  1702. log_buf_kexec_setup();
  1703. VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
  1704. VMCOREINFO_NUMBER(NR_FREE_PAGES);
  1705. VMCOREINFO_NUMBER(PG_lru);
  1706. VMCOREINFO_NUMBER(PG_private);
  1707. VMCOREINFO_NUMBER(PG_swapcache);
  1708. VMCOREINFO_NUMBER(PG_slab);
  1709. #ifdef CONFIG_MEMORY_FAILURE
  1710. VMCOREINFO_NUMBER(PG_hwpoison);
  1711. #endif
  1712. VMCOREINFO_NUMBER(PG_head_mask);
  1713. VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
  1714. #ifdef CONFIG_HUGETLBFS
  1715. VMCOREINFO_SYMBOL(free_huge_page);
  1716. #endif
  1717. arch_crash_save_vmcoreinfo();
  1718. update_vmcoreinfo_note();
  1719. return 0;
  1720. }
  1721. subsys_initcall(crash_save_vmcoreinfo_init);
  1722. #ifdef CONFIG_KEXEC_FILE
  1723. static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
  1724. struct kexec_buf *kbuf)
  1725. {
  1726. struct kimage *image = kbuf->image;
  1727. unsigned long temp_start, temp_end;
  1728. temp_end = min(end, kbuf->buf_max);
  1729. temp_start = temp_end - kbuf->memsz;
  1730. do {
  1731. /* align down start */
  1732. temp_start = temp_start & (~(kbuf->buf_align - 1));
  1733. if (temp_start < start || temp_start < kbuf->buf_min)
  1734. return 0;
  1735. temp_end = temp_start + kbuf->memsz - 1;
  1736. /*
  1737. * Make sure this does not conflict with any of existing
  1738. * segments
  1739. */
  1740. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  1741. temp_start = temp_start - PAGE_SIZE;
  1742. continue;
  1743. }
  1744. /* We found a suitable memory range */
  1745. break;
  1746. } while (1);
  1747. /* If we are here, we found a suitable memory range */
  1748. kbuf->mem = temp_start;
  1749. /* Success, stop navigating through remaining System RAM ranges */
  1750. return 1;
  1751. }
  1752. static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
  1753. struct kexec_buf *kbuf)
  1754. {
  1755. struct kimage *image = kbuf->image;
  1756. unsigned long temp_start, temp_end;
  1757. temp_start = max(start, kbuf->buf_min);
  1758. do {
  1759. temp_start = ALIGN(temp_start, kbuf->buf_align);
  1760. temp_end = temp_start + kbuf->memsz - 1;
  1761. if (temp_end > end || temp_end > kbuf->buf_max)
  1762. return 0;
  1763. /*
  1764. * Make sure this does not conflict with any of existing
  1765. * segments
  1766. */
  1767. if (kimage_is_destination_range(image, temp_start, temp_end)) {
  1768. temp_start = temp_start + PAGE_SIZE;
  1769. continue;
  1770. }
  1771. /* We found a suitable memory range */
  1772. break;
  1773. } while (1);
  1774. /* If we are here, we found a suitable memory range */
  1775. kbuf->mem = temp_start;
  1776. /* Success, stop navigating through remaining System RAM ranges */
  1777. return 1;
  1778. }
  1779. static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
  1780. {
  1781. struct kexec_buf *kbuf = (struct kexec_buf *)arg;
  1782. unsigned long sz = end - start + 1;
  1783. /* Returning 0 will take to next memory range */
  1784. if (sz < kbuf->memsz)
  1785. return 0;
  1786. if (end < kbuf->buf_min || start > kbuf->buf_max)
  1787. return 0;
  1788. /*
  1789. * Allocate memory top down with-in ram range. Otherwise bottom up
  1790. * allocation.
  1791. */
  1792. if (kbuf->top_down)
  1793. return locate_mem_hole_top_down(start, end, kbuf);
  1794. return locate_mem_hole_bottom_up(start, end, kbuf);
  1795. }
  1796. /*
  1797. * Helper function for placing a buffer in a kexec segment. This assumes
  1798. * that kexec_mutex is held.
  1799. */
  1800. int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
  1801. unsigned long memsz, unsigned long buf_align,
  1802. unsigned long buf_min, unsigned long buf_max,
  1803. bool top_down, unsigned long *load_addr)
  1804. {
  1805. struct kexec_segment *ksegment;
  1806. struct kexec_buf buf, *kbuf;
  1807. int ret;
  1808. /* Currently adding segment this way is allowed only in file mode */
  1809. if (!image->file_mode)
  1810. return -EINVAL;
  1811. if (image->nr_segments >= KEXEC_SEGMENT_MAX)
  1812. return -EINVAL;
  1813. /*
  1814. * Make sure we are not trying to add buffer after allocating
  1815. * control pages. All segments need to be placed first before
  1816. * any control pages are allocated. As control page allocation
  1817. * logic goes through list of segments to make sure there are
  1818. * no destination overlaps.
  1819. */
  1820. if (!list_empty(&image->control_pages)) {
  1821. WARN_ON(1);
  1822. return -EINVAL;
  1823. }
  1824. memset(&buf, 0, sizeof(struct kexec_buf));
  1825. kbuf = &buf;
  1826. kbuf->image = image;
  1827. kbuf->buffer = buffer;
  1828. kbuf->bufsz = bufsz;
  1829. kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
  1830. kbuf->buf_align = max(buf_align, PAGE_SIZE);
  1831. kbuf->buf_min = buf_min;
  1832. kbuf->buf_max = buf_max;
  1833. kbuf->top_down = top_down;
  1834. /* Walk the RAM ranges and allocate a suitable range for the buffer */
  1835. if (image->type == KEXEC_TYPE_CRASH)
  1836. ret = walk_iomem_res("Crash kernel",
  1837. IORESOURCE_MEM | IORESOURCE_BUSY,
  1838. crashk_res.start, crashk_res.end, kbuf,
  1839. locate_mem_hole_callback);
  1840. else
  1841. ret = walk_system_ram_res(0, -1, kbuf,
  1842. locate_mem_hole_callback);
  1843. if (ret != 1) {
  1844. /* A suitable memory range could not be found for buffer */
  1845. return -EADDRNOTAVAIL;
  1846. }
  1847. /* Found a suitable memory range */
  1848. ksegment = &image->segment[image->nr_segments];
  1849. ksegment->kbuf = kbuf->buffer;
  1850. ksegment->bufsz = kbuf->bufsz;
  1851. ksegment->mem = kbuf->mem;
  1852. ksegment->memsz = kbuf->memsz;
  1853. image->nr_segments++;
  1854. *load_addr = ksegment->mem;
  1855. return 0;
  1856. }
  1857. /* Calculate and store the digest of segments */
  1858. static int kexec_calculate_store_digests(struct kimage *image)
  1859. {
  1860. struct crypto_shash *tfm;
  1861. struct shash_desc *desc;
  1862. int ret = 0, i, j, zero_buf_sz, sha_region_sz;
  1863. size_t desc_size, nullsz;
  1864. char *digest;
  1865. void *zero_buf;
  1866. struct kexec_sha_region *sha_regions;
  1867. struct purgatory_info *pi = &image->purgatory_info;
  1868. zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
  1869. zero_buf_sz = PAGE_SIZE;
  1870. tfm = crypto_alloc_shash("sha256", 0, 0);
  1871. if (IS_ERR(tfm)) {
  1872. ret = PTR_ERR(tfm);
  1873. goto out;
  1874. }
  1875. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  1876. desc = kzalloc(desc_size, GFP_KERNEL);
  1877. if (!desc) {
  1878. ret = -ENOMEM;
  1879. goto out_free_tfm;
  1880. }
  1881. sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
  1882. sha_regions = vzalloc(sha_region_sz);
  1883. if (!sha_regions)
  1884. goto out_free_desc;
  1885. desc->tfm = tfm;
  1886. desc->flags = 0;
  1887. ret = crypto_shash_init(desc);
  1888. if (ret < 0)
  1889. goto out_free_sha_regions;
  1890. digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  1891. if (!digest) {
  1892. ret = -ENOMEM;
  1893. goto out_free_sha_regions;
  1894. }
  1895. for (j = i = 0; i < image->nr_segments; i++) {
  1896. struct kexec_segment *ksegment;
  1897. ksegment = &image->segment[i];
  1898. /*
  1899. * Skip purgatory as it will be modified once we put digest
  1900. * info in purgatory.
  1901. */
  1902. if (ksegment->kbuf == pi->purgatory_buf)
  1903. continue;
  1904. ret = crypto_shash_update(desc, ksegment->kbuf,
  1905. ksegment->bufsz);
  1906. if (ret)
  1907. break;
  1908. /*
  1909. * Assume rest of the buffer is filled with zero and
  1910. * update digest accordingly.
  1911. */
  1912. nullsz = ksegment->memsz - ksegment->bufsz;
  1913. while (nullsz) {
  1914. unsigned long bytes = nullsz;
  1915. if (bytes > zero_buf_sz)
  1916. bytes = zero_buf_sz;
  1917. ret = crypto_shash_update(desc, zero_buf, bytes);
  1918. if (ret)
  1919. break;
  1920. nullsz -= bytes;
  1921. }
  1922. if (ret)
  1923. break;
  1924. sha_regions[j].start = ksegment->mem;
  1925. sha_regions[j].len = ksegment->memsz;
  1926. j++;
  1927. }
  1928. if (!ret) {
  1929. ret = crypto_shash_final(desc, digest);
  1930. if (ret)
  1931. goto out_free_digest;
  1932. ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
  1933. sha_regions, sha_region_sz, 0);
  1934. if (ret)
  1935. goto out_free_digest;
  1936. ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
  1937. digest, SHA256_DIGEST_SIZE, 0);
  1938. if (ret)
  1939. goto out_free_digest;
  1940. }
  1941. out_free_digest:
  1942. kfree(digest);
  1943. out_free_sha_regions:
  1944. vfree(sha_regions);
  1945. out_free_desc:
  1946. kfree(desc);
  1947. out_free_tfm:
  1948. kfree(tfm);
  1949. out:
  1950. return ret;
  1951. }
  1952. /* Actually load purgatory. Lot of code taken from kexec-tools */
  1953. static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
  1954. unsigned long max, int top_down)
  1955. {
  1956. struct purgatory_info *pi = &image->purgatory_info;
  1957. unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
  1958. unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
  1959. unsigned char *buf_addr, *src;
  1960. int i, ret = 0, entry_sidx = -1;
  1961. const Elf_Shdr *sechdrs_c;
  1962. Elf_Shdr *sechdrs = NULL;
  1963. void *purgatory_buf = NULL;
  1964. /*
  1965. * sechdrs_c points to section headers in purgatory and are read
  1966. * only. No modifications allowed.
  1967. */
  1968. sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
  1969. /*
  1970. * We can not modify sechdrs_c[] and its fields. It is read only.
  1971. * Copy it over to a local copy where one can store some temporary
  1972. * data and free it at the end. We need to modify ->sh_addr and
  1973. * ->sh_offset fields to keep track of permanent and temporary
  1974. * locations of sections.
  1975. */
  1976. sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  1977. if (!sechdrs)
  1978. return -ENOMEM;
  1979. memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
  1980. /*
  1981. * We seem to have multiple copies of sections. First copy is which
  1982. * is embedded in kernel in read only section. Some of these sections
  1983. * will be copied to a temporary buffer and relocated. And these
  1984. * sections will finally be copied to their final destination at
  1985. * segment load time.
  1986. *
  1987. * Use ->sh_offset to reflect section address in memory. It will
  1988. * point to original read only copy if section is not allocatable.
  1989. * Otherwise it will point to temporary copy which will be relocated.
  1990. *
  1991. * Use ->sh_addr to contain final address of the section where it
  1992. * will go during execution time.
  1993. */
  1994. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  1995. if (sechdrs[i].sh_type == SHT_NOBITS)
  1996. continue;
  1997. sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
  1998. sechdrs[i].sh_offset;
  1999. }
  2000. /*
  2001. * Identify entry point section and make entry relative to section
  2002. * start.
  2003. */
  2004. entry = pi->ehdr->e_entry;
  2005. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2006. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  2007. continue;
  2008. if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
  2009. continue;
  2010. /* Make entry section relative */
  2011. if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
  2012. ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
  2013. pi->ehdr->e_entry)) {
  2014. entry_sidx = i;
  2015. entry -= sechdrs[i].sh_addr;
  2016. break;
  2017. }
  2018. }
  2019. /* Determine how much memory is needed to load relocatable object. */
  2020. buf_align = 1;
  2021. bss_align = 1;
  2022. buf_sz = 0;
  2023. bss_sz = 0;
  2024. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2025. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  2026. continue;
  2027. align = sechdrs[i].sh_addralign;
  2028. if (sechdrs[i].sh_type != SHT_NOBITS) {
  2029. if (buf_align < align)
  2030. buf_align = align;
  2031. buf_sz = ALIGN(buf_sz, align);
  2032. buf_sz += sechdrs[i].sh_size;
  2033. } else {
  2034. /* bss section */
  2035. if (bss_align < align)
  2036. bss_align = align;
  2037. bss_sz = ALIGN(bss_sz, align);
  2038. bss_sz += sechdrs[i].sh_size;
  2039. }
  2040. }
  2041. /* Determine the bss padding required to align bss properly */
  2042. bss_pad = 0;
  2043. if (buf_sz & (bss_align - 1))
  2044. bss_pad = bss_align - (buf_sz & (bss_align - 1));
  2045. memsz = buf_sz + bss_pad + bss_sz;
  2046. /* Allocate buffer for purgatory */
  2047. purgatory_buf = vzalloc(buf_sz);
  2048. if (!purgatory_buf) {
  2049. ret = -ENOMEM;
  2050. goto out;
  2051. }
  2052. if (buf_align < bss_align)
  2053. buf_align = bss_align;
  2054. /* Add buffer to segment list */
  2055. ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
  2056. buf_align, min, max, top_down,
  2057. &pi->purgatory_load_addr);
  2058. if (ret)
  2059. goto out;
  2060. /* Load SHF_ALLOC sections */
  2061. buf_addr = purgatory_buf;
  2062. load_addr = curr_load_addr = pi->purgatory_load_addr;
  2063. bss_addr = load_addr + buf_sz + bss_pad;
  2064. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2065. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  2066. continue;
  2067. align = sechdrs[i].sh_addralign;
  2068. if (sechdrs[i].sh_type != SHT_NOBITS) {
  2069. curr_load_addr = ALIGN(curr_load_addr, align);
  2070. offset = curr_load_addr - load_addr;
  2071. /* We already modifed ->sh_offset to keep src addr */
  2072. src = (char *) sechdrs[i].sh_offset;
  2073. memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
  2074. /* Store load address and source address of section */
  2075. sechdrs[i].sh_addr = curr_load_addr;
  2076. /*
  2077. * This section got copied to temporary buffer. Update
  2078. * ->sh_offset accordingly.
  2079. */
  2080. sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
  2081. /* Advance to the next address */
  2082. curr_load_addr += sechdrs[i].sh_size;
  2083. } else {
  2084. bss_addr = ALIGN(bss_addr, align);
  2085. sechdrs[i].sh_addr = bss_addr;
  2086. bss_addr += sechdrs[i].sh_size;
  2087. }
  2088. }
  2089. /* Update entry point based on load address of text section */
  2090. if (entry_sidx >= 0)
  2091. entry += sechdrs[entry_sidx].sh_addr;
  2092. /* Make kernel jump to purgatory after shutdown */
  2093. image->start = entry;
  2094. /* Used later to get/set symbol values */
  2095. pi->sechdrs = sechdrs;
  2096. /*
  2097. * Used later to identify which section is purgatory and skip it
  2098. * from checksumming.
  2099. */
  2100. pi->purgatory_buf = purgatory_buf;
  2101. return ret;
  2102. out:
  2103. vfree(sechdrs);
  2104. vfree(purgatory_buf);
  2105. return ret;
  2106. }
  2107. static int kexec_apply_relocations(struct kimage *image)
  2108. {
  2109. int i, ret;
  2110. struct purgatory_info *pi = &image->purgatory_info;
  2111. Elf_Shdr *sechdrs = pi->sechdrs;
  2112. /* Apply relocations */
  2113. for (i = 0; i < pi->ehdr->e_shnum; i++) {
  2114. Elf_Shdr *section, *symtab;
  2115. if (sechdrs[i].sh_type != SHT_RELA &&
  2116. sechdrs[i].sh_type != SHT_REL)
  2117. continue;
  2118. /*
  2119. * For section of type SHT_RELA/SHT_REL,
  2120. * ->sh_link contains section header index of associated
  2121. * symbol table. And ->sh_info contains section header
  2122. * index of section to which relocations apply.
  2123. */
  2124. if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
  2125. sechdrs[i].sh_link >= pi->ehdr->e_shnum)
  2126. return -ENOEXEC;
  2127. section = &sechdrs[sechdrs[i].sh_info];
  2128. symtab = &sechdrs[sechdrs[i].sh_link];
  2129. if (!(section->sh_flags & SHF_ALLOC))
  2130. continue;
  2131. /*
  2132. * symtab->sh_link contain section header index of associated
  2133. * string table.
  2134. */
  2135. if (symtab->sh_link >= pi->ehdr->e_shnum)
  2136. /* Invalid section number? */
  2137. continue;
  2138. /*
  2139. * Respective architecture needs to provide support for applying
  2140. * relocations of type SHT_RELA/SHT_REL.
  2141. */
  2142. if (sechdrs[i].sh_type == SHT_RELA)
  2143. ret = arch_kexec_apply_relocations_add(pi->ehdr,
  2144. sechdrs, i);
  2145. else if (sechdrs[i].sh_type == SHT_REL)
  2146. ret = arch_kexec_apply_relocations(pi->ehdr,
  2147. sechdrs, i);
  2148. if (ret)
  2149. return ret;
  2150. }
  2151. return 0;
  2152. }
  2153. /* Load relocatable purgatory object and relocate it appropriately */
  2154. int kexec_load_purgatory(struct kimage *image, unsigned long min,
  2155. unsigned long max, int top_down,
  2156. unsigned long *load_addr)
  2157. {
  2158. struct purgatory_info *pi = &image->purgatory_info;
  2159. int ret;
  2160. if (kexec_purgatory_size <= 0)
  2161. return -EINVAL;
  2162. if (kexec_purgatory_size < sizeof(Elf_Ehdr))
  2163. return -ENOEXEC;
  2164. pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
  2165. if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
  2166. || pi->ehdr->e_type != ET_REL
  2167. || !elf_check_arch(pi->ehdr)
  2168. || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
  2169. return -ENOEXEC;
  2170. if (pi->ehdr->e_shoff >= kexec_purgatory_size
  2171. || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
  2172. kexec_purgatory_size - pi->ehdr->e_shoff))
  2173. return -ENOEXEC;
  2174. ret = __kexec_load_purgatory(image, min, max, top_down);
  2175. if (ret)
  2176. return ret;
  2177. ret = kexec_apply_relocations(image);
  2178. if (ret)
  2179. goto out;
  2180. *load_addr = pi->purgatory_load_addr;
  2181. return 0;
  2182. out:
  2183. vfree(pi->sechdrs);
  2184. vfree(pi->purgatory_buf);
  2185. return ret;
  2186. }
  2187. static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
  2188. const char *name)
  2189. {
  2190. Elf_Sym *syms;
  2191. Elf_Shdr *sechdrs;
  2192. Elf_Ehdr *ehdr;
  2193. int i, k;
  2194. const char *strtab;
  2195. if (!pi->sechdrs || !pi->ehdr)
  2196. return NULL;
  2197. sechdrs = pi->sechdrs;
  2198. ehdr = pi->ehdr;
  2199. for (i = 0; i < ehdr->e_shnum; i++) {
  2200. if (sechdrs[i].sh_type != SHT_SYMTAB)
  2201. continue;
  2202. if (sechdrs[i].sh_link >= ehdr->e_shnum)
  2203. /* Invalid strtab section number */
  2204. continue;
  2205. strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
  2206. syms = (Elf_Sym *)sechdrs[i].sh_offset;
  2207. /* Go through symbols for a match */
  2208. for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
  2209. if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
  2210. continue;
  2211. if (strcmp(strtab + syms[k].st_name, name) != 0)
  2212. continue;
  2213. if (syms[k].st_shndx == SHN_UNDEF ||
  2214. syms[k].st_shndx >= ehdr->e_shnum) {
  2215. pr_debug("Symbol: %s has bad section index %d.\n",
  2216. name, syms[k].st_shndx);
  2217. return NULL;
  2218. }
  2219. /* Found the symbol we are looking for */
  2220. return &syms[k];
  2221. }
  2222. }
  2223. return NULL;
  2224. }
  2225. void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
  2226. {
  2227. struct purgatory_info *pi = &image->purgatory_info;
  2228. Elf_Sym *sym;
  2229. Elf_Shdr *sechdr;
  2230. sym = kexec_purgatory_find_symbol(pi, name);
  2231. if (!sym)
  2232. return ERR_PTR(-EINVAL);
  2233. sechdr = &pi->sechdrs[sym->st_shndx];
  2234. /*
  2235. * Returns the address where symbol will finally be loaded after
  2236. * kexec_load_segment()
  2237. */
  2238. return (void *)(sechdr->sh_addr + sym->st_value);
  2239. }
  2240. /*
  2241. * Get or set value of a symbol. If "get_value" is true, symbol value is
  2242. * returned in buf otherwise symbol value is set based on value in buf.
  2243. */
  2244. int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
  2245. void *buf, unsigned int size, bool get_value)
  2246. {
  2247. Elf_Sym *sym;
  2248. Elf_Shdr *sechdrs;
  2249. struct purgatory_info *pi = &image->purgatory_info;
  2250. char *sym_buf;
  2251. sym = kexec_purgatory_find_symbol(pi, name);
  2252. if (!sym)
  2253. return -EINVAL;
  2254. if (sym->st_size != size) {
  2255. pr_err("symbol %s size mismatch: expected %lu actual %u\n",
  2256. name, (unsigned long)sym->st_size, size);
  2257. return -EINVAL;
  2258. }
  2259. sechdrs = pi->sechdrs;
  2260. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  2261. pr_err("symbol %s is in a bss section. Cannot %s\n", name,
  2262. get_value ? "get" : "set");
  2263. return -EINVAL;
  2264. }
  2265. sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
  2266. sym->st_value;
  2267. if (get_value)
  2268. memcpy((void *)buf, sym_buf, size);
  2269. else
  2270. memcpy((void *)sym_buf, buf, size);
  2271. return 0;
  2272. }
  2273. #endif /* CONFIG_KEXEC_FILE */
  2274. /*
  2275. * Move into place and start executing a preloaded standalone
  2276. * executable. If nothing was preloaded return an error.
  2277. */
  2278. int kernel_kexec(void)
  2279. {
  2280. int error = 0;
  2281. if (!mutex_trylock(&kexec_mutex))
  2282. return -EBUSY;
  2283. if (!kexec_image) {
  2284. error = -EINVAL;
  2285. goto Unlock;
  2286. }
  2287. #ifdef CONFIG_KEXEC_JUMP
  2288. if (kexec_image->preserve_context) {
  2289. lock_system_sleep();
  2290. pm_prepare_console();
  2291. error = freeze_processes();
  2292. if (error) {
  2293. error = -EBUSY;
  2294. goto Restore_console;
  2295. }
  2296. suspend_console();
  2297. error = dpm_suspend_start(PMSG_FREEZE);
  2298. if (error)
  2299. goto Resume_console;
  2300. /* At this point, dpm_suspend_start() has been called,
  2301. * but *not* dpm_suspend_end(). We *must* call
  2302. * dpm_suspend_end() now. Otherwise, drivers for
  2303. * some devices (e.g. interrupt controllers) become
  2304. * desynchronized with the actual state of the
  2305. * hardware at resume time, and evil weirdness ensues.
  2306. */
  2307. error = dpm_suspend_end(PMSG_FREEZE);
  2308. if (error)
  2309. goto Resume_devices;
  2310. error = disable_nonboot_cpus();
  2311. if (error)
  2312. goto Enable_cpus;
  2313. local_irq_disable();
  2314. error = syscore_suspend();
  2315. if (error)
  2316. goto Enable_irqs;
  2317. } else
  2318. #endif
  2319. {
  2320. kexec_in_progress = true;
  2321. kernel_restart_prepare(NULL);
  2322. migrate_to_reboot_cpu();
  2323. /*
  2324. * migrate_to_reboot_cpu() disables CPU hotplug assuming that
  2325. * no further code needs to use CPU hotplug (which is true in
  2326. * the reboot case). However, the kexec path depends on using
  2327. * CPU hotplug again; so re-enable it here.
  2328. */
  2329. cpu_hotplug_enable();
  2330. pr_emerg("Starting new kernel\n");
  2331. machine_shutdown();
  2332. }
  2333. machine_kexec(kexec_image);
  2334. #ifdef CONFIG_KEXEC_JUMP
  2335. if (kexec_image->preserve_context) {
  2336. syscore_resume();
  2337. Enable_irqs:
  2338. local_irq_enable();
  2339. Enable_cpus:
  2340. enable_nonboot_cpus();
  2341. dpm_resume_start(PMSG_RESTORE);
  2342. Resume_devices:
  2343. dpm_resume_end(PMSG_RESTORE);
  2344. Resume_console:
  2345. resume_console();
  2346. thaw_processes();
  2347. Restore_console:
  2348. pm_restore_console();
  2349. unlock_system_sleep();
  2350. }
  2351. #endif
  2352. Unlock:
  2353. mutex_unlock(&kexec_mutex);
  2354. return error;
  2355. }