kernel/vm: handle page protections in cut_area

- Resize the `page_protections` array in `cut_area` and also shift
the bits if necessary.
- Set the correct protection array as well as the real page
protections for the second area produced by `cut_area`.

Change-Id: I62293480487e828970ebe5a3bc729cec2a14c687
This commit is contained in:
Trung Nguyen
2023-05-31 14:47:40 -04:00
committed by Augustin Cavalier
parent 338fedd65a
commit de07bc3fa5
4 changed files with 187 additions and 45 deletions
+57
View File
@@ -16,6 +16,8 @@
# include <Debug.h>
#endif
#include <string.h>
#include <SupportDefs.h>
namespace BKernel {
@@ -36,6 +38,8 @@ public:
ssize_t GetHighestSet() const;
template<typename T>
static void Shift(T* bits, size_t bitCount, ssize_t shift);
private:
size_t fElementsCount;
size_t fSize;
@@ -77,6 +81,59 @@ Bitmap::Clear(size_t index)
fBits[kArrayElement] &= ~addr_t(kBitMask);
}
template<typename T>
void
Bitmap::Shift(T* bits, size_t bitCount, ssize_t shift)
{
if (shift == 0)
return;
const size_t bitsPerElement = sizeof(T) * 8;
const size_t elementsCount = (bitCount + bitsPerElement - 1) / bitsPerElement;
const size_t absoluteShift = (shift > 0) ? shift : -shift;
const size_t nElements = absoluteShift / bitsPerElement;
const size_t nBits = absoluteShift % bitsPerElement;
if (nElements != 0) {
if (shift > 0) {
// "Left" shift.
memmove(&bits[nElements], bits, sizeof(T) * (elementsCount - nElements));
memset(bits, 0, sizeof(T) * nElements);
} else if (shift < 0) {
// "Right" shift.
memmove(bits, &bits[nElements], sizeof(T) * (elementsCount - nElements));
memset(&bits[elementsCount - nElements], 0, sizeof(T) * nElements);
}
}
// If the shift was by a multiple of the element size, nothing more to do.
if (nBits == 0)
return;
// One set of bits comes from the "current" element and are shifted in the
// direction of the shift; the other set comes from the next-processed
// element and are shifted in the opposite direction.
if (shift > 0) {
// "Left" shift.
for (ssize_t i = elementsCount - 1; i >= 0; i--) {
T low = 0;
if (i != 0)
low = bits[i - 1] >> (bitsPerElement - nBits);
const T high = bits[i] << nBits;
bits[i] = low | high;
}
} else if (shift < 0) {
// "Right" shift.
for (size_t i = 0; i < elementsCount; i++) {
const T low = bits[i] >> nBits;
T high = 0;
if (i != (elementsCount - 1))
high = bits[i + 1] << (bitsPerElement - nBits);
bits[i] = low | high;
}
}
}
} // namespace BKernel