1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 * $FreeBSD$
28 */
29
30#ifndef	_SYS_DUMPSET_H_
31#define	_SYS_DUMPSET_H_
32
33#include <sys/_bitset.h>
34#include <sys/bitset.h>
35
36extern struct bitset *vm_page_dump;
37extern long vm_page_dump_pages;
38extern vm_paddr_t dump_avail[PHYS_AVAIL_COUNT];
39
40static inline void
41dump_add_page(vm_paddr_t pa)
42{
43	vm_pindex_t adj;
44	int i;
45
46	adj = 0;
47	for (i = 0; dump_avail[i + 1] != 0; i += 2) {
48		if (pa >= dump_avail[i] && pa < dump_avail[i + 1]) {
49			BIT_SET_ATOMIC(vm_page_dump_pages,
50			    (pa >> PAGE_SHIFT) - (dump_avail[i] >> PAGE_SHIFT) +
51			    adj, vm_page_dump);
52			return;
53		}
54		adj += howmany(dump_avail[i + 1], PAGE_SIZE) -
55		    dump_avail[i] / PAGE_SIZE;
56	}
57}
58
59static inline void
60dump_drop_page(vm_paddr_t pa)
61{
62	vm_pindex_t adj;
63	int i;
64
65	adj = 0;
66	for (i = 0; dump_avail[i + 1] != 0; i += 2) {
67		if (pa >= dump_avail[i] && pa < dump_avail[i + 1]) {
68			BIT_CLR_ATOMIC(vm_page_dump_pages,
69			    (pa >> PAGE_SHIFT) - (dump_avail[i] >> PAGE_SHIFT) +
70			    adj, vm_page_dump);
71			return;
72		}
73		adj += howmany(dump_avail[i + 1], PAGE_SIZE) -
74		    dump_avail[i] / PAGE_SIZE;
75	}
76}
77
78static inline vm_paddr_t
79vm_page_dump_index_to_pa(int bit)
80{
81	int i, tot;
82
83	for (i = 0; dump_avail[i + 1] != 0; i += 2) {
84		tot = howmany(dump_avail[i + 1], PAGE_SIZE) -
85		    dump_avail[i] / PAGE_SIZE;
86		if (bit < tot)
87			return ((vm_paddr_t)bit * PAGE_SIZE +
88			    (dump_avail[i] & ~PAGE_MASK));
89		bit -= tot;
90	}
91	return ((vm_paddr_t)NULL);
92}
93
94#define VM_PAGE_DUMP_FOREACH(pa)						\
95	for (vm_pindex_t __b = BIT_FFS(vm_page_dump_pages, vm_page_dump);	\
96	    (pa) = vm_page_dump_index_to_pa(__b - 1), __b != 0;			\
97	    __b = BIT_FFS_AT(vm_page_dump_pages, vm_page_dump, __b))
98
99#endif	/* _SYS_DUMPSET_H_ */
100