From 07e62b1e71167e28b045928fb0246771e3588b2b Mon Sep 17 00:00:00 2001 From: Ved Shanbhogue Date: Fri, 10 Nov 2023 06:54:25 -0800 Subject: [PATCH 1/2] fix return values --- iommu_ref_model/libtables/src/build_ddt.c | 6 +++--- iommu_ref_model/libtables/src/build_g_stage_pt.c | 8 ++++---- iommu_ref_model/libtables/src/build_pdt.c | 12 ++++++------ iommu_ref_model/libtables/src/build_s_stage_pt.c | 8 ++++---- iommu_ref_model/libtables/src/build_vs_stage_pt.c | 12 ++++++------ iommu_ref_model/libtables/src/translate_gpa.c | 8 ++++---- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/iommu_ref_model/libtables/src/build_ddt.c b/iommu_ref_model/libtables/src/build_ddt.c index 18bdd20d..d4d8b292 100644 --- a/iommu_ref_model/libtables/src/build_ddt.c +++ b/iommu_ref_model/libtables/src/build_ddt.c @@ -34,15 +34,15 @@ add_dev_context( i = LEVELS - 1; while ( i > 0 ) { ddte.raw = 0; - read_memory((a + (DDI[i] * 8)), 8, (char *)&ddte.raw); + if ( read_memory((a + (DDI[i] * 8)), 8, (char *)&ddte.raw) ) return -1; if ( ddte.V == 0 ) { ddte.V = 1; ddte.PPN = get_free_ppn(1); - write_memory((char *)&ddte.raw, (a + (DDI[i] * 8)), 8); + if ( write_memory((char *)&ddte.raw, (a + (DDI[i] * 8)), 8) ) return -1; } i = i - 1; a = ddte.PPN * PAGESIZE; } - write_memory((char *)DC, (a + (DDI[0] * DC_SIZE)), DC_SIZE); + if ( write_memory((char *)DC, (a + (DDI[0] * DC_SIZE)), DC_SIZE) ) return -1; return (a + (DDI[0] * DC_SIZE)); } diff --git a/iommu_ref_model/libtables/src/build_g_stage_pt.c b/iommu_ref_model/libtables/src/build_g_stage_pt.c index 71111fbe..a9936949 100644 --- a/iommu_ref_model/libtables/src/build_g_stage_pt.c +++ b/iommu_ref_model/libtables/src/build_g_stage_pt.c @@ -45,16 +45,16 @@ add_g_stage_pte ( a = iohgatp.PPN * PAGESIZE; while ( i > add_level ) { nl_gpte.raw = 0; - read_memory((a | (vpn[i] * PTESIZE)), PTESIZE, (char *)&nl_gpte.raw); + if ( read_memory((a | (vpn[i] * PTESIZE)), PTESIZE, (char *)&nl_gpte.raw) ) return -1; if ( nl_gpte.V == 0 ) { nl_gpte.V = 1; nl_gpte.PPN = get_free_ppn(1); - write_memory((char *)&nl_gpte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE); + if ( write_memory((char *)&nl_gpte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE) ) return -1; } i = i - 1; - if ( i < 0 ) return 1; + if ( i < 0 ) return -1; a = nl_gpte.PPN * PAGESIZE; } - write_memory((char *)&gpte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE); + if ( write_memory((char *)&gpte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE) ) return -1; return (a | (vpn[i] * PTESIZE)); } diff --git a/iommu_ref_model/libtables/src/build_pdt.c b/iommu_ref_model/libtables/src/build_pdt.c index f680be11..3afe87fd 100644 --- a/iommu_ref_model/libtables/src/build_pdt.c +++ b/iommu_ref_model/libtables/src/build_pdt.c @@ -24,9 +24,9 @@ add_process_context( a = DC->fsc.pdtp.PPN * PAGESIZE; i = LEVELS - 1; while ( i > 0 ) { - if ( translate_gpa(DC->iohgatp, a, &a) == -1 ) return 1; + if ( translate_gpa(DC->iohgatp, a, &a) == -1 ) return -1; pdte.raw = 0; - read_memory((a + (PDI[i] * 8)), 8, (char *)&pdte.raw); + if ( read_memory((a + (PDI[i] * 8)), 8, (char *)&pdte.raw) ) return -1; if ( pdte.V == 0 ) { pdte.V = 1; pdte.reserved0 = pdte.reserved1 = 0; @@ -47,16 +47,16 @@ add_process_context( gpte.PBMT = PMA; gpte.PPN = get_free_ppn(1); - add_g_stage_pte(DC->iohgatp, (PAGESIZE * pdte.PPN), gpte, 0); + if ( add_g_stage_pte(DC->iohgatp, (PAGESIZE * pdte.PPN), gpte, 0) == -1 ) return -1; } else { pdte.PPN = get_free_ppn(1); } - write_memory((char *)&pdte.raw, (a + (PDI[i] * 8)), 8); + if ( write_memory((char *)&pdte.raw, (a + (PDI[i] * 8)), 8) ) return -1; } i = i - 1; a = pdte.PPN * PAGESIZE; } - if ( translate_gpa(DC->iohgatp, a, &a) == -1 ) return 1; - write_memory((char *)PC, (a + (PDI[0] * 16)), 16); + if ( translate_gpa(DC->iohgatp, a, &a) == -1 ) return -1; + if ( write_memory((char *)PC, (a + (PDI[0] * 16)), 16) ) return -1; return (a + (PDI[0] * 16)); } diff --git a/iommu_ref_model/libtables/src/build_s_stage_pt.c b/iommu_ref_model/libtables/src/build_s_stage_pt.c index 86b40b85..2f30b7af 100644 --- a/iommu_ref_model/libtables/src/build_s_stage_pt.c +++ b/iommu_ref_model/libtables/src/build_s_stage_pt.c @@ -45,16 +45,16 @@ add_s_stage_pte ( a = satp.PPN * PAGESIZE; while ( i > add_level ) { nl_pte.raw = 0; - read_memory((a | (vpn[i] * PTESIZE)), PTESIZE, (char *)&nl_pte.raw); + if ( read_memory((a | (vpn[i] * PTESIZE)), PTESIZE, (char *)&nl_pte.raw)) return -1; if ( nl_pte.V == 0 ) { nl_pte.V = 1; nl_pte.PPN = get_free_ppn(1); - write_memory((char *)&nl_pte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE); + if ( write_memory((char *)&nl_pte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE) ) return -1; } i = i - 1; - if ( i < 0 ) return 1; + if ( i < 0 ) return -1; a = nl_pte.PPN * PAGESIZE; } - write_memory((char *)&pte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE); + if ( write_memory((char *)&pte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE) ) return -1; return (a | (vpn[i] * PTESIZE)); } diff --git a/iommu_ref_model/libtables/src/build_vs_stage_pt.c b/iommu_ref_model/libtables/src/build_vs_stage_pt.c index 1582750b..361b8042 100644 --- a/iommu_ref_model/libtables/src/build_vs_stage_pt.c +++ b/iommu_ref_model/libtables/src/build_vs_stage_pt.c @@ -45,9 +45,9 @@ add_vs_stage_pte ( i = LEVELS - 1; a = satp.PPN * PAGESIZE; while ( i > add_level ) { - if ( translate_gpa(iohgatp, a, &a) == -1) return 1; + if ( translate_gpa(iohgatp, a, &a) == -1) return -1; nl_pte.raw = 0; - read_memory((a | (vpn[i] * PTESIZE)), PTESIZE, (char *)&nl_pte.raw); + if ( read_memory((a | (vpn[i] * PTESIZE)), PTESIZE, (char *)&nl_pte.raw)) return -1; if ( nl_pte.V == 0 ) { gpte_t gpte; @@ -66,15 +66,15 @@ add_vs_stage_pte ( gpte.PBMT = PMA; gpte.PPN = get_free_ppn(1); - add_g_stage_pte(iohgatp, (PAGESIZE * nl_pte.PPN), gpte, 0); + if ( add_g_stage_pte(iohgatp, (PAGESIZE * nl_pte.PPN), gpte, 0) == -1) return -1; - write_memory((char *)&nl_pte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE); + if ( write_memory((char *)&nl_pte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE) ) return -1; } i = i - 1; if ( i < 0 ) return 1; a = nl_pte.PPN * PAGESIZE; } - if ( translate_gpa(iohgatp, a, &a) == -1) return 1; - write_memory((char *)&pte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE); + if ( translate_gpa(iohgatp, a, &a) == -1) return -1; + if ( write_memory((char *)&pte.raw, (a | (vpn[i] * PTESIZE)), PTESIZE) ) return -1; return (a | (vpn[i] * PTESIZE)); } diff --git a/iommu_ref_model/libtables/src/translate_gpa.c b/iommu_ref_model/libtables/src/translate_gpa.c index b7fc8912..9c765c60 100644 --- a/iommu_ref_model/libtables/src/translate_gpa.c +++ b/iommu_ref_model/libtables/src/translate_gpa.c @@ -16,7 +16,7 @@ translate_gpa ( PTESIZE = 8; if ( iohgatp.MODE == IOHGATP_Bare ) { *spa = gpa; - return 0; + return -1; } if ( iohgatp.MODE == IOHGATP_Sv32x4 ) { vpn[0] = get_bits(21, 12, gpa); @@ -53,8 +53,8 @@ translate_gpa ( a = iohgatp.PPN * PAGESIZE; while ( 1 ) { nl_gpte.raw = 0; - read_memory((a | (vpn[i] * PTESIZE)), PTESIZE, (char *)&nl_gpte.raw); - if ( nl_gpte.V == 0 ) return 1; + if ( read_memory((a | (vpn[i] * PTESIZE)), PTESIZE, (char *)&nl_gpte.raw) ) return -1; + if ( nl_gpte.V == 0 ) return -1; if ( nl_gpte.R != 0 || nl_gpte.X != 0 ) { *spa = nl_gpte.PPN; *spa = *spa * PAGESIZE; @@ -63,7 +63,7 @@ translate_gpa ( return (a | (vpn[i] * PTESIZE)); } i = i - 1; - if ( i < 0 ) return -11; + if ( i < 0 ) return -1; gst_page_sz = ( i == 0 ) ? PAGESIZE : (gst_page_sz / 512); a = nl_gpte.PPN * PAGESIZE; } From 15f41b074a344d8a851a37e28d3d4aa66e350ad9 Mon Sep 17 00:00:00 2001 From: Ved Shanbhogue Date: Fri, 10 Nov 2023 06:54:36 -0800 Subject: [PATCH 2/2] check return values --- iommu_ref_model/test/test_app.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/iommu_ref_model/test/test_app.c b/iommu_ref_model/test/test_app.c index 4a2e7111..f98be1e8 100644 --- a/iommu_ref_model/test/test_app.c +++ b/iommu_ref_model/test/test_app.c @@ -2079,7 +2079,7 @@ main(void) { fail_if( ( check_rsp_and_faults(&req, &rsp, UNSUPPORTED_REQUEST, 266, 0) < 0 ) ); // Access viol on non-leaf PDTE - translate_gpa(DC.iohgatp, DC.fsc.pdtp.PPN * PAGESIZE, &temp); + fail_if( (translate_gpa(DC.iohgatp, DC.fsc.pdtp.PPN * PAGESIZE, &temp) == -1) ); access_viol_addr = (temp) | (get_bits(19, 17, 0xBABEC) * 8); send_translation_request(0x112233, 1, 0xBABEC, 0, 0, 1, 0, ADDR_TYPE_UNTRANSLATED, 0xdeadbeef, @@ -2117,10 +2117,10 @@ main(void) { PC.ta.ENS = 1; PC.ta.SUM = 1; PC_addr = add_process_context(&DC, &PC, 0xBABEC); - read_memory(PC_addr, 16, (char *)&PC); + fail_if( (read_memory(PC_addr, 16, (char *)&PC) != 0) ); // misconfigured NL PTE - translate_gpa(DC.iohgatp, DC.fsc.pdtp.PPN * PAGESIZE, &temp); + fail_if( (translate_gpa(DC.iohgatp, DC.fsc.pdtp.PPN * PAGESIZE, &temp) == -1) ); temp = (temp) | (get_bits(19, 17, 0xBABEC) * 8); read_memory(temp, 8, (char *)&pdte); pdte.reserved0 = 1; @@ -2207,6 +2207,7 @@ main(void) { // guest page fault on PC walk gpa = (DC.fsc.pdtp.PPN * PAGESIZE) | (get_bits(19, 17, 0xBABEC) * 8); temp = translate_gpa(DC.iohgatp, gpa, &temp); + fail_if( (temp == -1) ); read_memory(temp, 8, (char *)&gpte); gpte.V = 0; write_memory((char *)&gpte, temp, 8); @@ -2476,6 +2477,7 @@ main(void) { // Guest-Page fault on NL S-stage PTE gpte_addr = translate_gpa(DC.iohgatp, (PC.fsc.iosatp.PPN * PAGESIZE), &temp); + fail_if( (gpte_addr == -1) ); read_memory(gpte_addr, 8, (char *)&gpte); gpte.V = 0; write_memory((char *)&gpte.raw, gpte_addr, 8);