#ifndef _KERNEL_PROC_PROC_H
#define _KERNEL_PROC_PROC_H

#include <aux/compiler.h>
#include <aux/elf.h>
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <sync/spin_lock.h>
#include <sys/mm.h>

#if defined(__x86_64__)
#include <amd64/gdt.h>  /* KSTACK_SIZE */
#include <amd64/proc.h> /* USTACK_SIZE */
#endif

/// Process is ready to run
#define PROC_READY 0
/// Process marked garbage collection
#define PROC_DEAD 1

struct cpu;

struct proc_mapping {
  struct list_node_link proc_mappings_link;

  uintptr_t paddr;
  uintptr_t vaddr;
  size_t size;
};

struct proc {
  int pid;
  struct rb_node_link proc_tree_link;
  struct rb_node_link cpu_run_q_link;

  struct list_node_link* mappings; /* pd.lock implicitly protects this field */
  struct proc_platformdata pdata;
  struct pd pd;
  spin_lock_t lock;
  struct cpu* cpu;
  atomic_int state;
};

void proc_sched (void);
void proc_kill (struct proc* proc);
bool proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, size_t pages,
               uint32_t flags);
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
void proc_init (void);

#endif // _KERNEL_PROC_PROC_H
