| 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-2-Clause |
| 3 | * |
| 4 | * Copyright (c) 2020, Scott Phillips <scottph@freebsd.org> |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice unmodified, this list of conditions, and the following |
| 11 | * disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef	_SYS_DUMPSET_H_ |
| 29 | #define	_SYS_DUMPSET_H_ |
| 30 | |
| 31 | #include <sys/_bitset.h> |
| 32 | #include <sys/bitset.h> |
| 33 | |
| 34 | extern struct bitset *vm_page_dump; |
| 35 | extern long vm_page_dump_pages; |
| 36 | extern vm_paddr_t dump_avail[PHYS_AVAIL_COUNT]; |
| 37 | |
| 38 | /* For the common case: add/remove a page from the minidump bitset. */ |
| 39 | #define	dump_add_page(pa)	vm_page_dump_add(vm_page_dump, pa) |
| 40 | #define	dump_drop_page(pa)	vm_page_dump_drop(vm_page_dump, pa) |
| 41 | |
| 42 | static inline void |
| 43 | vm_page_dump_add(struct bitset *bitset, vm_paddr_t pa) |
| 44 | { |
| 45 | 	vm_pindex_t adj; |
| 46 | 	int i; |
| 47 | |
| 48 | 	adj = 0; |
| 49 | 	for (i = 0; dump_avail[i + 1] != 0; i += 2) { |
| 50 | 		if (pa >= dump_avail[i] && pa < dump_avail[i + 1]) { |
| 51 | 			BIT_SET_ATOMIC(vm_page_dump_pages, |
| 52 | 			 (pa >> PAGE_SHIFT) - (dump_avail[i] >> PAGE_SHIFT) + |
| 53 | 			 adj, bitset); |
| 54 | 			return; |
| 55 | 		} |
| 56 | 		adj += howmany(dump_avail[i + 1], PAGE_SIZE) - |
| 57 | 		 dump_avail[i] / PAGE_SIZE; |
| 58 | 	} |
| 59 | } |
| 60 | |
| 61 | static inline void |
| 62 | vm_page_dump_drop(struct bitset *bitset, vm_paddr_t pa) |
| 63 | { |
| 64 | 	vm_pindex_t adj; |
| 65 | 	int i; |
| 66 | |
| 67 | 	adj = 0; |
| 68 | 	for (i = 0; dump_avail[i + 1] != 0; i += 2) { |
| 69 | 		if (pa >= dump_avail[i] && pa < dump_avail[i + 1]) { |
| 70 | 			BIT_CLR_ATOMIC(vm_page_dump_pages, |
| 71 | 			 (pa >> PAGE_SHIFT) - (dump_avail[i] >> PAGE_SHIFT) + |
| 72 | 			 adj, bitset); |
| 73 | 			return; |
| 74 | 		} |
| 75 | 		adj += howmany(dump_avail[i + 1], PAGE_SIZE) - |
| 76 | 		 dump_avail[i] / PAGE_SIZE; |
| 77 | 	} |
| 78 | } |
| 79 | |
| 80 | static inline vm_paddr_t |
| 81 | vm_page_dump_index_to_pa(int bit) |
| 82 | { |
| 83 | 	int i, tot; |
| 84 | |
| 85 | 	for (i = 0; dump_avail[i + 1] != 0; i += 2) { |
| 86 | 		tot = howmany(dump_avail[i + 1], PAGE_SIZE) - |
| 87 | 		 dump_avail[i] / PAGE_SIZE; |
| 88 | 		if (bit < tot) |
| 89 | 			return ((vm_paddr_t)bit * PAGE_SIZE + |
| 90 | 			 (dump_avail[i] & ~PAGE_MASK)); |
| 91 | 		bit -= tot; |
| 92 | 	} |
| 93 | 	return (0); |
| 94 | } |
| 95 | |
| 96 | #define VM_PAGE_DUMP_FOREACH(bitset, pa)				\ |
| 97 | 	for (vm_pindex_t __b = BIT_FFS(vm_page_dump_pages, bitset);	\ |
| 98 | 	 (pa) = vm_page_dump_index_to_pa(__b - 1), __b != 0;		\ |
| 99 | 	 __b = BIT_FFS_AT(vm_page_dump_pages, bitset, __b)) |
| 100 | |
| 101 | #endif	/* _SYS_DUMPSET_H_ */ |