kexec.c 68 KB

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