1151f4e2bSMauro Carvalho Chehab===================================================== 2151f4e2bSMauro Carvalho ChehabDocumentation for userland software suspend interface 3151f4e2bSMauro Carvalho Chehab===================================================== 4151f4e2bSMauro Carvalho Chehab 5151f4e2bSMauro Carvalho Chehab (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> 6151f4e2bSMauro Carvalho Chehab 7151f4e2bSMauro Carvalho ChehabFirst, the warnings at the beginning of swsusp.txt still apply. 8151f4e2bSMauro Carvalho Chehab 9151f4e2bSMauro Carvalho ChehabSecond, you should read the FAQ in swsusp.txt _now_ if you have not 10151f4e2bSMauro Carvalho Chehabdone it already. 11151f4e2bSMauro Carvalho Chehab 12151f4e2bSMauro Carvalho ChehabNow, to use the userland interface for software suspend you need special 13151f4e2bSMauro Carvalho Chehabutilities that will read/write the system memory snapshot from/to the 14151f4e2bSMauro Carvalho Chehabkernel. Such utilities are available, for example, from 15151f4e2bSMauro Carvalho Chehab<http://suspend.sourceforge.net>. You may want to have a look at them if you 16151f4e2bSMauro Carvalho Chehabare going to develop your own suspend/resume utilities. 17151f4e2bSMauro Carvalho Chehab 18151f4e2bSMauro Carvalho ChehabThe interface consists of a character device providing the open(), 19151f4e2bSMauro Carvalho Chehabrelease(), read(), and write() operations as well as several ioctl() 20151f4e2bSMauro Carvalho Chehabcommands defined in include/linux/suspend_ioctls.h . The major and minor 21151f4e2bSMauro Carvalho Chehabnumbers of the device are, respectively, 10 and 231, and they can 22151f4e2bSMauro Carvalho Chehabbe read from /sys/class/misc/snapshot/dev. 23151f4e2bSMauro Carvalho Chehab 24151f4e2bSMauro Carvalho ChehabThe device can be open either for reading or for writing. If open for 25151f4e2bSMauro Carvalho Chehabreading, it is considered to be in the suspend mode. Otherwise it is 26151f4e2bSMauro Carvalho Chehabassumed to be in the resume mode. The device cannot be open for simultaneous 27151f4e2bSMauro Carvalho Chehabreading and writing. It is also impossible to have the device open more than 28151f4e2bSMauro Carvalho Chehabonce at a time. 29151f4e2bSMauro Carvalho Chehab 30151f4e2bSMauro Carvalho ChehabEven opening the device has side effects. Data structures are 31151f4e2bSMauro Carvalho Chehaballocated, and PM_HIBERNATION_PREPARE / PM_RESTORE_PREPARE chains are 32151f4e2bSMauro Carvalho Chehabcalled. 33151f4e2bSMauro Carvalho Chehab 34151f4e2bSMauro Carvalho ChehabThe ioctl() commands recognized by the device are: 35151f4e2bSMauro Carvalho Chehab 36151f4e2bSMauro Carvalho ChehabSNAPSHOT_FREEZE 37151f4e2bSMauro Carvalho Chehab freeze user space processes (the current process is 38151f4e2bSMauro Carvalho Chehab not frozen); this is required for SNAPSHOT_CREATE_IMAGE 39151f4e2bSMauro Carvalho Chehab and SNAPSHOT_ATOMIC_RESTORE to succeed 40151f4e2bSMauro Carvalho Chehab 41151f4e2bSMauro Carvalho ChehabSNAPSHOT_UNFREEZE 42151f4e2bSMauro Carvalho Chehab thaw user space processes frozen by SNAPSHOT_FREEZE 43151f4e2bSMauro Carvalho Chehab 44151f4e2bSMauro Carvalho ChehabSNAPSHOT_CREATE_IMAGE 45151f4e2bSMauro Carvalho Chehab create a snapshot of the system memory; the 46151f4e2bSMauro Carvalho Chehab last argument of ioctl() should be a pointer to an int variable, 47151f4e2bSMauro Carvalho Chehab the value of which will indicate whether the call returned after 48151f4e2bSMauro Carvalho Chehab creating the snapshot (1) or after restoring the system memory state 49151f4e2bSMauro Carvalho Chehab from it (0) (after resume the system finds itself finishing the 50151f4e2bSMauro Carvalho Chehab SNAPSHOT_CREATE_IMAGE ioctl() again); after the snapshot 51151f4e2bSMauro Carvalho Chehab has been created the read() operation can be used to transfer 52151f4e2bSMauro Carvalho Chehab it out of the kernel 53151f4e2bSMauro Carvalho Chehab 54151f4e2bSMauro Carvalho ChehabSNAPSHOT_ATOMIC_RESTORE 55151f4e2bSMauro Carvalho Chehab restore the system memory state from the 56151f4e2bSMauro Carvalho Chehab uploaded snapshot image; before calling it you should transfer 57151f4e2bSMauro Carvalho Chehab the system memory snapshot back to the kernel using the write() 58151f4e2bSMauro Carvalho Chehab operation; this call will not succeed if the snapshot 59151f4e2bSMauro Carvalho Chehab image is not available to the kernel 60151f4e2bSMauro Carvalho Chehab 61151f4e2bSMauro Carvalho ChehabSNAPSHOT_FREE 62151f4e2bSMauro Carvalho Chehab free memory allocated for the snapshot image 63151f4e2bSMauro Carvalho Chehab 64151f4e2bSMauro Carvalho ChehabSNAPSHOT_PREF_IMAGE_SIZE 65151f4e2bSMauro Carvalho Chehab set the preferred maximum size of the image 66151f4e2bSMauro Carvalho Chehab (the kernel will do its best to ensure the image size will not exceed 67151f4e2bSMauro Carvalho Chehab this number, but if it turns out to be impossible, the kernel will 68151f4e2bSMauro Carvalho Chehab create the smallest image possible) 69151f4e2bSMauro Carvalho Chehab 70151f4e2bSMauro Carvalho ChehabSNAPSHOT_GET_IMAGE_SIZE 71151f4e2bSMauro Carvalho Chehab return the actual size of the hibernation image 72*51995ff5SEric Biggers (the last argument should be a pointer to a loff_t variable that 73*51995ff5SEric Biggers will contain the result if the call is successful) 74151f4e2bSMauro Carvalho Chehab 75151f4e2bSMauro Carvalho ChehabSNAPSHOT_AVAIL_SWAP_SIZE 76*51995ff5SEric Biggers return the amount of available swap in bytes 77*51995ff5SEric Biggers (the last argument should be a pointer to a loff_t variable that 78*51995ff5SEric Biggers will contain the result if the call is successful) 79151f4e2bSMauro Carvalho Chehab 80151f4e2bSMauro Carvalho ChehabSNAPSHOT_ALLOC_SWAP_PAGE 81151f4e2bSMauro Carvalho Chehab allocate a swap page from the resume partition 82151f4e2bSMauro Carvalho Chehab (the last argument should be a pointer to a loff_t variable that 83151f4e2bSMauro Carvalho Chehab will contain the swap page offset if the call is successful) 84151f4e2bSMauro Carvalho Chehab 85151f4e2bSMauro Carvalho ChehabSNAPSHOT_FREE_SWAP_PAGES 86151f4e2bSMauro Carvalho Chehab free all swap pages allocated by 87151f4e2bSMauro Carvalho Chehab SNAPSHOT_ALLOC_SWAP_PAGE 88151f4e2bSMauro Carvalho Chehab 89151f4e2bSMauro Carvalho ChehabSNAPSHOT_SET_SWAP_AREA 90151f4e2bSMauro Carvalho Chehab set the resume partition and the offset (in <PAGE_SIZE> 91151f4e2bSMauro Carvalho Chehab units) from the beginning of the partition at which the swap header is 92151f4e2bSMauro Carvalho Chehab located (the last ioctl() argument should point to a struct 93151f4e2bSMauro Carvalho Chehab resume_swap_area, as defined in kernel/power/suspend_ioctls.h, 94151f4e2bSMauro Carvalho Chehab containing the resume device specification and the offset); for swap 95151f4e2bSMauro Carvalho Chehab partitions the offset is always 0, but it is different from zero for 96151f4e2bSMauro Carvalho Chehab swap files (see Documentation/power/swsusp-and-swap-files.rst for 97151f4e2bSMauro Carvalho Chehab details). 98151f4e2bSMauro Carvalho Chehab 99151f4e2bSMauro Carvalho ChehabSNAPSHOT_PLATFORM_SUPPORT 100151f4e2bSMauro Carvalho Chehab enable/disable the hibernation platform support, 101151f4e2bSMauro Carvalho Chehab depending on the argument value (enable, if the argument is nonzero) 102151f4e2bSMauro Carvalho Chehab 103151f4e2bSMauro Carvalho ChehabSNAPSHOT_POWER_OFF 104151f4e2bSMauro Carvalho Chehab make the kernel transition the system to the hibernation 105151f4e2bSMauro Carvalho Chehab state (eg. ACPI S4) using the platform (eg. ACPI) driver 106151f4e2bSMauro Carvalho Chehab 107151f4e2bSMauro Carvalho ChehabSNAPSHOT_S2RAM 108151f4e2bSMauro Carvalho Chehab suspend to RAM; using this call causes the kernel to 109151f4e2bSMauro Carvalho Chehab immediately enter the suspend-to-RAM state, so this call must always 110151f4e2bSMauro Carvalho Chehab be preceded by the SNAPSHOT_FREEZE call and it is also necessary 111151f4e2bSMauro Carvalho Chehab to use the SNAPSHOT_UNFREEZE call after the system wakes up. This call 112151f4e2bSMauro Carvalho Chehab is needed to implement the suspend-to-both mechanism in which the 113151f4e2bSMauro Carvalho Chehab suspend image is first created, as though the system had been suspended 114151f4e2bSMauro Carvalho Chehab to disk, and then the system is suspended to RAM (this makes it possible 115151f4e2bSMauro Carvalho Chehab to resume the system from RAM if there's enough battery power or restore 116151f4e2bSMauro Carvalho Chehab its state on the basis of the saved suspend image otherwise) 117151f4e2bSMauro Carvalho Chehab 118151f4e2bSMauro Carvalho ChehabThe device's read() operation can be used to transfer the snapshot image from 119151f4e2bSMauro Carvalho Chehabthe kernel. It has the following limitations: 120151f4e2bSMauro Carvalho Chehab 121151f4e2bSMauro Carvalho Chehab- you cannot read() more than one virtual memory page at a time 122151f4e2bSMauro Carvalho Chehab- read()s across page boundaries are impossible (ie. if you read() 1/2 of 123151f4e2bSMauro Carvalho Chehab a page in the previous call, you will only be able to read() 124151f4e2bSMauro Carvalho Chehab **at most** 1/2 of the page in the next call) 125151f4e2bSMauro Carvalho Chehab 126151f4e2bSMauro Carvalho ChehabThe device's write() operation is used for uploading the system memory snapshot 127151f4e2bSMauro Carvalho Chehabinto the kernel. It has the same limitations as the read() operation. 128151f4e2bSMauro Carvalho Chehab 129151f4e2bSMauro Carvalho ChehabThe release() operation frees all memory allocated for the snapshot image 130151f4e2bSMauro Carvalho Chehaband all swap pages allocated with SNAPSHOT_ALLOC_SWAP_PAGE (if any). 131151f4e2bSMauro Carvalho ChehabThus it is not necessary to use either SNAPSHOT_FREE or 132151f4e2bSMauro Carvalho ChehabSNAPSHOT_FREE_SWAP_PAGES before closing the device (in fact it will also 133151f4e2bSMauro Carvalho Chehabunfreeze user space processes frozen by SNAPSHOT_UNFREEZE if they are 134151f4e2bSMauro Carvalho Chehabstill frozen when the device is being closed). 135151f4e2bSMauro Carvalho Chehab 136151f4e2bSMauro Carvalho ChehabCurrently it is assumed that the userland utilities reading/writing the 137151f4e2bSMauro Carvalho Chehabsnapshot image from/to the kernel will use a swap partition, called the resume 138151f4e2bSMauro Carvalho Chehabpartition, or a swap file as storage space (if a swap file is used, the resume 139151f4e2bSMauro Carvalho Chehabpartition is the partition that holds this file). However, this is not really 140151f4e2bSMauro Carvalho Chehabrequired, as they can use, for example, a special (blank) suspend partition or 141151f4e2bSMauro Carvalho Chehaba file on a partition that is unmounted before SNAPSHOT_CREATE_IMAGE and 142151f4e2bSMauro Carvalho Chehabmounted afterwards. 143151f4e2bSMauro Carvalho Chehab 144151f4e2bSMauro Carvalho ChehabThese utilities MUST NOT make any assumptions regarding the ordering of 145151f4e2bSMauro Carvalho Chehabdata within the snapshot image. The contents of the image are entirely owned 146151f4e2bSMauro Carvalho Chehabby the kernel and its structure may be changed in future kernel releases. 147151f4e2bSMauro Carvalho Chehab 148151f4e2bSMauro Carvalho ChehabThe snapshot image MUST be written to the kernel unaltered (ie. all of the image 149151f4e2bSMauro Carvalho Chehabdata, metadata and header MUST be written in _exactly_ the same amount, form 150151f4e2bSMauro Carvalho Chehaband order in which they have been read). Otherwise, the behavior of the 151151f4e2bSMauro Carvalho Chehabresumed system may be totally unpredictable. 152151f4e2bSMauro Carvalho Chehab 153151f4e2bSMauro Carvalho ChehabWhile executing SNAPSHOT_ATOMIC_RESTORE the kernel checks if the 154151f4e2bSMauro Carvalho Chehabstructure of the snapshot image is consistent with the information stored 155151f4e2bSMauro Carvalho Chehabin the image header. If any inconsistencies are detected, 156151f4e2bSMauro Carvalho ChehabSNAPSHOT_ATOMIC_RESTORE will not succeed. Still, this is not a fool-proof 157151f4e2bSMauro Carvalho Chehabmechanism and the userland utilities using the interface SHOULD use additional 158151f4e2bSMauro Carvalho Chehabmeans, such as checksums, to ensure the integrity of the snapshot image. 159151f4e2bSMauro Carvalho Chehab 160151f4e2bSMauro Carvalho ChehabThe suspending and resuming utilities MUST lock themselves in memory, 161151f4e2bSMauro Carvalho Chehabpreferably using mlockall(), before calling SNAPSHOT_FREEZE. 162151f4e2bSMauro Carvalho Chehab 163151f4e2bSMauro Carvalho ChehabThe suspending utility MUST check the value stored by SNAPSHOT_CREATE_IMAGE 164151f4e2bSMauro Carvalho Chehabin the memory location pointed to by the last argument of ioctl() and proceed 165151f4e2bSMauro Carvalho Chehabin accordance with it: 166151f4e2bSMauro Carvalho Chehab 167151f4e2bSMauro Carvalho Chehab1. If the value is 1 (ie. the system memory snapshot has just been 168151f4e2bSMauro Carvalho Chehab created and the system is ready for saving it): 169151f4e2bSMauro Carvalho Chehab 170151f4e2bSMauro Carvalho Chehab (a) The suspending utility MUST NOT close the snapshot device 171151f4e2bSMauro Carvalho Chehab _unless_ the whole suspend procedure is to be cancelled, in 172151f4e2bSMauro Carvalho Chehab which case, if the snapshot image has already been saved, the 173151f4e2bSMauro Carvalho Chehab suspending utility SHOULD destroy it, preferably by zapping 174151f4e2bSMauro Carvalho Chehab its header. If the suspend is not to be cancelled, the 175151f4e2bSMauro Carvalho Chehab system MUST be powered off or rebooted after the snapshot 176151f4e2bSMauro Carvalho Chehab image has been saved. 177151f4e2bSMauro Carvalho Chehab (b) The suspending utility SHOULD NOT attempt to perform any 178151f4e2bSMauro Carvalho Chehab file system operations (including reads) on the file systems 179151f4e2bSMauro Carvalho Chehab that were mounted before SNAPSHOT_CREATE_IMAGE has been 180151f4e2bSMauro Carvalho Chehab called. However, it MAY mount a file system that was not 181151f4e2bSMauro Carvalho Chehab mounted at that time and perform some operations on it (eg. 182151f4e2bSMauro Carvalho Chehab use it for saving the image). 183151f4e2bSMauro Carvalho Chehab 184151f4e2bSMauro Carvalho Chehab2. If the value is 0 (ie. the system state has just been restored from 185151f4e2bSMauro Carvalho Chehab the snapshot image), the suspending utility MUST close the snapshot 186151f4e2bSMauro Carvalho Chehab device. Afterwards it will be treated as a regular userland process, 187151f4e2bSMauro Carvalho Chehab so it need not exit. 188151f4e2bSMauro Carvalho Chehab 189151f4e2bSMauro Carvalho ChehabThe resuming utility SHOULD NOT attempt to mount any file systems that could 190151f4e2bSMauro Carvalho Chehabbe mounted before suspend and SHOULD NOT attempt to perform any operations 191151f4e2bSMauro Carvalho Chehabinvolving such file systems. 192151f4e2bSMauro Carvalho Chehab 193151f4e2bSMauro Carvalho ChehabFor details, please refer to the source code. 194