This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

在cache操作结束后调用_mfence是为什么



请教:

        在keystone程序中的writebackcache和invalidcache结尾处都有这样的操作:

        _mfence();

        _mfence();

       好像去掉也没有什么问题。它们的功能是什么?有存在的必要性吗?

  • 楼主你好,对于你提到的这个指令,我在文档中看到如下解释:

    The MFENCE instruction is used to stall the instruction fetch pipeline until the
    completion of all CPU-triggered memory transactions.

    For example,
    1. STORE_A
    2. MFENCE
    3. TRANSACTION_B
    The MFENCE implementation stalls the CPU until the memory system asserts that
    there are no transactions "in flight," i.e. it is idle. This prevents the CPU from
    proceeding to TRANSACTION_B before STORE_A completes

    估计wb和inv两个函数是cache硬件进行的,cpu此时需要等待,所以加上这两条语句,防止cpu在cache一致性维护好之前就进行下一步操作,但是并不是一定会出现这样的特殊情况,所以这条语句有些时候并没有体现其作用,但是为了保险起见这条语句是必要的

  • mfence  指令确保前一条对内存操作的指令完成后才继续向下执行,在这里保证Cache操作完成后才执行以后的指令,防止错误的操作