int target_mprotect(abi_ulong start, abi_ulong len, int prot)

{

    abi_ulong end, host_start, host_end, addr;

    int prot1, ret;



#ifdef DEBUG_MMAP

    printf("mprotect: start=0x" TARGET_FMT_lx

           "len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len,

           prot & PROT_READ ? 'r' : '-',

           prot & PROT_WRITE ? 'w' : '-',

           prot & PROT_EXEC ? 'x' : '-');

#endif



    if ((start & ~TARGET_PAGE_MASK) != 0)

        return -EINVAL;

    len = TARGET_PAGE_ALIGN(len);

    end = start + len;

    if (end < start)

        return -EINVAL;

    if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))

        return -EINVAL;

    if (len == 0)

        return 0;



    host_start = start & qemu_host_page_mask;

    host_end = HOST_PAGE_ALIGN(end);

    if (start > host_start) {

        /* handle host page containing start */

        prot1 = prot;

        for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {

            prot1 |= page_get_flags(addr);

        }

        if (host_end == host_start + qemu_host_page_size) {

            for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {

                prot1 |= page_get_flags(addr);

            }

            end = host_end;

        }

        ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);

        if (ret != 0)

            return ret;

        host_start += qemu_host_page_size;

    }

    if (end < host_end) {

        prot1 = prot;

        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {

            prot1 |= page_get_flags(addr);

        }

        ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,

                       prot1 & PAGE_BITS);

        if (ret != 0)

            return ret;

        host_end -= qemu_host_page_size;

    }



    /* handle the pages in the middle */

    if (host_start < host_end) {

        ret = mprotect(g2h(host_start), host_end - host_start, prot);

        if (ret != 0)

            return ret;

    }

    page_set_flags(start, start + len, prot | PAGE_VALID);

    return 0;

}

int target_mprotect(abi_ulong start, abi_ulong len, int prot)

{

    abi_ulong end, host_start, host_end, addr;

    int prot1, ret;



#ifdef DEBUG_MMAP

    printf("mprotect: start=0x" TARGET_FMT_lx

           "len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len,

           prot & PROT_READ ? 'r' : '-',

           prot & PROT_WRITE ? 'w' : '-',

           prot & PROT_EXEC ? 'x' : '-');

#endif



    if ((start & ~TARGET_PAGE_MASK) != 0)

        return -EINVAL;

    len = TARGET_PAGE_ALIGN(len);

    end = start + len;

    if (end < start)

        return -EINVAL;

    if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))

        return -EINVAL;

    if (len == 0)

        return 0;



    host_start = start & qemu_host_page_mask;

    host_end = HOST_PAGE_ALIGN(end);

    if (start > host_start) {

        

        prot1 = prot;

        for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {

            prot1 |= page_get_flags(addr);

        }

        if (host_end == host_start + qemu_host_page_size) {

            for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {

                prot1 |= page_get_flags(addr);

            }

            end = host_end;

        }

        ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);

        if (ret != 0)

            return ret;

        host_start += qemu_host_page_size;

    }

    if (end < host_end) {

        prot1 = prot;

        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {

            prot1 |= page_get_flags(addr);

        }

        ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,

                       prot1 & PAGE_BITS);

        if (ret != 0)

            return ret;

        host_end -= qemu_host_page_size;

    }



    

    if (host_start < host_end) {

        ret = mprotect(g2h(host_start), host_end - host_start, prot);

        if (ret != 0)

            return ret;

    }

    page_set_flags(start, start + len, prot | PAGE_VALID);

    return 0;

}

int FUNC_0(abi_ulong VAR_0, abi_ulong VAR_1, int VAR_2)

{

    abi_ulong end, host_start, host_end, addr;

    int VAR_3, VAR_4;



#ifdef DEBUG_MMAP

    printf("mprotect: VAR_0=0x" TARGET_FMT_lx

           "VAR_1=0x" TARGET_FMT_lx " VAR_2=%c%c%c\n", VAR_0, VAR_1,

           VAR_2 & PROT_READ ? 'r' : '-',

           VAR_2 & PROT_WRITE ? 'w' : '-',

           VAR_2 & PROT_EXEC ? 'x' : '-');

#endif



    if ((VAR_0 & ~TARGET_PAGE_MASK) != 0)

        return -EINVAL;

    VAR_1 = TARGET_PAGE_ALIGN(VAR_1);

    end = VAR_0 + VAR_1;

    if (end < VAR_0)

        return -EINVAL;

    if (VAR_2 & ~(PROT_READ | PROT_WRITE | PROT_EXEC))

        return -EINVAL;

    if (VAR_1 == 0)

        return 0;



    host_start = VAR_0 & qemu_host_page_mask;

    host_end = HOST_PAGE_ALIGN(end);

    if (VAR_0 > host_start) {

        

        VAR_3 = VAR_2;

        for(addr = host_start; addr < VAR_0; addr += TARGET_PAGE_SIZE) {

            VAR_3 |= page_get_flags(addr);

        }

        if (host_end == host_start + qemu_host_page_size) {

            for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {

                VAR_3 |= page_get_flags(addr);

            }

            end = host_end;

        }

        VAR_4 = mprotect(g2h(host_start), qemu_host_page_size, VAR_3 & PAGE_BITS);

        if (VAR_4 != 0)

            return VAR_4;

        host_start += qemu_host_page_size;

    }

    if (end < host_end) {

        VAR_3 = VAR_2;

        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {

            VAR_3 |= page_get_flags(addr);

        }

        VAR_4 = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,

                       VAR_3 & PAGE_BITS);

        if (VAR_4 != 0)

            return VAR_4;

        host_end -= qemu_host_page_size;

    }



    

    if (host_start < host_end) {

        VAR_4 = mprotect(g2h(host_start), host_end - host_start, VAR_2);

        if (VAR_4 != 0)

            return VAR_4;

    }

    page_set_flags(VAR_0, VAR_0 + VAR_1, VAR_2 | PAGE_VALID);

    return 0;

}

int FUNC_0(abi_ulong VAR_0, abi_ulong VAR_1, int VAR_2)
{ abi_ulong end, host_start, host_end, addr; int VAR_3, VAR_4; #ifdef DEBUG_MMAP
printf("mprotect: VAR_0=0x" TARGET_FMT_lx
"VAR_1=0x" TARGET_FMT_lx " VAR_2=%c%c%c\n", VAR_0, VAR_1,
VAR_2 & PROT_READ ? 'r' : '-',
VAR_2 & PROT_WRITE ? 'w' : '-',
VAR_2 & PROT_EXEC ? 'x' : '-'); #endif
if ((VAR_0 & ~TARGET_PAGE_MASK) != 0)
return -EINVAL; VAR_1 = TARGET_PAGE_ALIGN(VAR_1); end = VAR_0 + VAR_1; if (end < VAR_0)
return -EINVAL; if (VAR_2 & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
return -EINVAL; if (VAR_1 == 0)
return 0; host_start = VAR_0 & qemu_host_page_mask; host_end = HOST_PAGE_ALIGN(end); if (VAR_0 > host_start) { VAR_3 = VAR_2; for(addr = host_start; addr < VAR_0; addr += TARGET_PAGE_SIZE) { VAR_3 |= page_get_flags(addr); } if (host_end == host_start + qemu_host_page_size) { for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { VAR_3 |= page_get_flags(addr); } end = host_end; } VAR_4 = mprotect(g2h(host_start), qemu_host_page_size, VAR_3 & PAGE_BITS); if (VAR_4 != 0)
return VAR_4; host_start += qemu_host_page_size; } if (end < host_end) { VAR_3 = VAR_2; for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { VAR_3 |= page_get_flags(addr); } VAR_4 = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
VAR_3 & PAGE_BITS); if (VAR_4 != 0)
return VAR_4; host_end -= qemu_host_page_size; } if (host_start < host_end) { VAR_4 = mprotect(g2h(host_start), host_end - host_start, VAR_2); if (VAR_4 != 0)
return VAR_4; } page_set_flags(VAR_0, VAR_0 + VAR_1, VAR_2 | PAGE_VALID); return 0; }