// SPDX-License-Identifier: MPL-2.0
#include "std/stdlib.h"
#include "kernel/test.h"
#include "kernel/log.h"
#include "kernel/conf.h"
#include "cpu/tsc.h"
#include "cpu/serial.h"
#include "memory/page.h"
#include "memory/slab.h"
#include "memory/pba.h"
#include "memory/heap.h"
#include "memory/pez/radix.h"
#include "memory/pez/pez.h"
int main() {
  sh_tsc_init_tsc();
  sh_log_string("\n---------------------------\n\n");
  sh_log_log("Shelter v0.1",SH_LOG_SOURCE_MAIN);
  sh_conf_BOOT_CONFIG *boot_config=SH_NULLPTR;
  SH_STATUS status=sh_conf_get_boot_config(&boot_config);
  if (status==SH_STATUS_INVALID_SIGNATURE) {
    sh_log_critical("Boot config doesn't have signature on.",SH_LOG_SOURCE_CONF);
    sh_log_fatal("Can't obtain boot config.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  } else if (sh_status_error(status)) {
    sh_log_error("Can't find boot config.",SH_LOG_SOURCE_CONF);
    sh_log_fatal("Can't obtain boot config.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  }
  sh_log_log("Obtained boot config.",SH_LOG_SOURCE_CONF);
  sh_log_log("Loading logging ring buffer setting...",SH_LOG_SOURCE_MAIN);
  sh_log_load_logging_ring_size(boot_config->log_ring_size);
  sh_log_log("Loading serial port setting...",SH_LOG_SOURCE_MAIN);
  sh_log_load_serial_setting(boot_config->log_disable_serial_port);
  sh_serial_load_serial_port_setting(boot_config->disable_serial_port);
  sh_log_log("Loading PTP VA...",SH_LOG_SOURCE_MAIN);
  sh_page_load_boot_ptp_va(boot_config->page_table_pool_va);
  sh_log_log("Loading log level...",SH_LOG_SOURCE_MAIN);
  sh_log_load_log_level(boot_config->log_level);
  sh_log_string("[Shelter:Log @Log]      ");
  sh_log_uint64((sh_uint64)sh_tsc_get_kernel_current_tsc());
  sh_log_string(" : Successfully set log level to ");
  sh_log_uint64((sh_uint64)sh_log_get_log_level());
  sh_log_string("\n");
  sh_log_flog(SH_LOG_SOURCE_MAIN,"Set logging ring buffer to %2u pages.\n",boot_config->log_ring_size);
  sh_log_log("Trying to obtain memory map...",SH_LOG_SOURCE_MAIN);
  status=sh_page_copy_memory_map();
  if (status==SH_STATUS_SUCCESS) {
    sh_log_log("Successfully copied memory map.",SH_LOG_SOURCE_PAGE);
    sh_log_log("Successfully obtained memory map.",SH_LOG_SOURCE_MAIN);
  } else {
    sh_log_error("Can't copy memory map.",SH_LOG_SOURCE_PAGE);
    sh_log_fatal("Can't obtain memory map.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  }
  sh_log_log("Checking memory map...",SH_LOG_SOURCE_MAIN);
  if (status==SH_STATUS_SUCCESS) {
    sh_log_log("Successfully checked memory map.",SH_LOG_SOURCE_MAIN);
  } else if (status==SH_STATUS_INVALID_SIGNATURE) {
    sh_log_fatal("Can't check memory map.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  } else if (status==SH_STATUS_MMAP_BUFFER_OVERFLOW) {
    sh_log_fatal("Memory map is overflowing. Can't safely continue.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  }
  sh_log_log("Initializing current page table pool...",SH_LOG_SOURCE_MAIN);
  sh_page_PAGE_TABLE_POOL kernel_ptp;
  status=sh_page_init_ptp(boot_config->page_table_pool_pa,sh_page_get_boot_ptp_va(),boot_config->page_table_allocator_level,&kernel_ptp);
  if (status==SH_STATUS_PT_POOL_NO_BITMAP_INIT) {
    sh_log_fatal("Couldn't initialize current page table pool.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  } else if (status==SH_STATUS_PT_POOL_NO_PAGE_SET) {
    sh_log_fatal("Couldn't initialize current page table pool.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  }
  sh_log_log("Successfully initialized current page table pool.",SH_LOG_SOURCE_MAIN);
  sh_log_log("Parsing memory map...",SH_LOG_SOURCE_MAIN);
  status=sh_page_analyse_memory_map(&kernel_ptp);
  if (sh_status_error(status)) {
    sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't parse memory map. Error code: %8s\n",status);
    while (SH_TRUE) {};
  }
  sh_log_log("Successfully parsed memory map.",SH_LOG_SOURCE_MAIN);
  sh_log_mem_stats(SH_LOG_SOURCE_MAIN);
  sh_log_log("Page managment subsystem successfully initialized",SH_LOG_SOURCE_MAIN);
  sh_slab_reg_phys_SLAB_ALLOCATOR slab_reg_phys;
  status=sh_slab_reg_phys_alloc_init(&slab_reg_phys,&kernel_ptp);
  if (sh_status_error(status)) {
    sh_log_fatal("Couldn't initialize physical region object slab allocator.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  }
  sh_log_log("Successfully initialize physical region object slab allocator.",SH_LOG_SOURCE_MAIN);
  sh_log_log("Allocating first slab...",SH_LOG_SOURCE_MAIN);
  sh_slab_reg_phys_SLAB_STRUCT *first_slab_rp=SH_NULLPTR;
  status=sh_slab_reg_phys_add_slab(&slab_reg_phys,&kernel_ptp,&first_slab_rp);
  if (status!=SH_STATUS_SUCCESS) {
    sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't allocate first slab. Error code: %8s\n",status);
    while (SH_TRUE) {};
  } else {
    sh_log_log("Successfully allocated first slab. Verifying data...",SH_LOG_SOURCE_MAIN);
    if (first_slab_rp->free_bitmap[0]!=1 ||
        first_slab_rp->slab_index!=0 ||
        first_slab_rp->next!=SH_NULLPTR ||
        first_slab_rp->prev!=SH_NULLPTR ||
        first_slab_rp->used_count!=0) {
          sh_log_error("One of the attributes inside first slab is wrong.",SH_LOG_SOURCE_MAIN);
          sh_log_fatal("Stopping for safety.",SH_LOG_SOURCE_MAIN);
          while (SH_TRUE) {};
        } else {
          sh_log_log("All attributes are good.",SH_LOG_SOURCE_MAIN);
        }
  }
  sh_slab_reg_virt_SLAB_ALLOCATOR slab_reg_virt;
  status=sh_slab_reg_virt_alloc_init(&slab_reg_virt,&kernel_ptp);
  if (sh_status_error(status)) {
    sh_log_fatal("Couldn't initialize virtual region object slab allocator.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  }
  sh_log_log("Successfully initialize virtual region object slab allocator.",SH_LOG_SOURCE_MAIN);
  sh_log_log("Allocating first slab...",SH_LOG_SOURCE_MAIN);
  sh_slab_reg_virt_SLAB_STRUCT *first_slab_rv=SH_NULLPTR;
  status=sh_slab_reg_virt_add_slab(&slab_reg_virt,&kernel_ptp,&first_slab_rv);
  if (status!=SH_STATUS_SUCCESS) {
    sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't allocate first slab. Error code: %8s\n",status);
    while (SH_TRUE) {};
  } else {
    sh_log_log("Successfully allocated first slab. Verifying data...",SH_LOG_SOURCE_MAIN);
    if (first_slab_rv->free_bitmap[0]!=1 ||
        first_slab_rv->slab_index!=0 ||
        first_slab_rv->next!=SH_NULLPTR ||
        first_slab_rv->prev!=SH_NULLPTR ||
        first_slab_rv->used_count!=0) {
          sh_log_error("One of the attributes inside first slab is wrong.",SH_LOG_SOURCE_MAIN);
          sh_log_fatal("Stopping for safety.",SH_LOG_SOURCE_MAIN);
          while (SH_TRUE) {};
        } else {
          sh_log_log("All attributes are good.",SH_LOG_SOURCE_MAIN);
        }
  }
  sh_pba_PAGE_BLOCK_ALLOCATOR slab_radix_node_pba;
  status=sh_pba_init(&slab_radix_node_pba,SH_VMEM_LAYOUT_SLAB_RADIX_NODE_VA,SH_VMEM_LAYOUT_SLAB_RADIX_NODE_SIZE_BYTES/SH_PAGE_SIZE,SH_SLAB_RADIX_NODE_SLAB_DATA_PAGES);
  if (sh_status_error(status)) {
    sh_log_fatal("Couldn't initialize PBA for radix nodes slab allocator.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  }
  sh_log_log("Successfully initialize PBA for radix nodes slab allocator.",SH_LOG_SOURCE_MAIN);
  struct sh_slab_radix_node_SLAB_ALLOCATOR slab_radix_node;
  status=sh_slab_radix_node_alloc_init(&slab_radix_node,&slab_radix_node_pba);
  if (sh_status_error(status)) {
    sh_log_fatal("Couldn't initialize radix nodes slab allocator.",SH_LOG_SOURCE_MAIN);
    while (SH_TRUE) {};
  }
  sh_log_log("Successfully initialize radix nodes slab allocator.",SH_LOG_SOURCE_MAIN);
  sh_log_log("Allocating first slab...",SH_LOG_SOURCE_MAIN);
  sh_slab_radix_node_SLAB *first_slab_rn=SH_NULLPTR;
  status=sh_slab_radix_node_add_slab(&slab_radix_node,&kernel_ptp,&first_slab_rn);
  if (status!=SH_STATUS_SUCCESS) {
    sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't allocate first slab. Error code: %8s\n",status);
    while (SH_TRUE) {};
  } else {
    sh_log_log("Successfully allocated first slab. Verifying data...",SH_LOG_SOURCE_MAIN);
    if (first_slab_rn->free_bitmap[0]!=0x3FFFF ||
        first_slab_rn->slab_index!=0 ||
        first_slab_rn->next_slab!=SH_NULLPTR ||
        first_slab_rn->prev_slab!=SH_NULLPTR ||
        first_slab_rn->next_partial!=SH_NULLPTR ||
        first_slab_rn->prev_partial!=SH_NULLPTR ||
        first_slab_rn->used_count!=0 ) {
          sh_log_error("One of the attributes inside first slab is wrong.",SH_LOG_SOURCE_MAIN);
          sh_log_fatal("Stopping for safety.",SH_LOG_SOURCE_MAIN);
          while (SH_TRUE) {};
        } else {
          sh_log_log("All attributes are good.",SH_LOG_SOURCE_MAIN);
        }
  }
  if (boot_config->test_benchmark) {
    status=sh_test_slabs_benchmark(&slab_reg_phys,&slab_reg_virt,&slab_radix_node,&kernel_ptp);
    if (status!=SH_STATUS_SUCCESS) {
      sh_log_fatal("Couldn't benchmark one of the slab allocator.",SH_LOG_SOURCE_MAIN);
      while (SH_TRUE) {};
    }
    status=sh_test_radix_benchmark(&slab_radix_node,&kernel_ptp);
    if (status!=SH_STATUS_SUCCESS) {
      sh_log_fatal("Couldn't benchmark radix trees subsystem.",SH_LOG_SOURCE_MAIN);
      while (SH_TRUE) {};
    }
  }
  sh_log_log("Creating Pez physical plane...",SH_LOG_SOURCE_MAIN);
  sh_pez_PHYSICAL_PLANE physical_plane;
  status=sh_pez_init_physical_plane((sh_uint8*)sh_page_get_physical_bitmap_ptr(),sh_page_get_physical_memory_amount_pages(),&slab_reg_phys,&slab_radix_node,&kernel_ptp,&physical_plane);
  if (status!=SH_STATUS_SUCCESS) {
    sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't create Pez physical plane. Error code: %8s\n",status);
    while (SH_TRUE) {};
  }
  sh_log_log("Succesfully created Pez physical plane.",SH_LOG_SOURCE_MAIN);
  if (boot_config->test_benchmark) {
    status=sh_test_pez_benchmark_physical(&physical_plane);
    if (status!=SH_STATUS_SUCCESS) {
      sh_log_fatal("Couldn't benchmark Pez physical plane.",SH_LOG_SOURCE_MAIN);
      while (SH_TRUE) {};
    }
  }
  sh_log_log("Creating Pez virtual plane for kernel heap...",SH_LOG_SOURCE_MAIN);
  sh_pez_VIRTUAL_PLANE virtual_plane_kernel_heap;
  status=sh_pez_init_virtual_plane(SH_VMEM_LAYOUT_HEAP_BIG_VA,&slab_reg_virt,&slab_radix_node,&kernel_ptp,&kernel_ptp,&virtual_plane_kernel_heap);
  if (status!=SH_STATUS_SUCCESS) {
    sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't create Pez virtual plane for kernel heap. Error code: %8s\n",status);
    while (SH_TRUE) {};
  }
  sh_log_log("Succesfully created Pez virtual plane for kernel heap.",SH_LOG_SOURCE_MAIN);
  sh_pez_set_available();
  sh_heap_KERNEL_HEAP kernel_heap;
  status=sh_heap_init_heap(&physical_plane,&virtual_plane_kernel_heap,&kernel_ptp,&kernel_heap);
  if (sh_status_error(status)) {
    sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't initialize kernel heap. Error code: %8s",status);
    while (SH_TRUE) {};
  }
  sh_uint64 slab_data_pages[8]=SH_SLAB_GENERIC_SLAB_DATA_PAGES;
  for (sh_iter64 i=0;i<8;i++) {
    status=sh_pba_init(&kernel_heap.pba[i],SH_VMEM_LAYOUT_HEAP_SLAB_LEVEL_0_VA+i*SH_VMEM_LAYOUT_HEAP_SLAB_SPACING,((SH_VMEM_LAYOUT_HEAP_SLAB_LEVEL_0_SIZE_BYTES/SH_PAGE_SIZE)/slab_data_pages[i])*slab_data_pages[i],slab_data_pages[i]);
    if (sh_status_error(status)) {
      sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't create PBA for heap allocations level %8u. Error code: %8s\n",i,status);
      while (SH_TRUE);
    }
    status=sh_slab_generic_alloc_init((sh_uint8)i,&kernel_heap.slabs_allocator[i],&kernel_heap.pba[i]);
    if (sh_status_error(status)) {
      sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't create slab allocator for heap allocations level %8u. Error code: %8s\n",i,status);
      while (SH_TRUE);
    }
    sh_slab_generic_SLAB *first_slab_generic;
    status=sh_slab_generic_add_slab(&kernel_heap.slabs_allocator[i],&kernel_ptp,&first_slab_generic);
    if (status!=SH_STATUS_SUCCESS) {
      sh_log_ffatal(SH_LOG_SOURCE_MAIN,"Couldn't allocate first slab for heap allocations level %8u. Error code: %8s\n",i,status);
      while (SH_TRUE) {};
    } else {
      if (first_slab_generic->free_bitmap[0]!=kernel_heap.slabs_allocator[i].bitmap_init ||
          first_slab_generic->slab_index!=0 ||
          first_slab_generic->next_slab!=SH_NULLPTR ||
          first_slab_generic->prev_slab!=SH_NULLPTR ||
          first_slab_generic->next_partial!=SH_NULLPTR ||
          first_slab_generic->prev_partial!=SH_NULLPTR ||
          first_slab_generic->used_count!=0 ) {
            sh_log_error("One of the attributes inside first slab is wrong.",SH_LOG_SOURCE_MAIN);
            sh_log_fatal("Stopping for safety.",SH_LOG_SOURCE_MAIN);
            while (SH_TRUE) {};
          }
    }
    sh_log_flog(SH_LOG_SOURCE_MAIN,"Succesfully created heap slab allocator with level %8u.\n",i);
  }
  sh_heap_load_default_heap(&kernel_heap);
  sh_log_log("Succesfully created kernel heap.",SH_LOG_SOURCE_MAIN);
  if (boot_config->test_benchmark) {
    status=sh_test_malloc_benchmark();
    if (status!=SH_STATUS_SUCCESS) {
      sh_log_fatal("Couldn't benchmark Pez physical plane.",SH_LOG_SOURCE_MAIN);
      while (SH_TRUE) {};
    }
  }
  while (1) {};
  return 0;
}
