Serenity Operating System
at portability 118 lines 3.6 kB view raw
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 <Kernel/Assertions.h> 31#include <Kernel/VM/PhysicalPage.h> 32#include <Kernel/VM/PhysicalRegion.h> 33 34namespace Kernel { 35 36NonnullRefPtr<PhysicalRegion> PhysicalRegion::create(PhysicalAddress lower, PhysicalAddress upper) 37{ 38 return adopt(*new PhysicalRegion(lower, upper)); 39} 40 41PhysicalRegion::PhysicalRegion(PhysicalAddress lower, PhysicalAddress upper) 42 : m_lower(lower) 43 , m_upper(upper) 44 , m_bitmap(Bitmap::create()) 45{ 46} 47 48void PhysicalRegion::expand(PhysicalAddress lower, PhysicalAddress upper) 49{ 50 ASSERT(!m_pages); 51 52 m_lower = lower; 53 m_upper = upper; 54} 55 56unsigned PhysicalRegion::finalize_capacity() 57{ 58 ASSERT(!m_pages); 59 60 m_pages = (m_upper.get() - m_lower.get()) / PAGE_SIZE; 61 m_bitmap.grow(m_pages, false); 62 63 return size(); 64} 65 66RefPtr<PhysicalPage> PhysicalRegion::take_free_page(bool supervisor) 67{ 68 ASSERT(m_pages); 69 70 if (m_used == m_pages) 71 return nullptr; 72 73 // search from the last page we allocated 74 for (unsigned page = m_last; page < m_pages; page++) { 75 if (!m_bitmap.get(page)) { 76 m_bitmap.set(page, true); 77 m_used++; 78 m_last = page + 1; 79 return PhysicalPage::create(m_lower.offset(page * PAGE_SIZE), supervisor); 80 } 81 } 82 83 // wrap back around to the start in case we missed something 84 for (unsigned page = 0; page < m_last; page++) { 85 if (!m_bitmap.get(page)) { 86 m_bitmap.set(page, true); 87 m_used++; 88 m_last = page + 1; 89 return PhysicalPage::create(m_lower.offset(page * PAGE_SIZE), supervisor); 90 } 91 } 92 93 ASSERT_NOT_REACHED(); 94 95 return nullptr; 96} 97 98void PhysicalRegion::return_page_at(PhysicalAddress addr) 99{ 100 ASSERT(m_pages); 101 102 if (m_used == 0) { 103 ASSERT_NOT_REACHED(); 104 } 105 106 ptrdiff_t local_offset = addr.get() - m_lower.get(); 107 ASSERT(local_offset >= 0); 108 ASSERT((uintptr_t)local_offset < (uintptr_t)(m_pages * PAGE_SIZE)); 109 110 auto page = (uintptr_t)local_offset / PAGE_SIZE; 111 if (page < m_last) 112 m_last = page; 113 114 m_bitmap.set(page, false); 115 m_used--; 116} 117 118}