Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Bitmap.h>
28#include <AK/NonnullRefPtr.h>
29#include <AK/RefPtr.h>
30#include <AK/Vector.h>
31#include <Kernel/Assertions.h>
32#include <Kernel/VM/PhysicalPage.h>
33#include <Kernel/VM/PhysicalRegion.h>
34
35namespace Kernel {
36
37NonnullRefPtr<PhysicalRegion> PhysicalRegion::create(PhysicalAddress lower, PhysicalAddress upper)
38{
39 return adopt(*new PhysicalRegion(lower, upper));
40}
41
42PhysicalRegion::PhysicalRegion(PhysicalAddress lower, PhysicalAddress upper)
43 : m_lower(lower)
44 , m_upper(upper)
45 , m_bitmap(Bitmap::create())
46{
47}
48
49void PhysicalRegion::expand(PhysicalAddress lower, PhysicalAddress upper)
50{
51 ASSERT(!m_pages);
52
53 m_lower = lower;
54 m_upper = upper;
55}
56
57unsigned PhysicalRegion::finalize_capacity()
58{
59 ASSERT(!m_pages);
60
61 m_pages = (m_upper.get() - m_lower.get()) / PAGE_SIZE;
62 m_bitmap.grow(m_pages, false);
63
64 return size();
65}
66
67Vector<RefPtr<PhysicalPage>> PhysicalRegion::take_contiguous_free_pages(size_t count, bool supervisor)
68{
69 ASSERT(m_pages);
70 ASSERT(m_used != m_pages);
71
72 Vector<RefPtr<PhysicalPage>> physical_pages;
73 physical_pages.ensure_capacity(count);
74
75 auto first_contiguous_page = find_contiguous_free_pages(count);
76
77 for (size_t index = 0; index < count; index++) {
78 physical_pages.append(PhysicalPage::create(m_lower.offset(PAGE_SIZE * (index + first_contiguous_page)), supervisor));
79 }
80 return physical_pages;
81}
82
83unsigned PhysicalRegion::find_contiguous_free_pages(size_t count)
84{
85 ASSERT(count != 0);
86 // search from the last page we allocated
87 auto range = find_and_allocate_contiguous_range(count);
88 ASSERT(range.has_value());
89 return range.value();
90}
91
92Optional<unsigned> PhysicalRegion::find_and_allocate_contiguous_range(size_t count)
93{
94 ASSERT(count != 0);
95 size_t found_pages_count = 0;
96 auto first_index = m_bitmap.find_longest_range_of_unset_bits(count, found_pages_count);
97 if (!first_index.has_value())
98 return {};
99
100 auto page = first_index.value();
101 if (count == found_pages_count) {
102 for (unsigned page_index = page; page_index < (page + count); page_index++) {
103 m_bitmap.set(page_index, true);
104 }
105 m_used += count;
106 m_last = page + count;
107 return page;
108 }
109 return {};
110}
111
112RefPtr<PhysicalPage> PhysicalRegion::take_free_page(bool supervisor)
113{
114 ASSERT(m_pages);
115
116 if (m_used == m_pages)
117 return nullptr;
118
119 return PhysicalPage::create(m_lower.offset(find_contiguous_free_pages(1) * PAGE_SIZE), supervisor);
120}
121
122void PhysicalRegion::return_page_at(PhysicalAddress addr)
123{
124 ASSERT(m_pages);
125
126 if (m_used == 0) {
127 ASSERT_NOT_REACHED();
128 }
129
130 ptrdiff_t local_offset = addr.get() - m_lower.get();
131 ASSERT(local_offset >= 0);
132 ASSERT((FlatPtr)local_offset < (FlatPtr)(m_pages * PAGE_SIZE));
133
134 auto page = (FlatPtr)local_offset / PAGE_SIZE;
135 if (page < m_last)
136 m_last = page;
137
138 m_bitmap.set(page, false);
139 m_used--;
140}
141
142}