<commit_before>#include <kernel.hpp>
#include <os.hpp>
#include <kernel/memory.hpp>
#include <kprint>

#define HIGHMEM_LOCATION  (1ull << 45)
#define LIVEUPDATE_AREA_SIZE 25
static_assert(LIVEUPDATE_AREA_SIZE > 0 && LIVEUPDATE_AREA_SIZE < 50,
              "LIVEUPDATE_AREA_SIZE must be a value between 1 and 50");

static uintptr_t temp_phys = 0;

//#define LIU_DEBUG 1
#ifdef LIU_DEBUG
#define PRATTLE(fmt, ...) kprintf(fmt, ##__VA_ARGS__)
#else
#define PRATTLE(fmt, ...) /* fmt */
#endif

size_t kernel::liveupdate_phys_size(size_t phys_max) noexcept {
  return phys_max / (100 / size_t(LIVEUPDATE_AREA_SIZE));
};

uintptr_t kernel::liveupdate_phys_loc(size_t phys_max)  noexcept {
  return (phys_max - liveupdate_phys_size(phys_max)) & ~(uintptr_t) 0xFFF;
};

void kernel::setup_liveupdate(uintptr_t phys)
{
  PRATTLE("Setting up LiveUpdate with phys at %p\n", (void*) phys);
  if (phys != 0) {
    PRATTLE("Deferring setup because too early\n");
    temp_phys = phys; return;
  }
  if (kernel::state().liveupdate_loc != 0) return;
  PRATTLE("New virtual move heap_max: %p\n", (void*) OS::heap_max());

  // highmem location
  kernel::state().liveupdate_loc = HIGHMEM_LOCATION;

  size_t size = 0;
  if (phys == 0) {
    size = kernel::liveupdate_phys_size(kernel::heap_max());
    phys = kernel::liveupdate_phys_loc(kernel::heap_max());
  }
  else {
    size = kernel::heap_max() - phys;
  }
  size &= ~(uintptr_t) 0xFFF; // page sized

  // move location to high memory
  const uintptr_t dst = (uintptr_t) kernel::liveupdate_storage_area();
  PRATTLE("virtual_move %p to %p (%zu bytes)\n",
          (void*) phys, (void*) dst, size);
  os::mem::virtual_move(phys, size, dst, "LiveUpdate");
}
<commit_msg>liu: Set update loc to temp phys if 0<commit_after>#include <kernel.hpp>
#include <os.hpp>
#include <kernel/memory.hpp>
#include <kprint>

#define HIGHMEM_LOCATION  (1ull << 45)
#define LIVEUPDATE_AREA_SIZE 25
static_assert(LIVEUPDATE_AREA_SIZE > 0 && LIVEUPDATE_AREA_SIZE < 50,
              "LIVEUPDATE_AREA_SIZE must be a value between 1 and 50");

static uintptr_t temp_phys = 0;

//#define LIU_DEBUG 1
#ifdef LIU_DEBUG
#define PRATTLE(fmt, ...) kprintf(fmt, ##__VA_ARGS__)
#else
#define PRATTLE(fmt, ...) /* fmt */
#endif

size_t kernel::liveupdate_phys_size(size_t phys_max) noexcept {
  return phys_max / (100 / size_t(LIVEUPDATE_AREA_SIZE));
};

uintptr_t kernel::liveupdate_phys_loc(size_t phys_max)  noexcept {
  return (phys_max - liveupdate_phys_size(phys_max)) & ~(uintptr_t) 0xFFF;
};

void kernel::setup_liveupdate(uintptr_t phys)
{
  PRATTLE("Setting up LiveUpdate with phys at %p\n", (void*) phys);
  if (phys != 0) {
    PRATTLE("Deferring setup because too early\n");
    temp_phys = phys; return;
  }
  else {
    PRATTLE("Using temp at %p\n", (void*) temp_phys);
    phys = temp_phys;
  }
  if (kernel::state().liveupdate_loc != 0) return;
  PRATTLE("New virtual move heap_max: %p\n", (void*) OS::heap_max());

  // highmem location
  kernel::state().liveupdate_loc = HIGHMEM_LOCATION;

  size_t size = 0;
  if (phys == 0) {
    size = kernel::liveupdate_phys_size(kernel::heap_max());
    phys = kernel::liveupdate_phys_loc(kernel::heap_max());
  }
  else {
    size = kernel::heap_max() - phys;
  }
  size &= ~(uintptr_t) 0xFFF; // page sized

  // move location to high memory
  const uintptr_t dst = (uintptr_t) kernel::liveupdate_storage_area();
  PRATTLE("virtual_move %p to %p (%zu bytes)\n",
          (void*) phys, (void*) dst, size);
  os::mem::virtual_move(phys, size, dst, "LiveUpdate");
}

{'fraction_non_alphanumeric': 0.14661760543447494, 'fraction_numerical': 0.01273705066515709, 'mean_word_length': 4.436923076923077, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2603076', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1294, 'n_words': 307}