subr_hibernate.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  1. /* $OpenBSD: subr_hibernate.c,v 1.116 2015/05/04 02:18:05 mlarkin Exp $ */
  2. /*
  3. * Copyright (c) 2011 Ariane van der Steldt <ariane@stack.nl>
  4. * Copyright (c) 2011 Mike Larkin <mlarkin@openbsd.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/hibernate.h>
  19. #include <sys/malloc.h>
  20. #include <sys/param.h>
  21. #include <sys/tree.h>
  22. #include <sys/systm.h>
  23. #include <sys/disklabel.h>
  24. #include <sys/disk.h>
  25. #include <sys/conf.h>
  26. #include <sys/buf.h>
  27. #include <sys/fcntl.h>
  28. #include <sys/stat.h>
  29. #include <sys/atomic.h>
  30. #include <uvm/uvm.h>
  31. #include <uvm/uvm_swap.h>
  32. #include <machine/hibernate.h>
  33. /*
  34. * Hibernate piglet layout information
  35. *
  36. * The piglet is a scratch area of memory allocated by the suspending kernel.
  37. * Its phys and virt addrs are recorded in the signature block. The piglet is
  38. * used to guarantee an unused area of memory that can be used by the resuming
  39. * kernel for various things. The piglet is excluded during unpack operations.
  40. * The piglet size is presently 4*HIBERNATE_CHUNK_SIZE (typically 4*4MB).
  41. *
  42. * Offset from piglet_base Purpose
  43. * ----------------------------------------------------------------------------
  44. * 0 Private page for suspend I/O write functions
  45. * 1*PAGE_SIZE I/O page used during hibernate suspend
  46. * 2*PAGE_SIZE I/O page used during hibernate suspend
  47. * 3*PAGE_SIZE copy page used during hibernate suspend
  48. * 4*PAGE_SIZE final chunk ordering list (24 pages)
  49. * 28*PAGE_SIZE RLE utility page
  50. * 29*PAGE_SIZE start of hiballoc area
  51. * 30*PAGE_SIZE preserved entropy
  52. * 110*PAGE_SIZE end of hiballoc area (80 pages)
  53. * ... unused
  54. * HIBERNATE_CHUNK_SIZE start of hibernate chunk table
  55. * 2*HIBERNATE_CHUNK_SIZE bounce area for chunks being unpacked
  56. * 4*HIBERNATE_CHUNK_SIZE end of piglet
  57. */
  58. /* Temporary vaddr ranges used during hibernate */
  59. vaddr_t hibernate_temp_page;
  60. vaddr_t hibernate_copy_page;
  61. vaddr_t hibernate_rle_page;
  62. /* Hibernate info as read from disk during resume */
  63. union hibernate_info disk_hib;
  64. /*
  65. * Global copy of the pig start address. This needs to be a global as we
  66. * switch stacks after computing it - it can't be stored on the stack.
  67. */
  68. paddr_t global_pig_start;
  69. /*
  70. * Global copies of the piglet start addresses (PA/VA). We store these
  71. * as globals to avoid having to carry them around as parameters, as the
  72. * piglet is allocated early and freed late - its lifecycle extends beyond
  73. * that of the hibernate info union which is calculated on suspend/resume.
  74. */
  75. vaddr_t global_piglet_va;
  76. paddr_t global_piglet_pa;
  77. /* #define HIB_DEBUG */
  78. #ifdef HIB_DEBUG
  79. int hib_debug = 99;
  80. #define DPRINTF(x...) do { if (hib_debug) printf(x); } while (0)
  81. #define DNPRINTF(n,x...) do { if (hib_debug > (n)) printf(x); } while (0)
  82. #else
  83. #define DPRINTF(x...)
  84. #define DNPRINTF(n,x...)
  85. #endif
  86. #ifndef NO_PROPOLICE
  87. extern long __guard_local;
  88. #endif /* ! NO_PROPOLICE */
  89. void hibernate_copy_chunk_to_piglet(paddr_t, vaddr_t, size_t);
  90. int hibernate_calc_rle(paddr_t, paddr_t);
  91. int hibernate_write_rle(union hibernate_info *, paddr_t, paddr_t, daddr_t *,
  92. size_t *);
  93. #define MAX_RLE (HIBERNATE_CHUNK_SIZE / PAGE_SIZE)
  94. /*
  95. * Hib alloc enforced alignment.
  96. */
  97. #define HIB_ALIGN 8 /* bytes alignment */
  98. /*
  99. * sizeof builtin operation, but with alignment constraint.
  100. */
  101. #define HIB_SIZEOF(_type) roundup(sizeof(_type), HIB_ALIGN)
  102. struct hiballoc_entry {
  103. size_t hibe_use;
  104. size_t hibe_space;
  105. RB_ENTRY(hiballoc_entry) hibe_entry;
  106. };
  107. /*
  108. * Sort hibernate memory ranges by ascending PA
  109. */
  110. void
  111. hibernate_sort_ranges(union hibernate_info *hib_info)
  112. {
  113. int i, j;
  114. struct hibernate_memory_range *ranges;
  115. paddr_t base, end;
  116. ranges = hib_info->ranges;
  117. for (i = 1; i < hib_info->nranges; i++) {
  118. j = i;
  119. while (j > 0 && ranges[j - 1].base > ranges[j].base) {
  120. base = ranges[j].base;
  121. end = ranges[j].end;
  122. ranges[j].base = ranges[j - 1].base;
  123. ranges[j].end = ranges[j - 1].end;
  124. ranges[j - 1].base = base;
  125. ranges[j - 1].end = end;
  126. j--;
  127. }
  128. }
  129. }
  130. /*
  131. * Compare hiballoc entries based on the address they manage.
  132. *
  133. * Since the address is fixed, relative to struct hiballoc_entry,
  134. * we just compare the hiballoc_entry pointers.
  135. */
  136. static __inline int
  137. hibe_cmp(struct hiballoc_entry *l, struct hiballoc_entry *r)
  138. {
  139. return l < r ? -1 : (l > r);
  140. }
  141. RB_PROTOTYPE(hiballoc_addr, hiballoc_entry, hibe_entry, hibe_cmp)
  142. /*
  143. * Given a hiballoc entry, return the address it manages.
  144. */
  145. static __inline void *
  146. hib_entry_to_addr(struct hiballoc_entry *entry)
  147. {
  148. caddr_t addr;
  149. addr = (caddr_t)entry;
  150. addr += HIB_SIZEOF(struct hiballoc_entry);
  151. return addr;
  152. }
  153. /*
  154. * Given an address, find the hiballoc that corresponds.
  155. */
  156. static __inline struct hiballoc_entry*
  157. hib_addr_to_entry(void *addr_param)
  158. {
  159. caddr_t addr;
  160. addr = (caddr_t)addr_param;
  161. addr -= HIB_SIZEOF(struct hiballoc_entry);
  162. return (struct hiballoc_entry*)addr;
  163. }
  164. RB_GENERATE(hiballoc_addr, hiballoc_entry, hibe_entry, hibe_cmp)
  165. /*
  166. * Allocate memory from the arena.
  167. *
  168. * Returns NULL if no memory is available.
  169. */
  170. void *
  171. hib_alloc(struct hiballoc_arena *arena, size_t alloc_sz)
  172. {
  173. struct hiballoc_entry *entry, *new_entry;
  174. size_t find_sz;
  175. /*
  176. * Enforce alignment of HIB_ALIGN bytes.
  177. *
  178. * Note that, because the entry is put in front of the allocation,
  179. * 0-byte allocations are guaranteed a unique address.
  180. */
  181. alloc_sz = roundup(alloc_sz, HIB_ALIGN);
  182. /*
  183. * Find an entry with hibe_space >= find_sz.
  184. *
  185. * If the root node is not large enough, we switch to tree traversal.
  186. * Because all entries are made at the bottom of the free space,
  187. * traversal from the end has a slightly better chance of yielding
  188. * a sufficiently large space.
  189. */
  190. find_sz = alloc_sz + HIB_SIZEOF(struct hiballoc_entry);
  191. entry = RB_ROOT(&arena->hib_addrs);
  192. if (entry != NULL && entry->hibe_space < find_sz) {
  193. RB_FOREACH_REVERSE(entry, hiballoc_addr, &arena->hib_addrs) {
  194. if (entry->hibe_space >= find_sz)
  195. break;
  196. }
  197. }
  198. /*
  199. * Insufficient or too fragmented memory.
  200. */
  201. if (entry == NULL)
  202. return NULL;
  203. /*
  204. * Create new entry in allocated space.
  205. */
  206. new_entry = (struct hiballoc_entry*)(
  207. (caddr_t)hib_entry_to_addr(entry) + entry->hibe_use);
  208. new_entry->hibe_space = entry->hibe_space - find_sz;
  209. new_entry->hibe_use = alloc_sz;
  210. /*
  211. * Insert entry.
  212. */
  213. if (RB_INSERT(hiballoc_addr, &arena->hib_addrs, new_entry) != NULL)
  214. panic("hib_alloc: insert failure");
  215. entry->hibe_space = 0;
  216. /* Return address managed by entry. */
  217. return hib_entry_to_addr(new_entry);
  218. }
  219. void
  220. hib_getentropy(char **bufp, size_t *bufplen)
  221. {
  222. if (!bufp || !bufplen)
  223. return;
  224. *bufp = (char *)(global_piglet_va + (29 * PAGE_SIZE));
  225. *bufplen = PAGE_SIZE;
  226. }
  227. /*
  228. * Free a pointer previously allocated from this arena.
  229. *
  230. * If addr is NULL, this will be silently accepted.
  231. */
  232. void
  233. hib_free(struct hiballoc_arena *arena, void *addr)
  234. {
  235. struct hiballoc_entry *entry, *prev;
  236. if (addr == NULL)
  237. return;
  238. /*
  239. * Derive entry from addr and check it is really in this arena.
  240. */
  241. entry = hib_addr_to_entry(addr);
  242. if (RB_FIND(hiballoc_addr, &arena->hib_addrs, entry) != entry)
  243. panic("hib_free: freed item %p not in hib arena", addr);
  244. /*
  245. * Give the space in entry to its predecessor.
  246. *
  247. * If entry has no predecessor, change its used space into free space
  248. * instead.
  249. */
  250. prev = RB_PREV(hiballoc_addr, &arena->hib_addrs, entry);
  251. if (prev != NULL &&
  252. (void *)((caddr_t)prev + HIB_SIZEOF(struct hiballoc_entry) +
  253. prev->hibe_use + prev->hibe_space) == entry) {
  254. /* Merge entry. */
  255. RB_REMOVE(hiballoc_addr, &arena->hib_addrs, entry);
  256. prev->hibe_space += HIB_SIZEOF(struct hiballoc_entry) +
  257. entry->hibe_use + entry->hibe_space;
  258. } else {
  259. /* Flip used memory to free space. */
  260. entry->hibe_space += entry->hibe_use;
  261. entry->hibe_use = 0;
  262. }
  263. }
  264. /*
  265. * Initialize hiballoc.
  266. *
  267. * The allocator will manage memmory at ptr, which is len bytes.
  268. */
  269. int
  270. hiballoc_init(struct hiballoc_arena *arena, void *p_ptr, size_t p_len)
  271. {
  272. struct hiballoc_entry *entry;
  273. caddr_t ptr;
  274. size_t len;
  275. RB_INIT(&arena->hib_addrs);
  276. /*
  277. * Hib allocator enforces HIB_ALIGN alignment.
  278. * Fixup ptr and len.
  279. */
  280. ptr = (caddr_t)roundup((vaddr_t)p_ptr, HIB_ALIGN);
  281. len = p_len - ((size_t)ptr - (size_t)p_ptr);
  282. len &= ~((size_t)HIB_ALIGN - 1);
  283. /*
  284. * Insufficient memory to be able to allocate and also do bookkeeping.
  285. */
  286. if (len <= HIB_SIZEOF(struct hiballoc_entry))
  287. return ENOMEM;
  288. /*
  289. * Create entry describing space.
  290. */
  291. entry = (struct hiballoc_entry*)ptr;
  292. entry->hibe_use = 0;
  293. entry->hibe_space = len - HIB_SIZEOF(struct hiballoc_entry);
  294. RB_INSERT(hiballoc_addr, &arena->hib_addrs, entry);
  295. return 0;
  296. }
  297. /*
  298. * Zero all free memory.
  299. */
  300. void
  301. uvm_pmr_zero_everything(void)
  302. {
  303. struct uvm_pmemrange *pmr;
  304. struct vm_page *pg;
  305. int i;
  306. uvm_lock_fpageq();
  307. TAILQ_FOREACH(pmr, &uvm.pmr_control.use, pmr_use) {
  308. /* Zero single pages. */
  309. while ((pg = TAILQ_FIRST(&pmr->single[UVM_PMR_MEMTYPE_DIRTY]))
  310. != NULL) {
  311. uvm_pmr_remove(pmr, pg);
  312. uvm_pagezero(pg);
  313. atomic_setbits_int(&pg->pg_flags, PG_ZERO);
  314. uvmexp.zeropages++;
  315. uvm_pmr_insert(pmr, pg, 0);
  316. }
  317. /* Zero multi page ranges. */
  318. while ((pg = RB_ROOT(&pmr->size[UVM_PMR_MEMTYPE_DIRTY]))
  319. != NULL) {
  320. pg--; /* Size tree always has second page. */
  321. uvm_pmr_remove(pmr, pg);
  322. for (i = 0; i < pg->fpgsz; i++) {
  323. uvm_pagezero(&pg[i]);
  324. atomic_setbits_int(&pg[i].pg_flags, PG_ZERO);
  325. uvmexp.zeropages++;
  326. }
  327. uvm_pmr_insert(pmr, pg, 0);
  328. }
  329. }
  330. uvm_unlock_fpageq();
  331. }
  332. /*
  333. * Mark all memory as dirty.
  334. *
  335. * Used to inform the system that the clean memory isn't clean for some
  336. * reason, for example because we just came back from hibernate.
  337. */
  338. void
  339. uvm_pmr_dirty_everything(void)
  340. {
  341. struct uvm_pmemrange *pmr;
  342. struct vm_page *pg;
  343. int i;
  344. uvm_lock_fpageq();
  345. TAILQ_FOREACH(pmr, &uvm.pmr_control.use, pmr_use) {
  346. /* Dirty single pages. */
  347. while ((pg = TAILQ_FIRST(&pmr->single[UVM_PMR_MEMTYPE_ZERO]))
  348. != NULL) {
  349. uvm_pmr_remove(pmr, pg);
  350. atomic_clearbits_int(&pg->pg_flags, PG_ZERO);
  351. uvm_pmr_insert(pmr, pg, 0);
  352. }
  353. /* Dirty multi page ranges. */
  354. while ((pg = RB_ROOT(&pmr->size[UVM_PMR_MEMTYPE_ZERO]))
  355. != NULL) {
  356. pg--; /* Size tree always has second page. */
  357. uvm_pmr_remove(pmr, pg);
  358. for (i = 0; i < pg->fpgsz; i++)
  359. atomic_clearbits_int(&pg[i].pg_flags, PG_ZERO);
  360. uvm_pmr_insert(pmr, pg, 0);
  361. }
  362. }
  363. uvmexp.zeropages = 0;
  364. uvm_unlock_fpageq();
  365. }
  366. /*
  367. * Allocate an area that can hold sz bytes and doesn't overlap with
  368. * the piglet at piglet_pa.
  369. */
  370. int
  371. uvm_pmr_alloc_pig(paddr_t *pa, psize_t sz, paddr_t piglet_pa)
  372. {
  373. struct uvm_constraint_range pig_constraint;
  374. struct kmem_pa_mode kp_pig = {
  375. .kp_constraint = &pig_constraint,
  376. .kp_maxseg = 1
  377. };
  378. vaddr_t va;
  379. sz = round_page(sz);
  380. pig_constraint.ucr_low = piglet_pa + 4 * HIBERNATE_CHUNK_SIZE;
  381. pig_constraint.ucr_high = -1;
  382. va = (vaddr_t)km_alloc(sz, &kv_any, &kp_pig, &kd_nowait);
  383. if (va == 0) {
  384. pig_constraint.ucr_low = 0;
  385. pig_constraint.ucr_high = piglet_pa - 1;
  386. va = (vaddr_t)km_alloc(sz, &kv_any, &kp_pig, &kd_nowait);
  387. if (va == 0)
  388. return ENOMEM;
  389. }
  390. pmap_extract(pmap_kernel(), va, pa);
  391. return 0;
  392. }
  393. /*
  394. * Allocate a piglet area.
  395. *
  396. * This needs to be in DMA-safe memory.
  397. * Piglets are aligned.
  398. *
  399. * sz and align in bytes.
  400. *
  401. * The call will sleep for the pagedaemon to attempt to free memory.
  402. * The pagedaemon may decide its not possible to free enough memory, causing
  403. * the allocation to fail.
  404. */
  405. int
  406. uvm_pmr_alloc_piglet(vaddr_t *va, paddr_t *pa, vsize_t sz, paddr_t align)
  407. {
  408. struct kmem_pa_mode kp_piglet = {
  409. .kp_constraint = &dma_constraint,
  410. .kp_align = align,
  411. .kp_maxseg = 1
  412. };
  413. /* Ensure align is a power of 2 */
  414. KASSERT((align & (align - 1)) == 0);
  415. /*
  416. * Fixup arguments: align must be at least PAGE_SIZE,
  417. * sz will be converted to pagecount, since that is what
  418. * pmemrange uses internally.
  419. */
  420. if (align < PAGE_SIZE)
  421. kp_piglet.kp_align = PAGE_SIZE;
  422. sz = round_page(sz);
  423. *va = (vaddr_t)km_alloc(sz, &kv_any, &kp_piglet, &kd_nowait);
  424. if (*va == 0)
  425. return ENOMEM;
  426. pmap_extract(pmap_kernel(), *va, pa);
  427. return 0;
  428. }
  429. /*
  430. * Free a piglet area.
  431. */
  432. void
  433. uvm_pmr_free_piglet(vaddr_t va, vsize_t sz)
  434. {
  435. /*
  436. * Fix parameters.
  437. */
  438. sz = round_page(sz);
  439. /*
  440. * Free the physical and virtual memory.
  441. */
  442. km_free((void *)va, sz, &kv_any, &kp_dma_contig);
  443. }
  444. /*
  445. * Physmem RLE compression support.
  446. *
  447. * Given a physical page address, return the number of pages starting at the
  448. * address that are free. Clamps to the number of pages in
  449. * HIBERNATE_CHUNK_SIZE. Returns 0 if the page at addr is not free.
  450. */
  451. int
  452. uvm_page_rle(paddr_t addr)
  453. {
  454. struct vm_page *pg, *pg_end;
  455. struct vm_physseg *vmp;
  456. int pseg_idx, off_idx;
  457. pseg_idx = vm_physseg_find(atop(addr), &off_idx);
  458. if (pseg_idx == -1)
  459. return 0;
  460. vmp = &vm_physmem[pseg_idx];
  461. pg = &vmp->pgs[off_idx];
  462. if (!(pg->pg_flags & PQ_FREE))
  463. return 0;
  464. /*
  465. * Search for the first non-free page after pg.
  466. * Note that the page may not be the first page in a free pmemrange,
  467. * therefore pg->fpgsz cannot be used.
  468. */
  469. for (pg_end = pg; pg_end <= vmp->lastpg &&
  470. (pg_end->pg_flags & PQ_FREE) == PQ_FREE; pg_end++)
  471. ;
  472. return min((pg_end - pg), HIBERNATE_CHUNK_SIZE/PAGE_SIZE);
  473. }
  474. /*
  475. * Fills out the hibernate_info union pointed to by hib
  476. * with information about this machine (swap signature block
  477. * offsets, number of memory ranges, kernel in use, etc)
  478. */
  479. int
  480. get_hibernate_info(union hibernate_info *hib, int suspend)
  481. {
  482. struct disklabel dl;
  483. char err_string[128], *dl_ret;
  484. #ifndef NO_PROPOLICE
  485. /* Save propolice guard */
  486. hib->guard = __guard_local;
  487. #endif /* ! NO_PROPOLICE */
  488. /* Determine I/O function to use */
  489. hib->io_func = get_hibernate_io_function(swdevt[0].sw_dev);
  490. if (hib->io_func == NULL)
  491. return (1);
  492. /* Calculate hibernate device */
  493. hib->dev = swdevt[0].sw_dev;
  494. /* Read disklabel (used to calculate signature and image offsets) */
  495. dl_ret = disk_readlabel(&dl, hib->dev, err_string, sizeof(err_string));
  496. if (dl_ret) {
  497. printf("Hibernate error reading disklabel: %s\n", dl_ret);
  498. return (1);
  499. }
  500. /* Make sure we have a swap partition. */
  501. if (dl.d_partitions[1].p_fstype != FS_SWAP ||
  502. DL_GETPSIZE(&dl.d_partitions[1]) == 0)
  503. return (1);
  504. /* Make sure the signature can fit in one block */
  505. if (sizeof(union hibernate_info) > DEV_BSIZE)
  506. return (1);
  507. /* Magic number */
  508. hib->magic = HIBERNATE_MAGIC;
  509. /* Calculate signature block location */
  510. hib->sig_offset = DL_GETPSIZE(&dl.d_partitions[1]) -
  511. sizeof(union hibernate_info)/DEV_BSIZE;
  512. /* Stash kernel version information */
  513. memset(&hib->kernel_version, 0, 128);
  514. bcopy(version, &hib->kernel_version,
  515. min(strlen(version), sizeof(hib->kernel_version)-1));
  516. if (suspend) {
  517. /* Grab the previously-allocated piglet addresses */
  518. hib->piglet_va = global_piglet_va;
  519. hib->piglet_pa = global_piglet_pa;
  520. hib->io_page = (void *)hib->piglet_va;
  521. /*
  522. * Initialization of the hibernate IO function for drivers
  523. * that need to do prep work (such as allocating memory or
  524. * setting up data structures that cannot safely be done
  525. * during suspend without causing side effects). There is
  526. * a matching HIB_DONE call performed after the write is
  527. * completed.
  528. */
  529. if (hib->io_func(hib->dev, DL_GETPOFFSET(&dl.d_partitions[1]),
  530. (vaddr_t)NULL, DL_GETPSIZE(&dl.d_partitions[1]),
  531. HIB_INIT, hib->io_page))
  532. goto fail;
  533. } else {
  534. /*
  535. * Resuming kernels use a regular private page for the driver
  536. * No need to free this I/O page as it will vanish as part of
  537. * the resume.
  538. */
  539. hib->io_page = malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT);
  540. if (!hib->io_page)
  541. goto fail;
  542. }
  543. if (get_hibernate_info_md(hib))
  544. goto fail;
  545. return (0);
  546. fail:
  547. return (1);
  548. }
  549. /*
  550. * Allocate nitems*size bytes from the hiballoc area presently in use
  551. */
  552. void *
  553. hibernate_zlib_alloc(void *unused, int nitems, int size)
  554. {
  555. struct hibernate_zlib_state *hibernate_state;
  556. hibernate_state =
  557. (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
  558. return hib_alloc(&hibernate_state->hiballoc_arena, nitems*size);
  559. }
  560. /*
  561. * Free the memory pointed to by addr in the hiballoc area presently in
  562. * use
  563. */
  564. void
  565. hibernate_zlib_free(void *unused, void *addr)
  566. {
  567. struct hibernate_zlib_state *hibernate_state;
  568. hibernate_state =
  569. (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
  570. hib_free(&hibernate_state->hiballoc_arena, addr);
  571. }
  572. /*
  573. * Inflate next page of data from the image stream.
  574. * The rle parameter is modified on exit to contain the number of pages to
  575. * skip in the output stream (or 0 if this page was inflated into).
  576. *
  577. * Returns 0 if the stream contains additional data, or 1 if the stream is
  578. * finished.
  579. */
  580. int
  581. hibernate_inflate_page(int *rle)
  582. {
  583. struct hibernate_zlib_state *hibernate_state;
  584. int i;
  585. hibernate_state =
  586. (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
  587. /* Set up the stream for RLE code inflate */
  588. hibernate_state->hib_stream.next_out = (unsigned char *)rle;
  589. hibernate_state->hib_stream.avail_out = sizeof(*rle);
  590. /* Inflate RLE code */
  591. i = inflate(&hibernate_state->hib_stream, Z_SYNC_FLUSH);
  592. if (i != Z_OK && i != Z_STREAM_END) {
  593. /*
  594. * XXX - this will likely reboot/hang most machines
  595. * since the console output buffer will be unmapped,
  596. * but there's not much else we can do here.
  597. */
  598. panic("rle inflate stream error");
  599. }
  600. if (hibernate_state->hib_stream.avail_out != 0) {
  601. /*
  602. * XXX - this will likely reboot/hang most machines
  603. * since the console output buffer will be unmapped,
  604. * but there's not much else we can do here.
  605. */
  606. panic("rle short inflate error");
  607. }
  608. if (*rle < 0 || *rle > 1024) {
  609. /*
  610. * XXX - this will likely reboot/hang most machines
  611. * since the console output buffer will be unmapped,
  612. * but there's not much else we can do here.
  613. */
  614. panic("invalid rle count");
  615. }
  616. if (i == Z_STREAM_END)
  617. return (1);
  618. if (*rle != 0)
  619. return (0);
  620. /* Set up the stream for page inflate */
  621. hibernate_state->hib_stream.next_out =
  622. (unsigned char *)HIBERNATE_INFLATE_PAGE;
  623. hibernate_state->hib_stream.avail_out = PAGE_SIZE;
  624. /* Process next block of data */
  625. i = inflate(&hibernate_state->hib_stream, Z_SYNC_FLUSH);
  626. if (i != Z_OK && i != Z_STREAM_END) {
  627. /*
  628. * XXX - this will likely reboot/hang most machines
  629. * since the console output buffer will be unmapped,
  630. * but there's not much else we can do here.
  631. */
  632. panic("inflate error");
  633. }
  634. /* We should always have extracted a full page ... */
  635. if (hibernate_state->hib_stream.avail_out != 0) {
  636. /*
  637. * XXX - this will likely reboot/hang most machines
  638. * since the console output buffer will be unmapped,
  639. * but there's not much else we can do here.
  640. */
  641. panic("incomplete page");
  642. }
  643. return (i == Z_STREAM_END);
  644. }
  645. /*
  646. * Inflate size bytes from src into dest, skipping any pages in
  647. * [src..dest] that are special (see hibernate_inflate_skip)
  648. *
  649. * This function executes while using the resume-time stack
  650. * and pmap, and therefore cannot use ddb/printf/etc. Doing so
  651. * will likely hang or reset the machine since the console output buffer
  652. * will be unmapped.
  653. */
  654. void
  655. hibernate_inflate_region(union hibernate_info *hib, paddr_t dest,
  656. paddr_t src, size_t size)
  657. {
  658. int end_stream = 0, rle;
  659. struct hibernate_zlib_state *hibernate_state;
  660. hibernate_state =
  661. (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
  662. hibernate_state->hib_stream.next_in = (unsigned char *)src;
  663. hibernate_state->hib_stream.avail_in = size;
  664. do {
  665. /*
  666. * Is this a special page? If yes, redirect the
  667. * inflate output to a scratch page (eg, discard it)
  668. */
  669. if (hibernate_inflate_skip(hib, dest)) {
  670. hibernate_enter_resume_mapping(
  671. HIBERNATE_INFLATE_PAGE,
  672. HIBERNATE_INFLATE_PAGE, 0);
  673. } else {
  674. hibernate_enter_resume_mapping(
  675. HIBERNATE_INFLATE_PAGE, dest, 0);
  676. }
  677. hibernate_flush();
  678. end_stream = hibernate_inflate_page(&rle);
  679. if (rle == 0)
  680. dest += PAGE_SIZE;
  681. else
  682. dest += (rle * PAGE_SIZE);
  683. } while (!end_stream);
  684. }
  685. /*
  686. * deflate from src into the I/O page, up to 'remaining' bytes
  687. *
  688. * Returns number of input bytes consumed, and may reset
  689. * the 'remaining' parameter if not all the output space was consumed
  690. * (this information is needed to know how much to write to disk
  691. */
  692. size_t
  693. hibernate_deflate(union hibernate_info *hib, paddr_t src,
  694. size_t *remaining)
  695. {
  696. vaddr_t hibernate_io_page = hib->piglet_va + PAGE_SIZE;
  697. struct hibernate_zlib_state *hibernate_state;
  698. hibernate_state =
  699. (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
  700. /* Set up the stream for deflate */
  701. hibernate_state->hib_stream.next_in = (unsigned char *)src;
  702. hibernate_state->hib_stream.avail_in = PAGE_SIZE - (src & PAGE_MASK);
  703. hibernate_state->hib_stream.next_out =
  704. (unsigned char *)hibernate_io_page + (PAGE_SIZE - *remaining);
  705. hibernate_state->hib_stream.avail_out = *remaining;
  706. /* Process next block of data */
  707. if (deflate(&hibernate_state->hib_stream, Z_SYNC_FLUSH) != Z_OK)
  708. panic("hibernate zlib deflate error");
  709. /* Update pointers and return number of bytes consumed */
  710. *remaining = hibernate_state->hib_stream.avail_out;
  711. return (PAGE_SIZE - (src & PAGE_MASK)) -
  712. hibernate_state->hib_stream.avail_in;
  713. }
  714. /*
  715. * Write the hibernation information specified in hiber_info
  716. * to the location in swap previously calculated (last block of
  717. * swap), called the "signature block".
  718. */
  719. int
  720. hibernate_write_signature(union hibernate_info *hib)
  721. {
  722. /* Write hibernate info to disk */
  723. return (hib->io_func(hib->dev, hib->sig_offset,
  724. (vaddr_t)hib, DEV_BSIZE, HIB_W,
  725. hib->io_page));
  726. }
  727. /*
  728. * Write the memory chunk table to the area in swap immediately
  729. * preceding the signature block. The chunk table is stored
  730. * in the piglet when this function is called. Returns errno.
  731. */
  732. int
  733. hibernate_write_chunktable(union hibernate_info *hib)
  734. {
  735. vaddr_t hibernate_chunk_table_start;
  736. size_t hibernate_chunk_table_size;
  737. int i, err;
  738. hibernate_chunk_table_size = HIBERNATE_CHUNK_TABLE_SIZE;
  739. hibernate_chunk_table_start = hib->piglet_va +
  740. HIBERNATE_CHUNK_SIZE;
  741. /* Write chunk table */
  742. for (i = 0; i < hibernate_chunk_table_size; i += MAXPHYS) {
  743. if ((err = hib->io_func(hib->dev,
  744. hib->chunktable_offset + (i/DEV_BSIZE),
  745. (vaddr_t)(hibernate_chunk_table_start + i),
  746. MAXPHYS, HIB_W, hib->io_page))) {
  747. DPRINTF("chunktable write error: %d\n", err);
  748. return (err);
  749. }
  750. }
  751. return (0);
  752. }
  753. /*
  754. * Write an empty hiber_info to the swap signature block, which is
  755. * guaranteed to not match any valid hib.
  756. */
  757. int
  758. hibernate_clear_signature(void)
  759. {
  760. union hibernate_info blank_hiber_info;
  761. union hibernate_info hib;
  762. /* Zero out a blank hiber_info */
  763. memset(&blank_hiber_info, 0, sizeof(union hibernate_info));
  764. /* Get the signature block location */
  765. if (get_hibernate_info(&hib, 0))
  766. return (1);
  767. /* Write (zeroed) hibernate info to disk */
  768. DPRINTF("clearing hibernate signature block location: %lld\n",
  769. hib.sig_offset);
  770. if (hibernate_block_io(&hib,
  771. hib.sig_offset,
  772. DEV_BSIZE, (vaddr_t)&blank_hiber_info, 1))
  773. printf("Warning: could not clear hibernate signature\n");
  774. return (0);
  775. }
  776. /*
  777. * Compare two hibernate_infos to determine if they are the same (eg,
  778. * we should be performing a hibernate resume on this machine.
  779. * Not all fields are checked - just enough to verify that the machine
  780. * has the same memory configuration and kernel as the one that
  781. * wrote the signature previously.
  782. */
  783. int
  784. hibernate_compare_signature(union hibernate_info *mine,
  785. union hibernate_info *disk)
  786. {
  787. u_int i;
  788. if (mine->nranges != disk->nranges) {
  789. DPRINTF("hibernate memory range count mismatch\n");
  790. return (1);
  791. }
  792. if (strcmp(mine->kernel_version, disk->kernel_version) != 0) {
  793. DPRINTF("hibernate kernel version mismatch\n");
  794. return (1);
  795. }
  796. for (i = 0; i < mine->nranges; i++) {
  797. if ((mine->ranges[i].base != disk->ranges[i].base) ||
  798. (mine->ranges[i].end != disk->ranges[i].end) ) {
  799. DPRINTF("hib range %d mismatch [%p-%p != %p-%p]\n",
  800. i,
  801. (void *)mine->ranges[i].base,
  802. (void *)mine->ranges[i].end,
  803. (void *)disk->ranges[i].base,
  804. (void *)disk->ranges[i].end);
  805. return (1);
  806. }
  807. }
  808. return (0);
  809. }
  810. /*
  811. * Transfers xfer_size bytes between the hibernate device specified in
  812. * hib_info at offset blkctr and the vaddr specified at dest.
  813. *
  814. * Separate offsets and pages are used to handle misaligned reads (reads
  815. * that span a page boundary).
  816. *
  817. * blkctr specifies a relative offset (relative to the start of swap),
  818. * not an absolute disk offset
  819. *
  820. */
  821. int
  822. hibernate_block_io(union hibernate_info *hib, daddr_t blkctr,
  823. size_t xfer_size, vaddr_t dest, int iswrite)
  824. {
  825. struct buf *bp;
  826. struct bdevsw *bdsw;
  827. int error;
  828. bp = geteblk(xfer_size);
  829. bdsw = &bdevsw[major(hib->dev)];
  830. error = (*bdsw->d_open)(hib->dev, FREAD, S_IFCHR, curproc);
  831. if (error) {
  832. printf("hibernate_block_io open failed\n");
  833. return (1);
  834. }
  835. if (iswrite)
  836. bcopy((caddr_t)dest, bp->b_data, xfer_size);
  837. bp->b_bcount = xfer_size;
  838. bp->b_blkno = blkctr;
  839. CLR(bp->b_flags, B_READ | B_WRITE | B_DONE);
  840. SET(bp->b_flags, B_BUSY | (iswrite ? B_WRITE : B_READ) | B_RAW);
  841. bp->b_dev = hib->dev;
  842. (*bdsw->d_strategy)(bp);
  843. error = biowait(bp);
  844. if (error) {
  845. printf("hib block_io biowait error %d blk %lld size %zu\n",
  846. error, (long long)blkctr, xfer_size);
  847. error = (*bdsw->d_close)(hib->dev, 0, S_IFCHR,
  848. curproc);
  849. if (error)
  850. printf("hibernate_block_io error close failed\n");
  851. return (1);
  852. }
  853. error = (*bdsw->d_close)(hib->dev, FREAD, S_IFCHR, curproc);
  854. if (error) {
  855. printf("hibernate_block_io close failed\n");
  856. return (1);
  857. }
  858. if (!iswrite)
  859. bcopy(bp->b_data, (caddr_t)dest, xfer_size);
  860. bp->b_flags |= B_INVAL;
  861. brelse(bp);
  862. return (0);
  863. }
  864. /*
  865. * Preserve one page worth of random data, generated from the resuming
  866. * kernel's arc4random. After resume, this preserved entropy can be used
  867. * to further improve the un-hibernated machine's entropy pool. This
  868. * random data is stored in the piglet, which is preserved across the
  869. * unpack operation, and is restored later in the resume process (see
  870. * hib_getentropy)
  871. */
  872. void
  873. hibernate_preserve_entropy(union hibernate_info *hib)
  874. {
  875. void *entropy;
  876. entropy = km_alloc(PAGE_SIZE, &kv_any, &kp_none, &kd_nowait);
  877. if (!entropy)
  878. return;
  879. pmap_activate(curproc);
  880. pmap_kenter_pa((vaddr_t)entropy,
  881. (paddr_t)(hib->piglet_pa + (29 * PAGE_SIZE)),
  882. PROT_READ | PROT_WRITE);
  883. arc4random_buf((void *)entropy, PAGE_SIZE);
  884. pmap_kremove((vaddr_t)entropy, PAGE_SIZE);
  885. km_free(entropy, PAGE_SIZE, &kv_any, &kp_none);
  886. }
  887. /*
  888. * Reads the signature block from swap, checks against the current machine's
  889. * information. If the information matches, perform a resume by reading the
  890. * saved image into the pig area, and unpacking.
  891. *
  892. * Must be called with interrupts enabled.
  893. */
  894. void
  895. hibernate_resume(void)
  896. {
  897. union hibernate_info hib;
  898. int s;
  899. /* Get current running machine's hibernate info */
  900. memset(&hib, 0, sizeof(hib));
  901. if (get_hibernate_info(&hib, 0)) {
  902. DPRINTF("couldn't retrieve machine's hibernate info\n");
  903. return;
  904. }
  905. /* Read hibernate info from disk */
  906. s = splbio();
  907. DPRINTF("reading hibernate signature block location: %lld\n",
  908. hib.sig_offset);
  909. if (hibernate_block_io(&hib,
  910. hib.sig_offset,
  911. DEV_BSIZE, (vaddr_t)&disk_hib, 0)) {
  912. DPRINTF("error in hibernate read");
  913. splx(s);
  914. return;
  915. }
  916. /* Check magic number */
  917. if (disk_hib.magic != HIBERNATE_MAGIC) {
  918. DPRINTF("wrong magic number in hibernate signature: %x\n",
  919. disk_hib.magic);
  920. splx(s);
  921. return;
  922. }
  923. /*
  924. * We (possibly) found a hibernate signature. Clear signature first,
  925. * to prevent accidental resume or endless resume cycles later.
  926. */
  927. if (hibernate_clear_signature()) {
  928. DPRINTF("error clearing hibernate signature block\n");
  929. splx(s);
  930. return;
  931. }
  932. /*
  933. * If on-disk and in-memory hibernate signatures match,
  934. * this means we should do a resume from hibernate.
  935. */
  936. if (hibernate_compare_signature(&hib, &disk_hib)) {
  937. DPRINTF("mismatched hibernate signature block\n");
  938. splx(s);
  939. return;
  940. }
  941. #ifdef MULTIPROCESSOR
  942. /* XXX - if we fail later, we may need to rehatch APs on some archs */
  943. DPRINTF("hibernate: quiescing APs\n");
  944. hibernate_quiesce_cpus();
  945. #endif /* MULTIPROCESSOR */
  946. /* Read the image from disk into the image (pig) area */
  947. if (hibernate_read_image(&disk_hib))
  948. goto fail;
  949. DPRINTF("hibernate: quiescing devices\n");
  950. if (config_suspend_all(DVACT_QUIESCE) != 0)
  951. goto fail;
  952. (void) splhigh();
  953. hibernate_disable_intr_machdep();
  954. cold = 1;
  955. DPRINTF("hibernate: suspending devices\n");
  956. if (config_suspend_all(DVACT_SUSPEND) != 0) {
  957. cold = 0;
  958. hibernate_enable_intr_machdep();
  959. goto fail;
  960. }
  961. hibernate_preserve_entropy(&disk_hib);
  962. printf("Unpacking image...\n");
  963. /* Switch stacks */
  964. DPRINTF("hibernate: switching stacks\n");
  965. hibernate_switch_stack_machdep();
  966. #ifndef NO_PROPOLICE
  967. /* Start using suspended kernel's propolice guard */
  968. __guard_local = disk_hib.guard;
  969. #endif /* ! NO_PROPOLICE */
  970. /* Unpack and resume */
  971. hibernate_unpack_image(&disk_hib);
  972. fail:
  973. splx(s);
  974. printf("\nUnable to resume hibernated image\n");
  975. }
  976. /*
  977. * Unpack image from pig area to original location by looping through the
  978. * list of output chunks in the order they should be restored (fchunks).
  979. *
  980. * Note that due to the stack smash protector and the fact that we have
  981. * switched stacks, it is not permitted to return from this function.
  982. */
  983. void
  984. hibernate_unpack_image(union hibernate_info *hib)
  985. {
  986. struct hibernate_disk_chunk *chunks;
  987. union hibernate_info local_hib;
  988. paddr_t image_cur = global_pig_start;
  989. short i, *fchunks;
  990. char *pva;
  991. /* Piglet will be identity mapped (VA == PA) */
  992. pva = (char *)hib->piglet_pa;
  993. fchunks = (short *)(pva + (4 * PAGE_SIZE));
  994. chunks = (struct hibernate_disk_chunk *)(pva + HIBERNATE_CHUNK_SIZE);
  995. /* Can't use hiber_info that's passed in after this point */
  996. bcopy(hib, &local_hib, sizeof(union hibernate_info));
  997. /* VA == PA */
  998. local_hib.piglet_va = local_hib.piglet_pa;
  999. /*
  1000. * Point of no return. Once we pass this point, only kernel code can
  1001. * be accessed. No global variables or other kernel data structures
  1002. * are guaranteed to be coherent after unpack starts.
  1003. *
  1004. * The image is now in high memory (pig area), we unpack from the pig
  1005. * to the correct location in memory. We'll eventually end up copying
  1006. * on top of ourself, but we are assured the kernel code here is the
  1007. * same between the hibernated and resuming kernel, and we are running
  1008. * on our own stack, so the overwrite is ok.
  1009. */
  1010. DPRINTF("hibernate: activating alt. pagetable and starting unpack\n");
  1011. hibernate_activate_resume_pt_machdep();
  1012. for (i = 0; i < local_hib.chunk_ctr; i++) {
  1013. /* Reset zlib for inflate */
  1014. if (hibernate_zlib_reset(&local_hib, 0) != Z_OK)
  1015. panic("hibernate failed to reset zlib for inflate");
  1016. hibernate_process_chunk(&local_hib, &chunks[fchunks[i]],
  1017. image_cur);
  1018. image_cur += chunks[fchunks[i]].compressed_size;
  1019. }
  1020. /*
  1021. * Resume the loaded kernel by jumping to the MD resume vector.
  1022. * We won't be returning from this call.
  1023. */
  1024. hibernate_resume_machdep();
  1025. }
  1026. /*
  1027. * Bounce a compressed image chunk to the piglet, entering mappings for the
  1028. * copied pages as needed
  1029. */
  1030. void
  1031. hibernate_copy_chunk_to_piglet(paddr_t img_cur, vaddr_t piglet, size_t size)
  1032. {
  1033. size_t ct, ofs;
  1034. paddr_t src = img_cur;
  1035. vaddr_t dest = piglet;
  1036. /* Copy first partial page */
  1037. ct = (PAGE_SIZE) - (src & PAGE_MASK);
  1038. ofs = (src & PAGE_MASK);
  1039. if (ct < PAGE_SIZE) {
  1040. hibernate_enter_resume_mapping(HIBERNATE_INFLATE_PAGE,
  1041. (src - ofs), 0);
  1042. hibernate_flush();
  1043. bcopy((caddr_t)(HIBERNATE_INFLATE_PAGE + ofs), (caddr_t)dest, ct);
  1044. src += ct;
  1045. dest += ct;
  1046. }
  1047. /* Copy remaining pages */
  1048. while (src < size + img_cur) {
  1049. hibernate_enter_resume_mapping(HIBERNATE_INFLATE_PAGE, src, 0);
  1050. hibernate_flush();
  1051. ct = PAGE_SIZE;
  1052. bcopy((caddr_t)(HIBERNATE_INFLATE_PAGE), (caddr_t)dest, ct);
  1053. hibernate_flush();
  1054. src += ct;
  1055. dest += ct;
  1056. }
  1057. }
  1058. /*
  1059. * Process a chunk by bouncing it to the piglet, followed by unpacking
  1060. */
  1061. void
  1062. hibernate_process_chunk(union hibernate_info *hib,
  1063. struct hibernate_disk_chunk *chunk, paddr_t img_cur)
  1064. {
  1065. char *pva = (char *)hib->piglet_va;
  1066. hibernate_copy_chunk_to_piglet(img_cur,
  1067. (vaddr_t)(pva + (HIBERNATE_CHUNK_SIZE * 2)), chunk->compressed_size);
  1068. hibernate_inflate_region(hib, chunk->base,
  1069. (vaddr_t)(pva + (HIBERNATE_CHUNK_SIZE * 2)),
  1070. chunk->compressed_size);
  1071. }
  1072. /*
  1073. * Calculate RLE component for 'inaddr'. Clamps to max RLE pages between
  1074. * inaddr and range_end.
  1075. */
  1076. int
  1077. hibernate_calc_rle(paddr_t inaddr, paddr_t range_end)
  1078. {
  1079. int rle;
  1080. rle = uvm_page_rle(inaddr);
  1081. KASSERT(rle >= 0 && rle <= MAX_RLE);
  1082. /* Clamp RLE to range end */
  1083. if (rle > 0 && inaddr + (rle * PAGE_SIZE) > range_end)
  1084. rle = (range_end - inaddr) / PAGE_SIZE;
  1085. return (rle);
  1086. }
  1087. /*
  1088. * Write the RLE byte for page at 'inaddr' to the output stream.
  1089. * Returns the number of pages to be skipped at 'inaddr'.
  1090. */
  1091. int
  1092. hibernate_write_rle(union hibernate_info *hib, paddr_t inaddr,
  1093. paddr_t range_end, daddr_t *blkctr,
  1094. size_t *out_remaining)
  1095. {
  1096. int rle, err, *rleloc;
  1097. struct hibernate_zlib_state *hibernate_state;
  1098. vaddr_t hibernate_io_page = hib->piglet_va + PAGE_SIZE;
  1099. hibernate_state =
  1100. (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
  1101. rle = hibernate_calc_rle(inaddr, range_end);
  1102. rleloc = (int *)hibernate_rle_page + MAX_RLE - 1;
  1103. *rleloc = rle;
  1104. /* Deflate the RLE byte into the stream */
  1105. hibernate_deflate(hib, (paddr_t)rleloc, out_remaining);
  1106. /* Did we fill the output page? If so, flush to disk */
  1107. if (*out_remaining == 0) {
  1108. if ((err = hib->io_func(hib->dev, *blkctr + hib->image_offset,
  1109. (vaddr_t)hibernate_io_page, PAGE_SIZE, HIB_W,
  1110. hib->io_page))) {
  1111. DPRINTF("hib write error %d\n", err);
  1112. return (err);
  1113. }
  1114. *blkctr += PAGE_SIZE / DEV_BSIZE;
  1115. *out_remaining = PAGE_SIZE;
  1116. /* If we didn't deflate the entire RLE byte, finish it now */
  1117. if (hibernate_state->hib_stream.avail_in != 0)
  1118. hibernate_deflate(hib,
  1119. (vaddr_t)hibernate_state->hib_stream.next_in,
  1120. out_remaining);
  1121. }
  1122. return (rle);
  1123. }
  1124. /*
  1125. * Write a compressed version of this machine's memory to disk, at the
  1126. * precalculated swap offset:
  1127. *
  1128. * end of swap - signature block size - chunk table size - memory size
  1129. *
  1130. * The function begins by looping through each phys mem range, cutting each
  1131. * one into MD sized chunks. These chunks are then compressed individually
  1132. * and written out to disk, in phys mem order. Some chunks might compress
  1133. * more than others, and for this reason, each chunk's size is recorded
  1134. * in the chunk table, which is written to disk after the image has
  1135. * properly been compressed and written (in hibernate_write_chunktable).
  1136. *
  1137. * When this function is called, the machine is nearly suspended - most
  1138. * devices are quiesced/suspended, interrupts are off, and cold has
  1139. * been set. This means that there can be no side effects once the
  1140. * write has started, and the write function itself can also have no
  1141. * side effects. This also means no printfs are permitted (since printf
  1142. * has side effects.)
  1143. *
  1144. * Return values :
  1145. *
  1146. * 0 - success
  1147. * EIO - I/O error occurred writing the chunks
  1148. * EINVAL - Failed to write a complete range
  1149. * ENOMEM - Memory allocation failure during preparation of the zlib arena
  1150. */
  1151. int
  1152. hibernate_write_chunks(union hibernate_info *hib)
  1153. {
  1154. paddr_t range_base, range_end, inaddr, temp_inaddr;
  1155. size_t nblocks, out_remaining, used;
  1156. struct hibernate_disk_chunk *chunks;
  1157. vaddr_t hibernate_io_page = hib->piglet_va + PAGE_SIZE;
  1158. daddr_t blkctr = 0;
  1159. int i, rle, err;
  1160. struct hibernate_zlib_state *hibernate_state;
  1161. hibernate_state =
  1162. (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
  1163. hib->chunk_ctr = 0;
  1164. /*
  1165. * Map the utility VAs to the piglet. See the piglet map at the
  1166. * top of this file for piglet layout information.
  1167. */
  1168. hibernate_copy_page = hib->piglet_va + 3 * PAGE_SIZE;
  1169. hibernate_rle_page = hib->piglet_va + 28 * PAGE_SIZE;
  1170. chunks = (struct hibernate_disk_chunk *)(hib->piglet_va +
  1171. HIBERNATE_CHUNK_SIZE);
  1172. /* Calculate the chunk regions */
  1173. for (i = 0; i < hib->nranges; i++) {
  1174. range_base = hib->ranges[i].base;
  1175. range_end = hib->ranges[i].end;
  1176. inaddr = range_base;
  1177. while (inaddr < range_end) {
  1178. chunks[hib->chunk_ctr].base = inaddr;
  1179. if (inaddr + HIBERNATE_CHUNK_SIZE < range_end)
  1180. chunks[hib->chunk_ctr].end = inaddr +
  1181. HIBERNATE_CHUNK_SIZE;
  1182. else
  1183. chunks[hib->chunk_ctr].end = range_end;
  1184. inaddr += HIBERNATE_CHUNK_SIZE;
  1185. hib->chunk_ctr ++;
  1186. }
  1187. }
  1188. uvm_pmr_dirty_everything();
  1189. uvm_pmr_zero_everything();
  1190. /* Compress and write the chunks in the chunktable */
  1191. for (i = 0; i < hib->chunk_ctr; i++) {
  1192. range_base = chunks[i].base;
  1193. range_end = chunks[i].end;
  1194. chunks[i].offset = blkctr + hib->image_offset;
  1195. /* Reset zlib for deflate */
  1196. if (hibernate_zlib_reset(hib, 1) != Z_OK) {
  1197. DPRINTF("hibernate_zlib_reset failed for deflate\n");
  1198. return (ENOMEM);
  1199. }
  1200. inaddr = range_base;
  1201. /*
  1202. * For each range, loop through its phys mem region
  1203. * and write out the chunks (the last chunk might be
  1204. * smaller than the chunk size).
  1205. */
  1206. while (inaddr < range_end) {
  1207. out_remaining = PAGE_SIZE;
  1208. while (out_remaining > 0 && inaddr < range_end) {
  1209. /*
  1210. * Adjust for regions that are not evenly
  1211. * divisible by PAGE_SIZE or overflowed
  1212. * pages from the previous iteration.
  1213. */
  1214. temp_inaddr = (inaddr & PAGE_MASK) +
  1215. hibernate_copy_page;
  1216. /* Deflate from temp_inaddr to IO page */
  1217. if (inaddr != range_end) {
  1218. if (inaddr % PAGE_SIZE == 0) {
  1219. rle = hibernate_write_rle(hib,
  1220. inaddr,
  1221. range_end,
  1222. &blkctr,
  1223. &out_remaining);
  1224. }
  1225. if (rle == 0) {
  1226. pmap_kenter_pa(hibernate_temp_page,
  1227. inaddr & PMAP_PA_MASK,
  1228. PROT_READ);
  1229. bcopy((caddr_t)hibernate_temp_page,
  1230. (caddr_t)hibernate_copy_page,
  1231. PAGE_SIZE);
  1232. inaddr += hibernate_deflate(hib,
  1233. temp_inaddr,
  1234. &out_remaining);
  1235. } else {
  1236. inaddr += rle * PAGE_SIZE;
  1237. if (inaddr > range_end)
  1238. inaddr = range_end;
  1239. }
  1240. }
  1241. if (out_remaining == 0) {
  1242. /* Filled up the page */
  1243. nblocks = PAGE_SIZE / DEV_BSIZE;
  1244. if ((err = hib->io_func(hib->dev,
  1245. blkctr + hib->image_offset,
  1246. (vaddr_t)hibernate_io_page,
  1247. PAGE_SIZE, HIB_W, hib->io_page))) {
  1248. DPRINTF("hib write error %d\n",
  1249. err);
  1250. return (err);
  1251. }
  1252. blkctr += nblocks;
  1253. }
  1254. }
  1255. }
  1256. if (inaddr != range_end) {
  1257. DPRINTF("deflate range ended prematurely\n");
  1258. return (EINVAL);
  1259. }
  1260. /*
  1261. * End of range. Round up to next secsize bytes
  1262. * after finishing compress
  1263. */
  1264. if (out_remaining == 0)
  1265. out_remaining = PAGE_SIZE;
  1266. /* Finish compress */
  1267. hibernate_state->hib_stream.next_in = (unsigned char *)inaddr;
  1268. hibernate_state->hib_stream.avail_in = 0;
  1269. hibernate_state->hib_stream.next_out =
  1270. (unsigned char *)hibernate_io_page +
  1271. (PAGE_SIZE - out_remaining);
  1272. /* We have an extra output page available for finalize */
  1273. hibernate_state->hib_stream.avail_out =
  1274. out_remaining + PAGE_SIZE;
  1275. if ((err = deflate(&hibernate_state->hib_stream, Z_FINISH)) !=
  1276. Z_STREAM_END) {
  1277. DPRINTF("deflate error in output stream: %d\n", err);
  1278. return (err);
  1279. }
  1280. out_remaining = hibernate_state->hib_stream.avail_out;
  1281. used = 2 * PAGE_SIZE - out_remaining;
  1282. nblocks = used / DEV_BSIZE;
  1283. /* Round up to next block if needed */
  1284. if (used % DEV_BSIZE != 0)
  1285. nblocks ++;
  1286. /* Write final block(s) for this chunk */
  1287. if ((err = hib->io_func(hib->dev, blkctr + hib->image_offset,
  1288. (vaddr_t)hibernate_io_page, nblocks*DEV_BSIZE,
  1289. HIB_W, hib->io_page))) {
  1290. DPRINTF("hib final write error %d\n", err);
  1291. return (err);
  1292. }
  1293. blkctr += nblocks;
  1294. chunks[i].compressed_size = (blkctr + hib->image_offset -
  1295. chunks[i].offset) * DEV_BSIZE;
  1296. }
  1297. hib->chunktable_offset = hib->image_offset + blkctr;
  1298. return (0);
  1299. }
  1300. /*
  1301. * Reset the zlib stream state and allocate a new hiballoc area for either
  1302. * inflate or deflate. This function is called once for each hibernate chunk.
  1303. * Calling hiballoc_init multiple times is acceptable since the memory it is
  1304. * provided is unmanaged memory (stolen). We use the memory provided to us
  1305. * by the piglet allocated via the supplied hib.
  1306. */
  1307. int
  1308. hibernate_zlib_reset(union hibernate_info *hib, int deflate)
  1309. {
  1310. vaddr_t hibernate_zlib_start;
  1311. size_t hibernate_zlib_size;
  1312. char *pva = (char *)hib->piglet_va;
  1313. struct hibernate_zlib_state *hibernate_state;
  1314. hibernate_state =
  1315. (struct hibernate_zlib_state *)HIBERNATE_HIBALLOC_PAGE;
  1316. if (!deflate)
  1317. pva = (char *)((paddr_t)pva & (PIGLET_PAGE_MASK));
  1318. /*
  1319. * See piglet layout information at the start of this file for
  1320. * information on the zlib page assignments.
  1321. */
  1322. hibernate_zlib_start = (vaddr_t)(pva + (30 * PAGE_SIZE));
  1323. hibernate_zlib_size = 80 * PAGE_SIZE;
  1324. memset((void *)hibernate_zlib_start, 0, hibernate_zlib_size);
  1325. memset(hibernate_state, 0, PAGE_SIZE);
  1326. /* Set up stream structure */
  1327. hibernate_state->hib_stream.zalloc = (alloc_func)hibernate_zlib_alloc;
  1328. hibernate_state->hib_stream.zfree = (free_func)hibernate_zlib_free;
  1329. /* Initialize the hiballoc arena for zlib allocs/frees */
  1330. hiballoc_init(&hibernate_state->hiballoc_arena,
  1331. (caddr_t)hibernate_zlib_start, hibernate_zlib_size);
  1332. if (deflate) {
  1333. return deflateInit(&hibernate_state->hib_stream,
  1334. Z_BEST_SPEED);
  1335. } else
  1336. return inflateInit(&hibernate_state->hib_stream);
  1337. }
  1338. /*
  1339. * Reads the hibernated memory image from disk, whose location and
  1340. * size are recorded in hib. Begin by reading the persisted
  1341. * chunk table, which records the original chunk placement location
  1342. * and compressed size for each. Next, allocate a pig region of
  1343. * sufficient size to hold the compressed image. Next, read the
  1344. * chunks into the pig area (calling hibernate_read_chunks to do this),
  1345. * and finally, if all of the above succeeds, clear the hibernate signature.
  1346. * The function will then return to hibernate_resume, which will proceed
  1347. * to unpack the pig image to the correct place in memory.
  1348. */
  1349. int
  1350. hibernate_read_image(union hibernate_info *hib)
  1351. {
  1352. size_t compressed_size, disk_size, chunktable_size, pig_sz;
  1353. paddr_t image_start, image_end, pig_start, pig_end;
  1354. struct hibernate_disk_chunk *chunks;
  1355. daddr_t blkctr;
  1356. vaddr_t chunktable = (vaddr_t)NULL;
  1357. paddr_t piglet_chunktable = hib->piglet_pa +
  1358. HIBERNATE_CHUNK_SIZE;
  1359. int i, status;
  1360. status = 0;
  1361. pmap_activate(curproc);
  1362. /* Calculate total chunk table size in disk blocks */
  1363. chunktable_size = HIBERNATE_CHUNK_TABLE_SIZE / DEV_BSIZE;
  1364. blkctr = hib->chunktable_offset;
  1365. chunktable = (vaddr_t)km_alloc(HIBERNATE_CHUNK_TABLE_SIZE, &kv_any,
  1366. &kp_none, &kd_nowait);
  1367. if (!chunktable)
  1368. return (1);
  1369. /* Map chunktable pages */
  1370. for (i = 0; i < HIBERNATE_CHUNK_TABLE_SIZE; i += PAGE_SIZE)
  1371. pmap_kenter_pa(chunktable + i, piglet_chunktable + i,
  1372. PROT_READ | PROT_WRITE);
  1373. pmap_update(pmap_kernel());
  1374. /* Read the chunktable from disk into the piglet chunktable */
  1375. for (i = 0; i < HIBERNATE_CHUNK_TABLE_SIZE;
  1376. i += MAXPHYS, blkctr += MAXPHYS/DEV_BSIZE)
  1377. hibernate_block_io(hib, blkctr, MAXPHYS,
  1378. chunktable + i, 0);
  1379. blkctr = hib->image_offset;
  1380. compressed_size = 0;
  1381. chunks = (struct hibernate_disk_chunk *)chunktable;
  1382. for (i = 0; i < hib->chunk_ctr; i++)
  1383. compressed_size += chunks[i].compressed_size;
  1384. disk_size = compressed_size;
  1385. printf("unhibernating @ block %lld length %lu bytes\n",
  1386. hib->sig_offset - chunktable_size,
  1387. compressed_size);
  1388. /* Allocate the pig area */
  1389. pig_sz = compressed_size + HIBERNATE_CHUNK_SIZE;
  1390. if (uvm_pmr_alloc_pig(&pig_start, pig_sz, hib->piglet_pa) == ENOMEM) {
  1391. status = 1;
  1392. goto unmap;
  1393. }
  1394. pig_end = pig_start + pig_sz;
  1395. /* Calculate image extents. Pig image must end on a chunk boundary. */
  1396. image_end = pig_end & ~(HIBERNATE_CHUNK_SIZE - 1);
  1397. image_start = image_end - disk_size;
  1398. hibernate_read_chunks(hib, image_start, image_end, disk_size,
  1399. chunks);
  1400. /* Prepare the resume time pmap/page table */
  1401. hibernate_populate_resume_pt(hib, image_start, image_end);
  1402. unmap:
  1403. /* Unmap chunktable pages */
  1404. pmap_kremove(chunktable, HIBERNATE_CHUNK_TABLE_SIZE);
  1405. pmap_update(pmap_kernel());
  1406. return (status);
  1407. }
  1408. /*
  1409. * Read the hibernated memory chunks from disk (chunk information at this
  1410. * point is stored in the piglet) into the pig area specified by
  1411. * [pig_start .. pig_end]. Order the chunks so that the final chunk is the
  1412. * only chunk with overlap possibilities.
  1413. */
  1414. int
  1415. hibernate_read_chunks(union hibernate_info *hib, paddr_t pig_start,
  1416. paddr_t pig_end, size_t image_compr_size,
  1417. struct hibernate_disk_chunk *chunks)
  1418. {
  1419. paddr_t img_cur, piglet_base;
  1420. daddr_t blkctr;
  1421. size_t processed, compressed_size, read_size;
  1422. int nchunks, nfchunks, num_io_pages;
  1423. vaddr_t tempva, hibernate_fchunk_area;
  1424. short *fchunks, i, j;
  1425. tempva = (vaddr_t)NULL;
  1426. hibernate_fchunk_area = (vaddr_t)NULL;
  1427. nfchunks = 0;
  1428. piglet_base = hib->piglet_pa;
  1429. global_pig_start = pig_start;
  1430. /*
  1431. * These mappings go into the resuming kernel's page table, and are
  1432. * used only during image read. They dissappear from existence
  1433. * when the suspended kernel is unpacked on top of us.
  1434. */
  1435. tempva = (vaddr_t)km_alloc(MAXPHYS + PAGE_SIZE, &kv_any, &kp_none,
  1436. &kd_nowait);
  1437. if (!tempva)
  1438. return (1);
  1439. hibernate_fchunk_area = (vaddr_t)km_alloc(24 * PAGE_SIZE, &kv_any,
  1440. &kp_none, &kd_nowait);
  1441. if (!hibernate_fchunk_area)
  1442. return (1);
  1443. /* Final output chunk ordering VA */
  1444. fchunks = (short *)hibernate_fchunk_area;
  1445. /* Map the chunk ordering region */
  1446. for(i = 0; i < 24 ; i++)
  1447. pmap_kenter_pa(hibernate_fchunk_area + (i * PAGE_SIZE),
  1448. piglet_base + ((4 + i) * PAGE_SIZE),
  1449. PROT_READ | PROT_WRITE);
  1450. pmap_update(pmap_kernel());
  1451. nchunks = hib->chunk_ctr;
  1452. /* Initially start all chunks as unplaced */
  1453. for (i = 0; i < nchunks; i++)
  1454. chunks[i].flags = 0;
  1455. /*
  1456. * Search the list for chunks that are outside the pig area. These
  1457. * can be placed first in the final output list.
  1458. */
  1459. for (i = 0; i < nchunks; i++) {
  1460. if (chunks[i].end <= pig_start || chunks[i].base >= pig_end) {
  1461. fchunks[nfchunks] = i;
  1462. nfchunks++;
  1463. chunks[i].flags |= HIBERNATE_CHUNK_PLACED;
  1464. }
  1465. }
  1466. /*
  1467. * Walk the ordering, place the chunks in ascending memory order.
  1468. */
  1469. for (i = 0; i < nchunks; i++) {
  1470. if (chunks[i].flags != HIBERNATE_CHUNK_PLACED) {
  1471. fchunks[nfchunks] = i;
  1472. nfchunks++;
  1473. chunks[i].flags = HIBERNATE_CHUNK_PLACED;
  1474. }
  1475. }
  1476. img_cur = pig_start;
  1477. for (i = 0; i < nfchunks; i++) {
  1478. blkctr = chunks[fchunks[i]].offset;
  1479. processed = 0;
  1480. compressed_size = chunks[fchunks[i]].compressed_size;
  1481. while (processed < compressed_size) {
  1482. if (compressed_size - processed >= MAXPHYS)
  1483. read_size = MAXPHYS;
  1484. else
  1485. read_size = compressed_size - processed;
  1486. /*
  1487. * We're reading read_size bytes, offset from the
  1488. * start of a page by img_cur % PAGE_SIZE, so the
  1489. * end will be read_size + (img_cur % PAGE_SIZE)
  1490. * from the start of the first page. Round that
  1491. * up to the next page size.
  1492. */
  1493. num_io_pages = (read_size + (img_cur % PAGE_SIZE)
  1494. + PAGE_SIZE - 1) / PAGE_SIZE;
  1495. KASSERT(num_io_pages <= MAXPHYS/PAGE_SIZE + 1);
  1496. /* Map pages for this read */
  1497. for (j = 0; j < num_io_pages; j ++)
  1498. pmap_kenter_pa(tempva + j * PAGE_SIZE,
  1499. img_cur + j * PAGE_SIZE,
  1500. PROT_READ | PROT_WRITE);
  1501. pmap_update(pmap_kernel());
  1502. hibernate_block_io(hib, blkctr, read_size,
  1503. tempva + (img_cur & PAGE_MASK), 0);
  1504. blkctr += (read_size / DEV_BSIZE);
  1505. pmap_kremove(tempva, num_io_pages * PAGE_SIZE);
  1506. pmap_update(pmap_kernel());
  1507. processed += read_size;
  1508. img_cur += read_size;
  1509. }
  1510. }
  1511. pmap_kremove(hibernate_fchunk_area, 24 * PAGE_SIZE);
  1512. pmap_update(pmap_kernel());
  1513. return (0);
  1514. }
  1515. /*
  1516. * Hibernating a machine comprises the following operations:
  1517. * 1. Calculating this machine's hibernate_info information
  1518. * 2. Allocating a piglet and saving the piglet's physaddr
  1519. * 3. Calculating the memory chunks
  1520. * 4. Writing the compressed chunks to disk
  1521. * 5. Writing the chunk table
  1522. * 6. Writing the signature block (hibernate_info)
  1523. *
  1524. * On most architectures, the function calling hibernate_suspend would
  1525. * then power off the machine using some MD-specific implementation.
  1526. */
  1527. int
  1528. hibernate_suspend(void)
  1529. {
  1530. union hibernate_info hib;
  1531. u_long start, end;
  1532. /*
  1533. * Calculate memory ranges, swap offsets, etc.
  1534. * This also allocates a piglet whose physaddr is stored in
  1535. * hib->piglet_pa and vaddr stored in hib->piglet_va
  1536. */
  1537. if (get_hibernate_info(&hib, 1)) {
  1538. DPRINTF("failed to obtain hibernate info\n");
  1539. return (1);
  1540. }
  1541. /* Find a page-addressed region in swap [start,end] */
  1542. if (uvm_hibswap(hib.dev, &start, &end)) {
  1543. printf("hibernate: cannot find any swap\n");
  1544. return (1);
  1545. }
  1546. if (end - start < 1000) {
  1547. printf("hibernate: insufficient swap (%lu is too small)\n",
  1548. end - start);
  1549. return (1);
  1550. }
  1551. /* Calculate block offsets in swap */
  1552. hib.image_offset = ctod(start);
  1553. DPRINTF("hibernate @ block %lld max-length %lu blocks\n",
  1554. hib.image_offset, ctod(end) - ctod(start));
  1555. pmap_activate(curproc);
  1556. DPRINTF("hibernate: writing chunks\n");
  1557. if (hibernate_write_chunks(&hib)) {
  1558. DPRINTF("hibernate_write_chunks failed\n");
  1559. return (1);
  1560. }
  1561. DPRINTF("hibernate: writing chunktable\n");
  1562. if (hibernate_write_chunktable(&hib)) {
  1563. DPRINTF("hibernate_write_chunktable failed\n");
  1564. return (1);
  1565. }
  1566. DPRINTF("hibernate: writing signature\n");
  1567. if (hibernate_write_signature(&hib)) {
  1568. DPRINTF("hibernate_write_signature failed\n");
  1569. return (1);
  1570. }
  1571. /* Allow the disk to settle */
  1572. delay(500000);
  1573. /*
  1574. * Give the device-specific I/O function a notification that we're
  1575. * done, and that it can clean up or shutdown as needed.
  1576. */
  1577. hib.io_func(hib.dev, 0, (vaddr_t)NULL, 0, HIB_DONE, hib.io_page);
  1578. return (0);
  1579. }
  1580. int
  1581. hibernate_alloc(void)
  1582. {
  1583. KASSERT(global_piglet_va == 0);
  1584. KASSERT(hibernate_temp_page == 0);
  1585. pmap_activate(curproc);
  1586. pmap_kenter_pa(HIBERNATE_HIBALLOC_PAGE, HIBERNATE_HIBALLOC_PAGE,
  1587. PROT_READ | PROT_WRITE);
  1588. /* Allocate a piglet, store its addresses in the supplied globals */
  1589. if (uvm_pmr_alloc_piglet(&global_piglet_va, &global_piglet_pa,
  1590. HIBERNATE_CHUNK_SIZE * 4, HIBERNATE_CHUNK_SIZE))
  1591. return (ENOMEM);
  1592. /*
  1593. * Allocate VA for the temp page.
  1594. *
  1595. * This will become part of the suspended kernel and will
  1596. * be freed in hibernate_free, upon resume (or hibernate
  1597. * failure)
  1598. */
  1599. hibernate_temp_page = (vaddr_t)km_alloc(PAGE_SIZE, &kv_any,
  1600. &kp_none, &kd_nowait);
  1601. if (!hibernate_temp_page) {
  1602. DPRINTF("out of memory allocating hibernate_temp_page\n");
  1603. return (ENOMEM);
  1604. }
  1605. return (0);
  1606. }
  1607. /*
  1608. * Free items allocated by hibernate_alloc()
  1609. */
  1610. void
  1611. hibernate_free(void)
  1612. {
  1613. pmap_activate(curproc);
  1614. if (global_piglet_va)
  1615. uvm_pmr_free_piglet(global_piglet_va,
  1616. 4 * HIBERNATE_CHUNK_SIZE);
  1617. if (hibernate_temp_page) {
  1618. pmap_kremove(hibernate_temp_page, PAGE_SIZE);
  1619. km_free((void *)hibernate_temp_page, PAGE_SIZE,
  1620. &kv_any, &kp_none);
  1621. }
  1622. global_piglet_va = 0;
  1623. hibernate_temp_page = 0;
  1624. pmap_kremove(HIBERNATE_HIBALLOC_PAGE, PAGE_SIZE);
  1625. pmap_update(pmap_kernel());
  1626. }