160727d8bSWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 5df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 6df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 9df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 10df8bae1dSRodney W. Grimes * Science Department. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 20fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * @(#)vm_pager.h 8.4 (Berkeley) 1/12/94 37c3aac50fSPeter Wemm * $FreeBSD$ 38df8bae1dSRodney W. Grimes */ 39df8bae1dSRodney W. Grimes 40df8bae1dSRodney W. Grimes /* 41df8bae1dSRodney W. Grimes * Pager routine interface definition. 42df8bae1dSRodney W. Grimes */ 43df8bae1dSRodney W. Grimes 44df8bae1dSRodney W. Grimes #ifndef _VM_PAGER_ 45df8bae1dSRodney W. Grimes #define _VM_PAGER_ 46df8bae1dSRodney W. Grimes 47e7a58978SBruce Evans #include <sys/queue.h> 48e7a58978SBruce Evans 49e3975643SJake Burkholder TAILQ_HEAD(pagerlst, vm_object); 50df8bae1dSRodney W. Grimes 51d20b2f76SPoul-Henning Kamp typedef void pgo_init_t(void); 523364c323SKonstantin Belousov typedef vm_object_t pgo_alloc_t(void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t, 533364c323SKonstantin Belousov struct ucred *); 54d20b2f76SPoul-Henning Kamp typedef void pgo_dealloc_t(vm_object_t); 55b0cd2017SGleb Smirnoff typedef int pgo_getpages_t(vm_object_t, vm_page_t *, int, int *, int *); 5690effb23SGleb Smirnoff typedef void pgo_getpages_iodone_t(void *, vm_page_t *, int, int); 57b0cd2017SGleb Smirnoff typedef int pgo_getpages_async_t(vm_object_t, vm_page_t *, int, int *, int *, 5890effb23SGleb Smirnoff pgo_getpages_iodone_t, void *); 59d20b2f76SPoul-Henning Kamp typedef void pgo_putpages_t(vm_object_t, vm_page_t *, int, int, int *); 60d20b2f76SPoul-Henning Kamp typedef boolean_t pgo_haspage_t(vm_object_t, vm_pindex_t, int *, int *); 61c42b43a0SKonstantin Belousov typedef int pgo_populate_t(vm_object_t, vm_pindex_t, int, vm_prot_t, 62c42b43a0SKonstantin Belousov vm_pindex_t *, vm_pindex_t *); 63d20b2f76SPoul-Henning Kamp typedef void pgo_pageunswapped_t(vm_page_t); 64d20b2f76SPoul-Henning Kamp 65df8bae1dSRodney W. Grimes struct pagerops { 66d20b2f76SPoul-Henning Kamp pgo_init_t *pgo_init; /* Initialize pager. */ 67d20b2f76SPoul-Henning Kamp pgo_alloc_t *pgo_alloc; /* Allocate pager. */ 68d20b2f76SPoul-Henning Kamp pgo_dealloc_t *pgo_dealloc; /* Disassociate. */ 69d20b2f76SPoul-Henning Kamp pgo_getpages_t *pgo_getpages; /* Get (read) page. */ 7090effb23SGleb Smirnoff pgo_getpages_async_t *pgo_getpages_async; /* Get page asyncly. */ 71d20b2f76SPoul-Henning Kamp pgo_putpages_t *pgo_putpages; /* Put (write) page. */ 72e1a44545SGleb Smirnoff pgo_haspage_t *pgo_haspage; /* Query page. */ 73c42b43a0SKonstantin Belousov pgo_populate_t *pgo_populate; /* Bulk spec pagein. */ 74d20b2f76SPoul-Henning Kamp pgo_pageunswapped_t *pgo_pageunswapped; 75df8bae1dSRodney W. Grimes }; 76df8bae1dSRodney W. Grimes 77745f3305SPoul-Henning Kamp extern struct pagerops defaultpagerops; 78745f3305SPoul-Henning Kamp extern struct pagerops swappagerops; 79745f3305SPoul-Henning Kamp extern struct pagerops vnodepagerops; 80745f3305SPoul-Henning Kamp extern struct pagerops devicepagerops; 81745f3305SPoul-Henning Kamp extern struct pagerops physpagerops; 8201381811SJohn Baldwin extern struct pagerops sgpagerops; 83b7ac5a85SKonstantin Belousov extern struct pagerops mgtdevicepagerops; 84745f3305SPoul-Henning Kamp 85df8bae1dSRodney W. Grimes /* 86df8bae1dSRodney W. Grimes * get/put return values 87df8bae1dSRodney W. Grimes * OK operation was successful 88df8bae1dSRodney W. Grimes * BAD specified data was out of the accepted range 89df8bae1dSRodney W. Grimes * FAIL specified data was in range, but doesn't exist 90df8bae1dSRodney W. Grimes * PEND operations was initiated but not completed 91df8bae1dSRodney W. Grimes * ERROR error while accessing data that is in range and exists 92df8bae1dSRodney W. Grimes * AGAIN temporary resource shortage prevented operation from happening 93df8bae1dSRodney W. Grimes */ 94df8bae1dSRodney W. Grimes #define VM_PAGER_OK 0 95df8bae1dSRodney W. Grimes #define VM_PAGER_BAD 1 96df8bae1dSRodney W. Grimes #define VM_PAGER_FAIL 2 97df8bae1dSRodney W. Grimes #define VM_PAGER_PEND 3 98df8bae1dSRodney W. Grimes #define VM_PAGER_ERROR 4 99df8bae1dSRodney W. Grimes #define VM_PAGER_AGAIN 5 100df8bae1dSRodney W. Grimes 10143b7990eSMatthew Dillon #define VM_PAGER_PUT_SYNC 0x0001 10243b7990eSMatthew Dillon #define VM_PAGER_PUT_INVAL 0x0002 10399e6e193SMark Johnston #define VM_PAGER_PUT_NOREUSE 0x0004 10443b7990eSMatthew Dillon #define VM_PAGER_CLUSTER_OK 0x0008 1058f9110f6SJohn Dyson 106c4473420SPeter Wemm #ifdef _KERNEL 107a1c995b6SPoul-Henning Kamp 1081c7c3c6aSMatthew Dillon extern struct pagerops *pagertab[]; 1095f518366SJeff Roberson extern struct mtx_padalign pbuf_mtx; 11028f8db14SBruce Evans 1113364c323SKonstantin Belousov vm_object_t vm_pager_allocate(objtype_t, void *, vm_ooffset_t, vm_prot_t, 1123364c323SKonstantin Belousov vm_ooffset_t, struct ucred *); 11311caded3SAlfred Perlstein void vm_pager_bufferinit(void); 11411caded3SAlfred Perlstein void vm_pager_deallocate(vm_object_t); 115b0cd2017SGleb Smirnoff int vm_pager_get_pages(vm_object_t, vm_page_t *, int, int *, int *); 116b0cd2017SGleb Smirnoff int vm_pager_get_pages_async(vm_object_t, vm_page_t *, int, int *, int *, 117093ebe1dSGleb Smirnoff pgo_getpages_iodone_t, void *); 11811caded3SAlfred Perlstein void vm_pager_init(void); 11911caded3SAlfred Perlstein vm_object_t vm_pager_object_lookup(struct pagerlst *, void *); 1201c7c3c6aSMatthew Dillon 121e4542174SMatthew Dillon static __inline void 1221c7c3c6aSMatthew Dillon vm_pager_put_pages( 1231c7c3c6aSMatthew Dillon vm_object_t object, 1241c7c3c6aSMatthew Dillon vm_page_t *m, 1251c7c3c6aSMatthew Dillon int count, 1261c7c3c6aSMatthew Dillon int flags, 1271c7c3c6aSMatthew Dillon int *rtvals 1281c7c3c6aSMatthew Dillon ) { 1292e3b314dSAlan Cox 13089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 131e4542174SMatthew Dillon (*pagertab[object->type]->pgo_putpages) 132e4542174SMatthew Dillon (object, m, count, flags, rtvals); 1331c7c3c6aSMatthew Dillon } 1341c7c3c6aSMatthew Dillon 13525db2c54SMatthew Dillon /* 13625db2c54SMatthew Dillon * vm_pager_haspage 13725db2c54SMatthew Dillon * 13825db2c54SMatthew Dillon * Check to see if an object's pager has the requested page. The 13925db2c54SMatthew Dillon * object's pager will also set before and after to give the caller 14025db2c54SMatthew Dillon * some idea of the number of pages before and after the requested 14125db2c54SMatthew Dillon * page can be I/O'd efficiently. 14225db2c54SMatthew Dillon * 143cf51adc0SAlan Cox * The object must be locked. 14425db2c54SMatthew Dillon */ 1451c7c3c6aSMatthew Dillon static __inline boolean_t 1461c7c3c6aSMatthew Dillon vm_pager_has_page( 1471c7c3c6aSMatthew Dillon vm_object_t object, 1481c7c3c6aSMatthew Dillon vm_pindex_t offset, 1491c7c3c6aSMatthew Dillon int *before, 1501c7c3c6aSMatthew Dillon int *after 1511c7c3c6aSMatthew Dillon ) { 15223955314SAlfred Perlstein boolean_t ret; 15323955314SAlfred Perlstein 15489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 15523955314SAlfred Perlstein ret = (*pagertab[object->type]->pgo_haspage) 15623955314SAlfred Perlstein (object, offset, before, after); 15723955314SAlfred Perlstein return (ret); 1581c7c3c6aSMatthew Dillon } 1591c7c3c6aSMatthew Dillon 160c42b43a0SKonstantin Belousov static __inline int 161c42b43a0SKonstantin Belousov vm_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type, 162c42b43a0SKonstantin Belousov vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last) 163c42b43a0SKonstantin Belousov { 164c42b43a0SKonstantin Belousov 165c42b43a0SKonstantin Belousov MPASS((object->flags & OBJ_POPULATE) != 0); 166c42b43a0SKonstantin Belousov MPASS(pidx < object->size); 167c42b43a0SKonstantin Belousov MPASS(object->paging_in_progress > 0); 168c42b43a0SKonstantin Belousov return ((*pagertab[object->type]->pgo_populate)(object, pidx, 169c42b43a0SKonstantin Belousov fault_type, max_prot, first, last)); 170c42b43a0SKonstantin Belousov } 171c42b43a0SKonstantin Belousov 172c42b43a0SKonstantin Belousov 1731c7c3c6aSMatthew Dillon /* 1741c7c3c6aSMatthew Dillon * vm_pager_page_unswapped 1751c7c3c6aSMatthew Dillon * 176cf51adc0SAlan Cox * Destroy swap associated with the page. 1771c7c3c6aSMatthew Dillon * 178cf51adc0SAlan Cox * The object containing the page must be locked. 1791c7c3c6aSMatthew Dillon * This function may not block. 180f976cfd9SPoul-Henning Kamp * 181f976cfd9SPoul-Henning Kamp * XXX: A much better name would be "vm_pager_page_dirtied()" 182f976cfd9SPoul-Henning Kamp * XXX: It is not obvious if this could be profitably used by any 183f976cfd9SPoul-Henning Kamp * XXX: pagers besides the swap_pager or if it should even be a 184f976cfd9SPoul-Henning Kamp * XXX: generic pager_op in the first place. 1851c7c3c6aSMatthew Dillon */ 1861c7c3c6aSMatthew Dillon static __inline void 1871c7c3c6aSMatthew Dillon vm_pager_page_unswapped(vm_page_t m) 1881c7c3c6aSMatthew Dillon { 1892e3b314dSAlan Cox 190385b4265SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(m->object); 1911c7c3c6aSMatthew Dillon if (pagertab[m->object->type]->pgo_pageunswapped) 1921c7c3c6aSMatthew Dillon (*pagertab[m->object->type]->pgo_pageunswapped)(m); 1931c7c3c6aSMatthew Dillon } 1941c7c3c6aSMatthew Dillon 195286790a7SKonstantin Belousov struct cdev_pager_ops { 196286790a7SKonstantin Belousov int (*cdev_pg_fault)(vm_object_t vm_obj, vm_ooffset_t offset, 197286790a7SKonstantin Belousov int prot, vm_page_t *mres); 198c42b43a0SKonstantin Belousov int (*cdev_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx, 199c42b43a0SKonstantin Belousov int fault_type, vm_prot_t max_prot, vm_pindex_t *first, 200c42b43a0SKonstantin Belousov vm_pindex_t *last); 201286790a7SKonstantin Belousov int (*cdev_pg_ctor)(void *handle, vm_ooffset_t size, vm_prot_t prot, 202286790a7SKonstantin Belousov vm_ooffset_t foff, struct ucred *cred, u_short *color); 203286790a7SKonstantin Belousov void (*cdev_pg_dtor)(void *handle); 204286790a7SKonstantin Belousov }; 205286790a7SKonstantin Belousov 206286790a7SKonstantin Belousov vm_object_t cdev_pager_allocate(void *handle, enum obj_type tp, 207286790a7SKonstantin Belousov struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot, 208286790a7SKonstantin Belousov vm_ooffset_t foff, struct ucred *cred); 209286790a7SKonstantin Belousov vm_object_t cdev_pager_lookup(void *handle); 210286790a7SKonstantin Belousov void cdev_pager_free_page(vm_object_t object, vm_page_t m); 211286790a7SKonstantin Belousov 212a1287949SEivind Eklund #endif /* _KERNEL */ 213df8bae1dSRodney W. Grimes #endif /* _VM_PAGER_ */ 214