#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/atomic.h>
#include <sys/bitmap.h>
#include <sys/systm.h>
#include <vm/seg_kmem.h>
#include <vm/hat.h>
#include <vm/vm_dep.h>
#include <vm/hat_i86.h>
#include <sys/cmn_err.h>
#include <sys/avl.h>
struct hment {
avl_node_t hm_hashlink;
struct hment *hm_next;
struct hment *hm_prev;
htable_t *hm_htable;
pfn_t hm_pfn;
uint16_t hm_entry;
uint16_t hm_pad;
uint32_t hm_pad2;
};
#define HMENT_EMBEDDED ((hment_t *)(uintptr_t)1)
kmem_cache_t *hment_cache;
#define HMENT_RESERVE_AMOUNT (200)
uint_t hment_reserve_amount = HMENT_RESERVE_AMOUNT;
kmutex_t hment_reserve_mutex;
uint_t hment_reserve_count;
hment_t *hment_reserve_pool;
#define HMENT_HASH_SIZE (64 * 1024)
static uint_t hment_hash_entries = HMENT_HASH_SIZE;
static avl_tree_t *hment_table;
#define HMENT_HASH(pfn, entry) (uint32_t) \
((((pfn) << 9) + entry + pfn) & (hment_hash_entries - 1))
#define MLIST_NUM_LOCK 2048
static kmutex_t *mlist_lock;
#define MLIST_MUTEX(pp) \
&mlist_lock[((pp)->p_pagenum + ((pp)->p_pagenum >> 9)) & \
(MLIST_NUM_LOCK - 1)]
#define HASH_NUM_LOCK 2048
static kmutex_t *hash_lock;
#define HASH_MUTEX(idx) &hash_lock[(idx) & (HASH_NUM_LOCK-1)]
static avl_node_t null_avl_link;
static hment_t *hment_steal(void);
static int
hment_compare(const void *hm1, const void *hm2)
{
hment_t *h1 = (hment_t *)hm1;
hment_t *h2 = (hment_t *)hm2;
long diff;
diff = (uintptr_t)h1->hm_htable - (uintptr_t)h2->hm_htable;
if (diff == 0) {
diff = h1->hm_entry - h2->hm_entry;
if (diff == 0)
diff = h1->hm_pfn - h2->hm_pfn;
}
if (diff < 0)
diff = -1;
else if (diff > 0)
diff = 1;
return (diff);
}
static void
hment_put_reserve(hment_t *hm)
{
HATSTAT_INC(hs_hm_put_reserve);
mutex_enter(&hment_reserve_mutex);
hm->hm_next = hment_reserve_pool;
hment_reserve_pool = hm;
++hment_reserve_count;
mutex_exit(&hment_reserve_mutex);
}
static hment_t *
hment_get_reserve(void)
{
hment_t *hm = NULL;
HATSTAT_INC(hs_hm_get_reserve);
mutex_enter(&hment_reserve_mutex);
if (hment_reserve_count != 0) {
hm = hment_reserve_pool;
hment_reserve_pool = hm->hm_next;
--hment_reserve_count;
}
mutex_exit(&hment_reserve_mutex);
return (hm);
}
static hment_t *
hment_alloc()
{
int km_flag = can_steal_post_boot ? KM_NOSLEEP : KM_SLEEP;
hment_t *hm = NULL;
HATSTAT_INC(hs_hm_alloc);
if (!USE_HAT_RESERVES()) {
for (;;) {
hm = kmem_cache_alloc(hment_cache, km_flag);
if (hm == NULL ||
USE_HAT_RESERVES() ||
hment_reserve_count >= hment_reserve_amount)
break;
hment_put_reserve(hm);
}
}
if (hm == NULL) {
if (USE_HAT_RESERVES())
hm = hment_get_reserve();
if (hm == NULL && can_steal_post_boot)
hm = hment_steal();
if (hm == NULL)
hm = hment_get_reserve();
if (hm == NULL)
panic("hment_alloc(): no reserve, couldn't steal");
}
hm->hm_entry = 0;
hm->hm_htable = NULL;
hm->hm_hashlink = null_avl_link;
hm->hm_next = NULL;
hm->hm_prev = NULL;
hm->hm_pfn = PFN_INVALID;
return (hm);
}
void
hment_free(hment_t *hm)
{
#ifdef DEBUG
bzero(hm, sizeof (*hm));
#endif
HATSTAT_INC(hs_hm_free);
if (USE_HAT_RESERVES() ||
hment_reserve_count < hment_reserve_amount) {
hment_put_reserve(hm);
} else {
kmem_cache_free(hment_cache, hm);
hment_adjust_reserve();
}
}
int
x86_hm_held(page_t *pp)
{
ASSERT(pp != NULL);
if (mlist_lock == NULL)
return (1);
return (MUTEX_HELD(MLIST_MUTEX(pp)));
}
void
x86_hm_enter(page_t *pp)
{
ASSERT(pp != NULL);
if (mlist_lock != NULL)
mutex_enter(MLIST_MUTEX(pp));
}
void
x86_hm_exit(page_t *pp)
{
ASSERT(pp != NULL);
if (mlist_lock != NULL)
mutex_exit(MLIST_MUTEX(pp));
}
static void
hment_insert(hment_t *hm, page_t *pp)
{
uint_t idx;
ASSERT(x86_hm_held(pp));
ASSERT(!pp->p_embed);
++pp->p_share;
hm->hm_next = pp->p_mapping;
if (pp->p_mapping != NULL)
((hment_t *)pp->p_mapping)->hm_prev = hm;
pp->p_mapping = hm;
idx = HMENT_HASH(hm->hm_htable->ht_pfn, hm->hm_entry);
mutex_enter(HASH_MUTEX(idx));
avl_add(&hment_table[idx], hm);
mutex_exit(HASH_MUTEX(idx));
}
hment_t *
hment_prepare(htable_t *htable, uint_t entry, page_t *pp)
{
hment_t *hm = NULL;
ASSERT(x86_hm_held(pp));
for (;;) {
if (pp->p_mapping == NULL) {
ASSERT(!pp->p_embed);
ASSERT(pp->p_share == 0);
if (hm == NULL)
break;
goto free_and_continue;
}
if (pp->p_embed) {
ASSERT(pp->p_mapping != NULL);
if (pp->p_mapping == htable && pp->p_mlentry == entry) {
if (hm == NULL)
break;
goto free_and_continue;
}
if (hm != NULL) {
hm->hm_htable = pp->p_mapping;
hm->hm_entry = pp->p_mlentry;
hm->hm_pfn = pp->p_pagenum;
pp->p_mapping = NULL;
pp->p_share = 0;
pp->p_embed = 0;
hment_insert(hm, pp);
}
goto allocate_and_continue;
}
if (hm != NULL)
break;
allocate_and_continue:
x86_hm_exit(pp);
hm = hment_alloc();
x86_hm_enter(pp);
continue;
free_and_continue:
x86_hm_exit(pp);
hment_free(hm);
hm = NULL;
x86_hm_enter(pp);
}
ASSERT(x86_hm_held(pp));
return (hm);
}
void
hment_assign(htable_t *htable, uint_t entry, page_t *pp, hment_t *hm)
{
ASSERT(x86_hm_held(pp));
if (pp->p_mapping == NULL) {
ASSERT(hm == NULL);
ASSERT(!pp->p_embed);
ASSERT(pp->p_share == 0);
pp->p_embed = 1;
pp->p_mapping = htable;
pp->p_mlentry = entry;
return;
}
ASSERT(!pp->p_embed);
ASSERT(hm != NULL);
hm->hm_htable = htable;
hm->hm_entry = entry;
hm->hm_pfn = pp->p_pagenum;
hment_insert(hm, pp);
}
hment_t *
hment_walk(page_t *pp, htable_t **ht, uint_t *entry, hment_t *prev)
{
hment_t *hm;
ASSERT(x86_hm_held(pp));
if (pp->p_embed) {
if (prev == NULL) {
*ht = (htable_t *)pp->p_mapping;
*entry = pp->p_mlentry;
hm = HMENT_EMBEDDED;
} else {
ASSERT(prev == HMENT_EMBEDDED);
hm = NULL;
}
} else {
if (prev == NULL) {
ASSERT(prev != HMENT_EMBEDDED);
hm = (hment_t *)pp->p_mapping;
} else {
hm = prev->hm_next;
}
if (hm != NULL) {
*ht = hm->hm_htable;
*entry = hm->hm_entry;
}
}
return (hm);
}
hment_t *
hment_remove(page_t *pp, htable_t *ht, uint_t entry)
{
hment_t dummy;
avl_index_t where;
hment_t *hm;
uint_t idx;
ASSERT(x86_hm_held(pp));
if (pp->p_embed) {
ASSERT(ht == (htable_t *)pp->p_mapping);
ASSERT(entry == pp->p_mlentry);
ASSERT(pp->p_share == 0);
pp->p_mapping = NULL;
pp->p_mlentry = 0;
pp->p_embed = 0;
return (NULL);
}
ASSERT(pp->p_share != 0);
dummy.hm_htable = ht;
dummy.hm_entry = entry;
dummy.hm_pfn = pp->p_pagenum;
idx = HMENT_HASH(ht->ht_pfn, entry);
mutex_enter(HASH_MUTEX(idx));
hm = avl_find(&hment_table[idx], &dummy, &where);
if (hm == NULL)
panic("hment_remove() missing in hash table pp=%lx, ht=%lx,"
"entry=0x%x hash index=0x%x", (uintptr_t)pp, (uintptr_t)ht,
entry, idx);
avl_remove(&hment_table[idx], hm);
mutex_exit(HASH_MUTEX(idx));
if (hm->hm_next)
hm->hm_next->hm_prev = hm->hm_prev;
if (hm->hm_prev)
hm->hm_prev->hm_next = hm->hm_next;
else
pp->p_mapping = hm->hm_next;
--pp->p_share;
hm->hm_hashlink = null_avl_link;
hm->hm_next = NULL;
hm->hm_prev = NULL;
return (hm);
}
void
hment_reserve(uint_t count)
{
hment_t *hm;
count += hment_reserve_amount;
while (hment_reserve_count < count) {
hm = kmem_cache_alloc(hment_cache, KM_NOSLEEP);
if (hm == NULL)
return;
hment_put_reserve(hm);
}
}
void
hment_adjust_reserve()
{
hment_t *hm;
while (hment_reserve_count > hment_reserve_amount &&
!USE_HAT_RESERVES()) {
hm = hment_get_reserve();
if (hm == NULL)
return;
kmem_cache_free(hment_cache, hm);
}
}
void
hment_init(void)
{
int i;
int flags = KMC_NOHASH | KMC_NODEBUG;
hment_cache = kmem_cache_create("hment_t",
sizeof (hment_t), 0, NULL, NULL, NULL,
NULL, hat_memload_arena, flags);
hment_table = kmem_zalloc(hment_hash_entries * sizeof (*hment_table),
KM_SLEEP);
mlist_lock = kmem_zalloc(MLIST_NUM_LOCK * sizeof (kmutex_t), KM_SLEEP);
hash_lock = kmem_zalloc(HASH_NUM_LOCK * sizeof (kmutex_t), KM_SLEEP);
for (i = 0; i < hment_hash_entries; ++i)
avl_create(&hment_table[i], hment_compare, sizeof (hment_t),
offsetof(hment_t, hm_hashlink));
for (i = 0; i < MLIST_NUM_LOCK; i++)
mutex_init(&mlist_lock[i], NULL, MUTEX_DEFAULT, NULL);
for (i = 0; i < HASH_NUM_LOCK; i++)
mutex_init(&hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
}
uint_t
hment_mapcnt(page_t *pp)
{
uint_t cnt;
uint_t szc;
page_t *larger;
hment_t *hm;
x86_hm_enter(pp);
if (pp->p_mapping == NULL)
cnt = 0;
else if (pp->p_embed)
cnt = 1;
else
cnt = pp->p_share;
x86_hm_exit(pp);
for (szc = 1; szc <= pp->p_szc; ++szc) {
larger = PP_GROUPLEADER(pp, szc);
if (larger == pp)
continue;
x86_hm_enter(larger);
if (larger->p_mapping != NULL) {
if (larger->p_embed &&
((htable_t *)larger->p_mapping)->ht_level == szc) {
++cnt;
} else if (!larger->p_embed) {
for (hm = larger->p_mapping; hm;
hm = hm->hm_next) {
if (hm->hm_htable->ht_level == szc)
++cnt;
}
}
}
x86_hm_exit(larger);
}
return (cnt);
}
static page_t *last_page = NULL;
static hment_t *
hment_steal(void)
{
page_t *last = last_page;
page_t *pp = last;
hment_t *hm = NULL;
hment_t *hm2;
htable_t *ht;
uint_t found_one = 0;
HATSTAT_INC(hs_hm_steals);
if (pp == NULL)
last = pp = page_first();
while (!found_one) {
HATSTAT_INC(hs_hm_steal_exam);
pp = page_next(pp);
if (pp == NULL)
pp = page_first();
if (pp == last)
return (NULL);
if (pp->p_mapping == NULL || pp->p_embed)
continue;
x86_hm_enter(pp);
if (!pp->p_embed) {
for (hm = pp->p_mapping; hm; hm = hm->hm_next) {
ht = hm->hm_htable;
if (ht->ht_hat != kas.a_hat &&
ht->ht_busy == 0 &&
ht->ht_lock_cnt == 0) {
found_one = 1;
break;
}
}
}
if (!found_one)
x86_hm_exit(pp);
}
hm2 = hati_page_unmap(pp, ht, hm->hm_entry);
ASSERT(hm2 == hm);
last_page = pp;
return (hm);
}