Home | History | Annotate | Download | only in s390x
      1 #include <stdint.h>
      2 #include <inttypes.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 #include <stdio.h>
      6 #include <assert.h>
      7 #include "opcodes.h"
      8 
      9 /* The abstracted result of an CU41 insn */
     10 typedef struct {
     11    uint64_t addr1;  // target
     12    uint64_t len1;
     13    uint64_t addr2;  // source
     14    uint64_t len2;
     15    uint32_t cc;
     16 } cu41_t;
     17 
     18 /* Define various input buffers. */
     19 
     20 /* 0000 to 00ff:  Result is 1 byte for each uint32_t */
     21 uint32_t pattern1[] = {
     22    0x0000, 0x007f,    /* corner cases */
     23    0x0001, 0x007e, 0x0030, 0x005e /* misc */
     24 };
     25 
     26 /* 0080 to 07ff: Result is 2 bytes for each uint32_t */
     27 uint32_t pattern2[] = {
     28    0x0080, 0x07ff,    /* corner cases */
     29    0x0081, 0x07fe, 0x100, 0x333, 0x555, 0x6aa  /* misc */
     30 };
     31 
     32 /* 0800 to d7ff: Result is 3 bytes for each uint32_t */
     33 /* dc00 to ffff: Result is 3 bytes for each uint32_t */
     34 uint32_t pattern3[] = {
     35    0x0800, 0xd7ff,    /* corner cases */
     36    0xdc00, 0xffff,    /* corner cases */
     37    0xdc01, 0xfffe, 0xdea0, 0xd00d, 0xe555  /* misc */
     38 };
     39 
     40 /* 10000 to 10ffff: Result is 4 bytes for each uint32_t */
     41 uint32_t pattern4[] = {
     42    0x10000, 0x10ffff,    /* corner cases */
     43    0x10001, 0x10fffe, 0x12345, 0x23456, 0xfedcb  /* misc */
     44 };
     45 
     46 /* Invalid UTF-32 character */
     47 uint32_t invalid[] = {
     48    0x0000d800, 0x0000dbff,   /* corner cases */
     49    0x00110000, 0xffffffff,   /* corner cases */
     50    0x0000daad, 0x0000d901, 0x0000d8ff, /* misc */
     51    0x00110011, 0x01000000, 0x10000000, 0xdeadbeef  /* misc */
     52 };
     53 
     54 /* Mixed bytes */
     55 uint32_t mixed[] = {
     56    0x00000078 /* 1 byte  */,
     57    0x00000111 /* 2 bytes */,
     58    0x00001234 /* 3 bytes */,
     59    0x00040404 /* 4 bytes */,
     60 };
     61 
     62 /* This is the buffer for the converted bytes. */
     63 uint8_t buff[1000];  /* Large so we con'don't have to worry about it */
     64 
     65 void write_and_check(uint32_t *, unsigned, unsigned);
     66 
     67 
     68 static cu41_t
     69 do_cu41(uint8_t *dst, uint64_t dst_len, uint32_t *src, uint64_t src_len)
     70 {
     71    int cc = 42;
     72    cu41_t regs;
     73 
     74    /* build up the register pairs */
     75    register uint32_t *source     asm("4") = src;
     76    register uint64_t  source_len asm("5") = src_len;
     77    register uint8_t  *dest       asm("2") = dst;
     78    register uint64_t  dest_len   asm("3") = dst_len;
     79 
     80    asm volatile(
     81                 CU41(2,4)
     82                 "ipm %2\n\t"
     83                 "srl %2,28\n\t"
     84                 : "+d"(dest), "+d"(source), "=d"(cc),
     85                   "+d"(source_len), "+d"(dest_len)
     86                 :
     87                 : "memory", "cc");
     88 
     89    /* Capture register contents at end of cu41 */
     90    regs.addr1 = (uint64_t)dest;
     91    regs.len1  = dest_len;
     92    regs.addr2 = (uint64_t)source;
     93    regs.len2  = source_len;
     94    regs.cc = cc;
     95 
     96    return regs;
     97 }
     98 
     99 void
    100 run_test(uint8_t *dst, uint64_t dst_len, uint32_t *src, uint64_t src_len)
    101 {
    102    int i;
    103    cu41_t result;
    104 
    105    result = do_cu41(dst, dst_len, src, src_len);
    106 
    107    // Write out the converted values, if any
    108    printf("UTF8: ");
    109    if (dst_len - result.len1 == 0)
    110       printf(" <none>");
    111    else
    112       for (i = 0; i < dst_len - result.len1; ++i) {
    113          printf(" %02x", dst[i]);
    114       }
    115    printf("\n");
    116 
    117    printf("  cc = %d\n", result.cc);
    118    if (dst != NULL)
    119       printf("  dst address difference: %"PRId64, result.addr1 - (uint64_t)dst);
    120    printf("  dst len: %"PRId64"\n", result.len1);
    121 
    122    if (src != NULL)
    123       printf("  src address difference: %"PRId64, result.addr2 - (uint64_t)src);
    124    printf("  src len: %"PRId64"\n", result.len2);
    125 }
    126 
    127 int main()
    128 {
    129    int i;
    130 
    131    /* Length == 0, no memory should be read or written */
    132    printf("\n------------- test1 ----------------\n");
    133    run_test(NULL, 0, NULL, 0);
    134 
    135    /* Test exhaustion of source length (source bytes are valid) */
    136    printf("\n------------- test2.1 ----------------\n");
    137 
    138    /* No character will be written to BUFF, i.e. loop in jitted code
    139       is not iterated */
    140    run_test(buff, sizeof buff, NULL,     0);
    141    run_test(buff, sizeof buff, NULL,     1);
    142    run_test(buff, sizeof buff, NULL,     2);
    143    run_test(buff, sizeof buff, NULL,     3);
    144    run_test(buff, sizeof buff, pattern1, 0);
    145    run_test(buff, sizeof buff, pattern1, 1);
    146    run_test(buff, sizeof buff, pattern1, 2);
    147    run_test(buff, sizeof buff, pattern1, 3);
    148 
    149    printf("\n------------- test2.2 ----------------\n");
    150    /* At least one character will be written to BUFF, i.e. loop in jitted
    151       code is iterated */
    152    run_test(buff, sizeof buff, pattern1, 4);  /* 1 utf32 -> 1 1-byte utf8 */
    153    run_test(buff, sizeof buff, pattern2, 10); /* 2 utf32 -> 2 2-byte utf8 */
    154    run_test(buff, sizeof buff, pattern3, 5);  /* 1 utf32 -> 1 3-byte utf8 */
    155    run_test(buff, sizeof buff, pattern4, 21); /* 5 utf32 -> 5 4-byte utf8 */
    156 
    157    /* Test exhaustion of destination length (source bytes are valid) */
    158    printf("\n------------- test3.1 ----------------\n");
    159 
    160    /* No character will be written to BUFF, i.e. loop in jitted code
    161       is not iterated */
    162 
    163    /* Want to write at least 1 byte */
    164    run_test(NULL, 0, pattern1, sizeof pattern1);
    165 
    166    /* Want to write at least 2 bytes */
    167    run_test(NULL, 0, pattern2, sizeof pattern2);
    168    run_test(NULL, 1, pattern2, sizeof pattern2);
    169 
    170    /* Want to write at least 3 bytes */
    171    run_test(NULL, 0, pattern3, sizeof pattern3);
    172    run_test(NULL, 1, pattern3, sizeof pattern3);
    173 
    174    /* Want to write at least 4 bytes */
    175    run_test(NULL, 0, pattern4, sizeof pattern4);
    176    run_test(NULL, 1, pattern4, sizeof pattern4);
    177    run_test(NULL, 2, pattern4, sizeof pattern4);
    178    run_test(NULL, 3, pattern4, sizeof pattern4);
    179 
    180    /* When both operands are exhausted, cc=0 takes precedence.
    181       (test1 tests this for len == 0) */
    182    printf("\n------------- test4 ----------------\n");
    183    run_test(buff, 2, pattern1, 8);
    184 
    185    /* Input contains invalid characters */
    186 
    187    // As conversion stops upon encountering an invalid character, we
    188    // need to test each invalid character separately, to make sure it
    189    // is recognized as invalid.
    190 
    191    printf("\n------------- test5 ----------------\n");
    192    for (i = 0; i < sizeof invalid / 4; ++i) {
    193       run_test(buff, sizeof buff, invalid + i, 4);
    194    }
    195    run_test(buff, 0, invalid, sizeof invalid);  // cc = 2
    196    run_test(buff, 100, invalid, sizeof invalid);
    197 
    198    /* Convert all pattern buffers */
    199    printf("\n------------- test6 ----------------\n");
    200    run_test(buff, sizeof buff, pattern1, sizeof pattern1);
    201    run_test(buff, sizeof buff, pattern2, sizeof pattern2);
    202    run_test(buff, sizeof buff, pattern3, sizeof pattern3);
    203    run_test(buff, sizeof buff, pattern4, sizeof pattern4);
    204    run_test(buff, sizeof buff, mixed,    sizeof mixed);
    205 
    206    /* Make sure we only write the exact number of bytes (and not more) */
    207 
    208    /* Write 1 byte */
    209    printf("\n------------- test7.0 ----------------\n");
    210    write_and_check(pattern1 + 2, 4, 1);
    211 
    212    /* Write 2 bytes */
    213    printf("\n------------- test7.1 ----------------\n");
    214    write_and_check(pattern2 + 3, 4, 2);
    215 
    216    /* Write 3 bytes */
    217    printf("\n------------- test7.2 ----------------\n");
    218    write_and_check(pattern3 + 6, 4, 3);
    219 
    220    /* Write 4 bytes */
    221    printf("\n------------- test7.3 ----------------\n");
    222    write_and_check(pattern4 + 5, 4, 4);
    223 
    224    return 0;
    225 }
    226 
    227 
    228 void
    229 write_and_check_aux(uint32_t *input, unsigned num_input_bytes,
    230                     unsigned num_expected_output_bytes,
    231                     unsigned fill_byte)
    232 {
    233    int num_errors, i;
    234 
    235    /* Fill output buffer with FILL_BYTE */
    236    memset(buff, fill_byte, sizeof buff);
    237 
    238    /* Execute cu41 */
    239    run_test(buff, sizeof buff, input, num_input_bytes);
    240 
    241    /* Make sure the rest of the buffer is unmodified.  */
    242    num_errors = 0;
    243    for (i = num_expected_output_bytes; i < sizeof buff; ++i)
    244       if (((unsigned char *)buff)[i] != fill_byte) ++num_errors;
    245    if (num_errors)
    246       fprintf(stderr, "*** wrote more than %d bytes\n",
    247               num_expected_output_bytes);
    248 }
    249 
    250 void
    251 write_and_check(uint32_t *input, unsigned num_input_bytes,
    252                 unsigned num_expected_output_bytes)
    253 {
    254    write_and_check_aux(input, num_input_bytes, num_expected_output_bytes, 0x0);
    255 
    256    /* Run again with different fill pattern to make sure we did not write
    257       an extra 0x0 byte */
    258    write_and_check_aux(input, num_input_bytes, num_expected_output_bytes, 0xFF);
    259 }
    260