kexec.c 68 KB

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