NVlabs / NVBit

199 stars 18 forks source link

nvbit_add_call_arg* functions #53

Closed mahmoodn closed 2 years ago

mahmoodn commented 2 years ago

Hi I have difficulty in understanding the following piece of code

            for (int i = 0; i < instr->getNumOperands(); i++) {
                /* get the operand "i" */
                const InstrType::operand_t* op = instr->getOperand(i);

                if (op->type == InstrType::OperandType::MREF) {
                    /* insert call to the instrumentation function with its
                     * arguments */
                    nvbit_insert_call(instr, "instrument_mem", IPOINT_BEFORE);
                    /* predicate value */
                    nvbit_add_call_arg_guard_pred_val(instr);
                    /* opcode id */
                    nvbit_add_call_arg_const_val32(instr, opcode_id);
                    /* memory reference 64 bit address */
                    nvbit_add_call_arg_mref_addr64(instr, mref_idx);
                    /* add "space" for kernel function pointer that will be set
                     * at launch time (64 bit value at offset 0 of the dynamic
                     * arguments)*/
                    nvbit_add_call_arg_launch_val64(instr, 0);
                    /* add pointer to channel_dev*/
                    nvbit_add_call_arg_const_val64(
                        instr, (uint64_t)ctx_state->channel_dev);
                    mref_idx++;
                }
            }

For LDGSTS, there are two MREF operands, so the loop iterates twice. Why opcode_id is pushed two times then? Also, what happen after each nvbit_add_call* function?

BTW, With respect to the async-copy logic, I expect that "LDGSTS [R1], [R2]" means sh_mem[R1] <- gl_mem[R2]. Is that correct? Are there more operands for async-copy?

ovilla commented 2 years ago

Sorry for not answering this earlier.

The reason the nvbit_add_call is added multiple times is that in this example we are potentially instrumenting the same instruction multiple times, one for each memory reference.

The only instruction that currently support more than one memory reference is LDGSTS like you mentioned, which will be instrumented twice. All the other memory instructions are only instrumented once.

Closing this issue, since it is not an nvbit issue "per-se" but rather a request for explanation of how a specific tool works.