Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
hw/arm/apple_a13.c: Fixed wkdm page compression
Browse files Browse the repository at this point in the history
  • Loading branch information
TrungNguyen1909 committed Apr 24, 2022
1 parent 9333154 commit ff04a57
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 28 deletions.
37 changes: 29 additions & 8 deletions target/arm/WKdmCompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ static WK_word *WK_pack_3_tenbits(WK_word *source_buf,

unsigned int WKdm_compress(WK_word *src_buf,
WK_word *dest_buf,
unsigned int size)
int byte_budget)
{
unsigned num_input_words = size / BYTES_PER_WORD;
int byte_budget = size;
unsigned num_input_words = TARGET_PAGE_SIZE / BYTES_PER_WORD;
DictionaryElement dictionary[DICTIONARY_SIZE];

/*
Expand All @@ -99,9 +98,9 @@ unsigned int WKdm_compress(WK_word *src_buf,
* sizes of these arrays should be increased if you want to compress
* pages larger than 16KB
*/
WK_word tempTagsArray[1024]; /* tags for everything */
WK_word tempQPosArray[1024]; /* queue positions for matches */
WK_word tempLowBitsArray[1024]; /* low bits for partial matches */
WK_word tempTagsArray[4096]; /* tags for everything */
WK_word tempQPosArray[4096]; /* queue positions for matches */
WK_word tempLowBitsArray[4096]; /* low bits for partial matches */

/*
* boundary_tmp will be used for keeping track of what's where in
Expand All @@ -125,7 +124,8 @@ unsigned int WKdm_compress(WK_word *src_buf,

WK_word *next_input_word = src_buf;
WK_word *end_of_input = src_buf + num_input_words;
byte_budget -= (TAGS_AREA_OFFSET + TAGS_AREA_SIZE) * BYTES_PER_WORD;
int header_size = (TAGS_AREA_OFFSET + TAGS_AREA_SIZE) * BYTES_PER_WORD;
byte_budget -= header_size;
if (byte_budget <= 0) {
return -1;
}
Expand Down Expand Up @@ -160,8 +160,11 @@ unsigned int WKdm_compress(WK_word *src_buf,
RECORD_PARTIAL(dict_location - dictionary, LOW_BITS(input_word));
*dict_location = input_word;
} else {
RECORD_MISS(input_word);
byte_budget -= 4;
if (byte_budget < 0) {
return -1;
}
RECORD_MISS(input_word);
*dict_location = input_word;
}
}
Expand Down Expand Up @@ -192,6 +195,24 @@ unsigned int WKdm_compress(WK_word *src_buf,
/* same value page */
return SV_RETURN;
}
int sparse_csize = (miss + hits) * 6 + 4;
int normal_csize = 2*partial/3 + miss*4 + hits/2 + header_size;

if (sparse_csize < normal_csize) {
/* Mostly Zero */
*(dest_buf++) = MZV_MAGIC;
unsigned short *output = (unsigned short *)dest_buf;
next_input_word = (WK_word *)src_buf;
while (next_input_word < end_of_input) {
WK_word input_word = *next_input_word;
if (input_word != 0) {
*(uint32_t *)(output += 2) = input_word;
*(output++) = (char *)next_input_word - (char *)src_buf;
}
next_input_word++;
}
return sparse_csize;
}

/*
* Record (into the header) where we stopped writing full words,
Expand Down
20 changes: 16 additions & 4 deletions target/arm/WKdmDecompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,26 @@ bool WKdm_decompress(WK_word *src_buf,
/* and whose contents are packed into the actual output after modeling */

/* sizes of these arrays should be increased if you want to compress
* pages larger than 4KB
* pages larger than 16KB
*/
WK_word tempTagsArray[1024]; /* tags for everything */
WK_word tempQPosArray[1024]; /* queue positions for matches */
WK_word tempLowBitsArray[1024]; /* low bits for partial matches */
WK_word tempTagsArray[4096]; /* tags for everything */
WK_word tempQPosArray[4096]; /* queue positions for matches */
WK_word tempLowBitsArray[4096]; /* low bits for partial matches */

(void)words;

if (*src_buf == MZV_MAGIC) {
unsigned short *input = (unsigned short *)(src_buf + 1);
assert(src_buf != dest_buf);
memset(dest_buf, 0, TARGET_PAGE_SIZE);
WK_word word = *(WK_word *)(input);
input += 2;
int index = *(input++);
*(WK_word *)(((char *)dest_buf) + index) = word;
return true;
}


PRELOAD_DICTIONARY;

if ((TAGS_AREA_START(src_buf) >= src_buf + words)
Expand Down
11 changes: 7 additions & 4 deletions target/arm/WKdm_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ typedef WK_word DictionaryElement;
/* ============================================================ */
/* Misc constants */

#define MZV_MAGIC 0x4321

#define BITS_PER_WORD 32
#define BYTES_PER_WORD 4
#define NUM_LOW_BITS 10
Expand All @@ -110,9 +112,10 @@ typedef WK_word DictionaryElement;
#define MISS_TAG 0x2
#define EXACT_TAG 0x3

#ifndef BITS_PER_BYTE
#define BITS_PER_BYTE 8
#ifdef BITS_PER_BYTE
#undef BITS_PER_BYTE
#endif
#define BITS_PER_BYTE 8

/* ============================================================ */
/* Global macros */
Expand Down Expand Up @@ -209,10 +212,10 @@ extern const char hashLookupTable[];

unsigned int WKdm_compress(WK_word *src_buf,
WK_word *dest_buf,
unsigned int num_input_words);
int byte_budget);

bool WKdm_decompress(WK_word *src_buf,
WK_word *dest_buf,
unsigned int words);
unsigned int size);

#endif /* WKDM_INTERNAL_H */
61 changes: 49 additions & 12 deletions target/arm/helper-a64.c
Original file line number Diff line number Diff line change
Expand Up @@ -1137,8 +1137,7 @@ void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
memset(mem, 0, blocklen);
}

static void *get_page(CPUARMState *env, uint64_t vaddr_in,
MMUAccessType access_type, int mmu_idx)
static void *get_page_read(CPUARMState *env, uint64_t vaddr_in, int mmu_idx)
{
uint64_t vaddr = vaddr_in & TARGET_PAGE_MASK;

Expand All @@ -1158,21 +1157,53 @@ static void *get_page(CPUARMState *env, uint64_t vaddr_in,
return mem;
}

static void *get_page_write(CPUARMState *env, uint64_t vaddr_in, int mmu_idx)
{
uint64_t vaddr = vaddr_in & TARGET_PAGE_MASK;

void *mem = tlb_vaddr_to_host(env, vaddr, MMU_DATA_STORE, mmu_idx);
#ifndef CONFIG_USER_ONLY
if (unlikely(!mem)) {
uintptr_t ra = GETPC();

/*
* Trap if accessing an invalid page.
*/
(void) probe_write(env, vaddr_in, 1, mmu_idx, ra);
mem = probe_write(env, vaddr, TARGET_PAGE_SIZE, mmu_idx, ra);
}
#endif

return mem;
}

uint64_t HELPER(wkdmc)(CPUARMState *env, uint64_t vaddr_in, uint64_t vaddr_out)
{
int mmu_idx = cpu_mmu_index(env, false);
void *in_mem, *out_mem;
char *in_mem, *out_mem;
uint8_t scratch[TARGET_PAGE_SIZE];
uint8_t scratch1[TARGET_PAGE_SIZE];
vaddr_in &= TARGET_PAGE_MASK;
vaddr_out &= TARGET_PAGE_MASK;
vaddr_out &= ~0x3f;
int out_offset = vaddr_out - (vaddr_out & TARGET_PAGE_MASK);
int csize = TARGET_PAGE_SIZE - out_offset;

if (TARGET_PAGE_BITS < 10 || TARGET_PAGE_BITS > 14) {
return -1;
}

in_mem = get_page(env, vaddr_in, MMU_DATA_LOAD, mmu_idx);
out_mem = get_page(env, vaddr_out, MMU_DATA_STORE, mmu_idx);
in_mem = get_page_read(env, vaddr_in, mmu_idx);
out_mem = get_page_write(env, vaddr_out, mmu_idx);
if (in_mem && out_mem) {
int n = WKdm_compress(in_mem, out_mem, TARGET_PAGE_SIZE);
memcpy(scratch1, in_mem, TARGET_PAGE_SIZE);
int n = WKdm_compress(scratch1, scratch, csize);
if (n <= 0) {
return n;
}
if (n > csize) {
return -1;
}
memcpy(out_mem + out_offset, scratch, n);
return n >> 6;
}
return -1;
Expand All @@ -1181,18 +1212,24 @@ uint64_t HELPER(wkdmc)(CPUARMState *env, uint64_t vaddr_in, uint64_t vaddr_out)
uint64_t HELPER(wkdmd)(CPUARMState *env, uint64_t vaddr_in, uint64_t vaddr_out)
{
int mmu_idx = cpu_mmu_index(env, false);
void *in_mem, *out_mem;
vaddr_in &= TARGET_PAGE_MASK;
uint8_t *in_mem, *out_mem;
uint8_t scratch[TARGET_PAGE_SIZE];
uint8_t scratch2[TARGET_PAGE_SIZE];
vaddr_out &= TARGET_PAGE_MASK;
vaddr_in &= ~0x3f;
int in_offset = vaddr_in - (vaddr_in & TARGET_PAGE_MASK);
int csize = TARGET_PAGE_SIZE - in_offset;

if (TARGET_PAGE_BITS < 10 || TARGET_PAGE_BITS > 14) {
return 0x3000;
}

in_mem = get_page(env, vaddr_in, MMU_DATA_LOAD, mmu_idx);
out_mem = get_page(env, vaddr_out, MMU_DATA_STORE, mmu_idx);
in_mem = get_page_read(env, vaddr_in, mmu_idx);
out_mem = get_page_write(env, vaddr_out, mmu_idx);
if (in_mem && out_mem) {
if (WKdm_decompress(in_mem, out_mem, TARGET_PAGE_SIZE)) {
memcpy(scratch, in_mem + in_offset, csize);
if (WKdm_decompress(scratch, scratch2, csize)) {
memcpy(out_mem, scratch2, TARGET_PAGE_SIZE);
return 0;
}
}
Expand Down

0 comments on commit ff04a57

Please sign in to comment.