Device and method for controlling branch prediction

By switching to an alternative branch prediction mechanism in the branch prediction circuit and using confidence indications to detect mispredictions, we address branch target injection attacks and mispredictions caused by nested code sections, restoring performance and improving branch prediction accuracy.

CN111886580BActive Publication Date: 2024-09-20ARM LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN201980020659.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Priority Date
2018-03-29
Filing Date
2019-02-18
Publication Date
2024-09-20
Estimated Expiration
2039-02-18

AI Technical Summary

Technical Problem

When existing branch prediction circuits face branch target injection attacks, although the use of Retpoline software construction can alleviate mispredictions, it results in performance loss, and there may be legacy code with performance impact in the future, and the return prediction mechanism may lead to mispredictions, affecting accuracy.

Method used

In the branch prediction circuit, when a return instruction is detected, the return prediction mechanism is used by default, but when a misprediction occurs, it switches to an alternative branch prediction mechanism to improve prediction accuracy and detect trigger conditions through confidence indication and prediction control information. Dynamically adjust prediction mechanisms to restore performance.

Benefits of technology

Effectively reduces branch prediction mispredictions, restores performance, especially avoids performance impact when facing branch target injection attacks, and switches to a replacement branch prediction mechanism in the case of overflow caused by nested code parts, improving branch prediction accuracy and efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111886580B_ABST
    Figure CN111886580B_ABST
Patent Text Reader

Abstract

An apparatus and method for controlling branch prediction are provided. The apparatus has a processing circuit for executing instructions, and a branch prediction circuit, which includes a plurality of branch prediction mechanisms for predicting target addresses for branch instructions to be executed by the processing circuit. The branch instruction includes a plurality of branch types, one of which is a return instruction. The branch prediction mechanism includes a return prediction mechanism, and when the branch prediction circuit detects a return instruction, the return prediction mechanism is used by default to predict the target address. However, the branch prediction circuit switches to using a replacement branch prediction mechanism to predict the target address for a given return instruction in response to a trigger condition indicating a misprediction of the target address when the return prediction mechanism is used to predict the target address for a given return instruction. It has been found that this improves performance in some cases.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present technology relates to apparatus and methods for controlling branch prediction. Background Art

[0002] The data processing apparatus may have a branch prediction circuit for predicting the outcome of a branch instruction before the branch instruction is actually executed. By predicting the outcome of a branch before the branch instruction is actually executed, subsequent instructions following the branch may begin to be fetched and speculatively executed before execution of the branch instruction is completed, thereby saving execution if the prediction is correct because the subsequent instructions may be executed earlier than if they were only fetched once the outcome of the branch is actually known.

[0003] In order to obtain optimal performance benefits from the use of branch prediction, it is desirable for the predictions made by the branch prediction circuitry to be as accurate as possible, so as to reduce the number of mispredictions that occur when using the branch prediction circuitry. Summary of the invention

[0004] In one example configuration, an apparatus is provided, comprising: processing circuitry for executing instructions; and branch prediction circuitry comprising a plurality of branch prediction mechanisms for predicting a target address for a branch instruction to be executed by the processing circuitry, the branch instruction comprising a plurality of branch types, one of the branch types being a return instruction; wherein the branch prediction mechanism comprises a return prediction mechanism, and when the branch prediction circuitry detects a return instruction, the return prediction mechanism is used by default to predict a target address; the branch prediction circuitry switches to using an alternative branch prediction mechanism to predict a target address for a given return instruction in response to a trigger condition, the trigger condition indicating a misprediction of the target address when using the return prediction mechanism to predict a target address for a given return instruction.

[0005] In another example configuration, a method of controlling branch prediction in an apparatus having a processing circuit for executing instructions is provided, the method comprising: providing a branch prediction circuit, the branch prediction circuit comprising a plurality of branch prediction mechanisms, the plurality of branch prediction mechanisms being used to predict a target address for a branch instruction to be executed by the processing circuit, the branch instruction comprising a plurality of branch types, one of the branch types being a return instruction; when the branch prediction circuit detects a return instruction, using a return prediction mechanism among the plurality of branch prediction mechanisms by default to predict the target address; and switching to using an alternative branch prediction mechanism among the plurality of branch prediction mechanisms to predict the target address for a given return instruction in response to a trigger condition, the trigger instruction indicating a misprediction of the target address when using the return prediction mechanism to predict the target address for the given return instruction.

[0006] In another example configuration, a device is provided, comprising: a processing device for executing instructions; and a branch prediction device, comprising multiple branch prediction mechanisms, the multiple branch prediction mechanisms being used to predict target addresses for branch instructions to be executed by the processing device, the branch instructions comprising multiple branch types, one of which is a return instruction; wherein the branch prediction mechanism comprises a return prediction mechanism, and when the branch prediction device detects a return instruction, the return prediction mechanism is used by default to predict the target address; the branch prediction device switches to using an alternative branch prediction mechanism to predict the target address for a given return instruction in response to a trigger condition, the trigger condition indicating a misprediction of the target address when using the return prediction mechanism to predict the target address for the given return instruction. BRIEF DESCRIPTION OF THE DRAWINGS

[0007] The present technology will be further described by way of illustration only with reference to examples of the present technology illustrated in the accompanying drawings, in which:

[0008] Figure 1 schematically illustrates an example of a data processing apparatus having a branch predictor;

[0009] Figure 2 is shown in more detail in an example arrangement in Figure 1 A block diagram of components that may be provided within a branch predictor of;

[0010] Figure 3 is a schematic diagram showing in one example arrangement Figure 2 A map of fields that may be provided within each entry of a branch target buffer (BTB);

[0011] Figure 4 is a flow chart illustrating how, in one example, a branch prediction circuit may be used to process predictions for a return instruction;

[0012] Figure 5 is a flow chart illustrating how predictive control information may be updated in one example;

[0013] Figure 6 is a flow chart illustrating how a trigger condition may be detected in one example arrangement;

[0014] Figure 7 is a flow chart illustrating how, in one example, an alternative branch prediction mechanism may be selected once a trigger condition is determined to exist;

[0015] Fig. 8A The operation of a linked branch instruction is schematically illustrated;

[0016] Figure 8B illustrates software constructs that may be used to isolate indirect branch instructions from speculative execution;

[0017] Fig. 9 schematically illustrates the use of nested code sections; and

[0018] Fig.10 is a flow diagram illustrating how an overflow within a return stack due to the extent to which nested code sections are used may lead to a misprediction and a switch to the use of an alternate branch prediction mechanism, according to one example arrangement. DETAILED DESCRIPTION

[0019] In one example, a device is provided having a processing circuit for executing instructions, and a branch prediction circuit for predicting a target address for a branch instruction to be executed by the processing circuit. The branch prediction circuit includes a plurality of branch prediction mechanisms that can be used to predict a target address for a branch instruction. The branch instruction may include a plurality of branch types, and the branch prediction mechanism used in any particular instance may depend on the type of branch instruction for which the target address is predicted.

[0020] One type of branch instruction is a return instruction. When a call is made to a function in software code, this may be performed via a branch with link instruction. When the branch with link instruction is executed, a return address is stored, such as in a link register, which is typically the address of the instruction following the branch with link instruction. The function is then executed, and at the end of the function, a return instruction is executed. For a return instruction, the target address is the return address determined when the associated branch with link instruction is executed.

[0021] Thus, a return instruction is a specific type of branch instruction that uses as its target address the return address previously established when the associated branch with link instruction was executed. Within the branch prediction circuitry, when the branch prediction circuitry detects a return instruction, a return prediction mechanism may be used to predict the target address. Typically, this return prediction mechanism is very accurate because, for each return instruction, the contents of the return prediction mechanism may be populated when a prediction is made for the associated branch with link instruction.

[0022] However, it has been recognized that in certain instances, the return prediction mechanism may not be able to provide an accurate target address for the return instruction, resulting in a misprediction.

[0023] As a specific example, Google recently published information about speculative cache timing based side channel attacks, where several techniques can be used so that cache timing analysis can be abused to leak information from the mis-speculative execution of instructions. The basic principle behind the cache timing side channel is that the pattern of allocations into the cache, and in particular which cache sets have been used for allocations, can be determined by measuring the time it takes to access entries that were previously in the cache, or by measuring the time to access entries that have been allocated. This can then be used to determine which addresses have been allocated into the cache. The novelty of the speculative cache timing based side channel lies in its use of speculative memory reads. Speculative memory reads are a typical feature of advanced microprocessors and are part of the overall functionality that enables high performance.

[0024] By performing a first speculative memory read to a cacheable location beyond an architecturally unresolved branch (or other change in program flow), the result of that read can itself be used to form the addresses of further speculative memory reads. These speculative reads cause entries to be allocated into the cache whose addresses indicate the values ​​of the first speculative read. If the untrusted code can control speculation in such a way that it causes a first speculative read to a location that would not otherwise be accessible to the untrusted code, this can become an exploitable side channel. In particular, the effects of second and subsequent speculative allocations to the cache can be measured by the untrusted code.

[0025] One mechanism that can be used to attempt to perform this attack is called a branch target injection variant of the attack. This attack exploits branch prediction circuits and specifically attempts to train a specific branch prediction mechanism with untrusted code in order to influence branch predictions made by trusted code. In particular, the branch prediction mechanism can use the history of previous branches to speculate changes in the instruction flow. Deciding to make such a resulting speculation can take considerable time. The delay in this decision can result in the process of performing speculative memory accesses, thereby causing allocations to the cache. In some implementations, the history of previous branches used to drive the speculation is not filtered by the exception level at which the processor is located, and therefore code running at one exception level has the potential to train the branch predictor in a way that causes other exception levels (or other scenarios) to perform speculative memory accesses. This can then be used to influence the speculative instruction flow of a higher exception level by having the lower exception level train the branch predictor, or in a different scenario, read data that would otherwise be inaccessible at a lower exception level, and additionally speculatively allocate items to the cache based on that data, thereby stimulating a speculative cache timing side channel attack. Code running at a lower exception level can then examine the impact of cache allocations, thereby exploiting the cache timing side channel.

[0026] Google recently proposed a software construct known as a "Retpoline" sequence that can be used to mitigate against branch target injection techniques. In particular, the software construct can be used to cause branch mispredictions when executing indirect branch instructions by causing the mispredicted target address to be obtained from a return prediction mechanism rather than via an expected branch prediction mechanism, thereby protecting against situations where the expected branch prediction mechanism is easily exploited to implement a branch target injection attack. Thus, by using this software construct, the expected branch prediction mechanism for indirect branch instructions can be bypassed, and instead, the software construct causes the target address to be obtained from a return prediction mechanism. Due to the way this software construct is arranged, the target address obtained from the branch prediction mechanism is not the expected target address, but rather an address used to capture speculative execution in a state where no useful process is executed, and is therefore used to isolate indirect branch instructions from speculative execution.

[0027] While this protects against branch target injection attacks, it has a significant impact on performance due to the forced branch mispredictions that occur.

[0028] However, it is recognized that in the future, techniques will be developed to make the branch prediction mechanism itself robust to such branch target injection attacks. However, at that time, there may be legacy code that uses the "Retpoline" sequence which has a significant impact on performance, but which is no longer required to address the branch target injection attack vulnerability.

[0029] To address this potential problem, according to the technology described herein, when a return instruction is detected by a branch prediction circuit, the branch prediction circuit uses a return prediction mechanism to predict the target address by default. However, the branch prediction circuit responds to a trigger condition indicating a misprediction of the target address when using the return prediction mechanism to predict the target address for a given return instruction, so as to switch to using a replacement branch prediction mechanism to predict the target address. Therefore, although the return prediction mechanism is generally expected to be very accurate in predicting the target address, when a situation is detected in which the return prediction mechanism does not accurately predict the target address, the branch prediction circuit can switch to using a replacement branch prediction mechanism to predict the target address for a given return instruction. Depending on the implementation, the replacement branch prediction mechanism can be used for the current instance of a given branch instruction, and / or for at least one subsequent instance of a given branch instruction.

[0030] Thus, in accordance with the earlier example where the "Retpoline" software sequence is used to mitigate against branch target injection attacks, once the branch prediction circuitry is such that the desired branch prediction mechanism to be used for indirect branch instructions is configured to be robust against branch target injection attacks, the use of a trigger condition enables mispredictions caused by the "Retpoline" software construct to be detected, and subsequent predictions to be made using the desired branch prediction mechanism. As a result, this restores performance, and in particular avoids performance impacts that would otherwise occur due to the use of this software construct.

[0031] However, the use of the above-mentioned software construction is not the only reason that the return prediction mechanism can cause the misprediction of the target address. For example, the code that may be executed by the processing circuit includes multiple nested code parts, and each nested code part can be defined by a band link branch instruction and its associated return instruction. The existence of multiple nested code parts can be enough to cause an overflow condition in the return prediction mechanism, wherein at least one target address of a specific older pending return instruction is overwritten with the target address of the later pending return instruction. Due to the nesting effect, the older pending return instruction will not be actually executed until after the later pending return instruction. Therefore, the prediction for the later pending return instruction is consumed earlier than the prediction for the older pending return instruction. As a result, when the prediction of the target address is made to the specific older pending return instruction that the target address has been overwritten, the relevant entry will be lacking in the return prediction mechanism, and this can be used to make the trigger condition detected so that the replacement branch prediction mechanism is used to predict the target address.

[0032] It will therefore be appreciated that there are a number of different scenarios in which a misprediction may be detected in the return prediction mechanism, and use of the above described techniques enables the branch prediction circuitry to switch to using an alternative branch prediction mechanism in such circumstances.

[0033] There are several ways in which a trigger condition may be detected for a particular return instruction. In one example arrangement, the branch prediction circuitry is arranged to maintain prediction control information for one or more return instructions, the prediction control information identifying, for each return instruction, whether a trigger condition exists, thereby controlling, for each return instruction, whether the target address of the return instruction is predicted using a return prediction mechanism or an alternative branch prediction mechanism. Thus, in this arrangement, the presence or absence of a trigger condition may be detected for individual return instructions.

[0034] In one example arrangement, when the processing circuitry executes a given return instruction, the processing circuitry is arranged to determine whether the target address determined by the branch prediction circuitry is correct, and to provide feedback information to the branch prediction circuitry indicating whether the target address determined by the branch prediction circuitry is correct, the branch prediction circuitry being arranged to use the feedback information to maintain prediction control information. Thus, the prediction control information may be updated based on information received when the relevant return instruction is actually executed, enabling an assessment of how accurate the prediction made by the return prediction mechanism is.

[0035] The prediction control information may take a variety of forms, but in one arrangement includes a confidence indication indicating, for an associated return instruction, a level of misprediction observed when using a return prediction mechanism to predict a target address for the associated return instruction. In this case, a trigger condition may be deemed to exist when the misprediction level indicated by the confidence indication reaches a selected misprediction level. With this approach, the misprediction level required to trigger a switch to use an alternative branch prediction mechanism may be customized for a particular implementation.

[0036] In one example, the selected misprediction level may include a single misprediction instance confirmed by the processing circuit for an associated return instruction, so that after the single misprediction instance, the confidence indication causes the branch prediction circuit to use the replacement branch prediction mechanism to predict the target address for at least one subsequent instance of the associated return instruction. Therefore, in this arrangement, once there is a single misprediction instance about a given return instruction, the replacement branch prediction mechanism can then be used to predict the target address for one or more subsequent instances of the return instruction. As mentioned earlier, it is generally expected that the return prediction mechanism is very accurate, and indeed in many implementations, it is not expected that misprediction will occur. Therefore, even a single misprediction can be sufficient to indicate the presence of a trigger condition in this arrangement, and therefore causes a switch to use the replacement branch prediction mechanism.

[0037] However, the selected misprediction level may be configured such that, if desired, more than one instance of misprediction is required before a trigger condition is detected. In particular, in one example arrangement, the selected misprediction level may include the processing circuitry confirming the determined plurality of misprediction instances for the associated return instruction, and for each misprediction instance, the branch prediction circuitry is arranged to adjust the value of the confidence indication. As a result, after the determined plurality of misprediction instances, the confidence indication has a value indicative of a trigger condition such that the branch prediction circuitry uses an alternative branch prediction mechanism to predict a target address for at least one subsequent instance of the associated return instruction.

[0038] The manner in which the confidence indication is adjusted will vary depending on the implementation. However, in one example arrangement, the confidence indication is initialised to an initial value, and the branch prediction circuitry is arranged to increment the confidence indication in response to each misprediction instance.

[0039] In one example arrangement, the confidence indication value may be arranged so that it only increases, and so each misprediction instance will cause the value to be incremented, so that at some point in time, once a sufficient number of mispredictions have been detected, a trigger condition will be detected. However, in alternative arrangements, an equivalent decrementing mechanism may be employed if desired. For example, where the confidence indication is greater than an initial value, the branch prediction circuitry may be arranged to decrement the confidence indication in response to each correct prediction instance.

[0040] Therefore, through this mechanism, after the trigger condition is determined to exist, it is possible to subsequently evaluate that the return prediction mechanism is able to make a correct prediction again, so that the confidence indication is decremented to the point where the trigger condition no longer exists, and the branch prediction circuit switches back to using the return prediction mechanism. It should be noted that in order to keep the return prediction mechanism synchronized with respect to the number of linked branch instructions and the number of associated return instructions, it is still appropriate to remove entries from the return prediction mechanism even when another prediction mechanism is used to predict the target address for the return instruction when the prediction is made or when feedback is sent to the branch prediction circuit after the execution of the return instruction. Therefore, when providing feedback information, reference can be made to the target address that would be predicted if the return prediction mechanism was used to determine whether the return prediction mechanism would have correctly predicted the target address, and if so, the confidence counter can be decremented.

[0041] In one example arrangement, one of the plurality of branch prediction mechanisms provided by the branch prediction circuitry may include a branch target buffer (BTB) having a plurality of entries, each entry maintaining a memory address indication and a branch type indication to identify a branch type for a branch instruction predicted in association with the memory address indication, wherein the branch type indication is used by default to determine the branch prediction mechanism used to predict a target address for the branch instruction predicted for that entry. Thus, when a hit is detected within one of the entries of the branch target buffer, information stored therein may be used to determine which branch prediction mechanism was used to predict the target address. This may be, for example, the branch target buffer itself, but may be any of the other branch prediction mechanisms.

[0042] For example, if a hit occurs within an entry of the branch target buffer, and the branch type indication indicates that the branch instruction is a return instruction, the default return prediction mechanism will be used to determine the predicted target address. However, in the presence of a triggering condition, an alternate branch prediction mechanism will be used. Therefore, for a return instruction, the branch type indication in the hit entry of the branch target buffer is used in combination with the prediction control information to identify the appropriate branch prediction mechanism to use to predict the target address.

[0043] The prediction control information may be maintained in a variety of ways, but in one example arrangement, the branch target buffer is arranged to maintain the prediction control information in each entry where the predicted branch type is a return instruction.

[0044] In one example arrangement, the prediction control information is encoded with the branch type indication, such that in the absence of a trigger condition, the branch type indication identifies that a return prediction mechanism is to be used to predict a target address for an associated return instruction, and in the presence of a trigger condition, the branch type indication identifies that an alternate branch prediction mechanism is to be used to predict a target address for an associated return instruction. In particular, in one example arrangement, the number of bits provided for the branch type indication field is sufficient such that there are bits that would otherwise not be used for return instructions in a BTB entry, and these unused bits can be used to encode the prediction control information.

[0045] In one example arrangement, each entry in the branch target buffer includes a target address field to indicate a target address when a branch instruction is predicted in association with a memory address indication, and the branch target buffer includes an alternate branch prediction mechanism. Thus, in the event that a hit is detected within an entry of the branch target buffer for a return instruction and the prediction control information indicates that the alternate branch prediction mechanism should be used, the target address is obtained directly from the hit entry of the branch target buffer.

[0046] However, in some instances, it may be appropriate for the alternative branch prediction mechanism to have a form other than a branch target buffer. For example, in one arrangement, the alternative branch target prediction mechanism may be a multi-target branch target prediction mechanism for predicting target addresses for polymorphic branch instructions, that is, a branch target address that changes from time to time so that different execution instances of a branch instruction at the same instruction fetch address may result in different target addresses depending on the outcome of a previous instruction preceding the branch. A multi-target branch target prediction mechanism is a type of branch target prediction mechanism that attempts to make accurate predictions for polymorphic branch instructions.

[0047] As mentioned earlier, one example use case in which the above techniques may be useful is when the processing circuitry is arranged to execute software that employs a software construct to cause branch mispredictions when executing indirect branch instructions by causing the mispredicted target address to be obtained from the return prediction mechanism rather than via the expected branch prediction mechanism in a situation where the expected branch prediction mechanism is susceptible to exploitation to implement a branch target injection attack. However, once this vulnerability in the expected branch prediction mechanism has been addressed and the device running the software thus has a form of the expected branch prediction mechanism that is robust to branch target injection attacks, the performance issues caused by the software construct are no longer a price to pay in order to ensure security against attacks. However, according to the above techniques, the replacement branch prediction mechanism used in the presence of a trigger condition can be set to be the expected branch prediction mechanism for indirect branch instructions, and thus the use of the trigger condition enables the mispredictions caused by the software construct to be detected, and subsequent predictions can be made using the expected branch prediction mechanism, thereby restoring performance.

[0048] While the confidence indication mechanism described earlier may be used to detect mispredictions, in one particular arrangement using the software construct described above, the processing circuitry may include detection circuitry to detect the use of the software construct and indicate the detection to the branch prediction circuitry for use by the branch prediction circuitry in detecting the presence of a trigger condition. This may enable the trigger condition to be determined more quickly, thereby further improving performance.

[0049] As mentioned earlier, the above techniques may also be used in other situations where mispredictions using a return prediction mechanism may occur, such as when the processing circuit is executing software that includes multiple nested code portions and the multiple nested code portions are sufficient to cause an overflow condition in the return prediction mechanism. At a certain point in time, this will result in the absence of a relevant entry within the return prediction mechanism for a particular return instruction, thus meaning that the return prediction mechanism cannot provide a prediction for that particular return instruction, which is then considered a misprediction causing the trigger condition to be detected.

[0050] The return prediction mechanism can take many forms, but in one embodiment is a return stack. When the branch prediction circuit predicts a target address for a function call, such as when a linked branch instruction is encountered, it can calculate the return address to be used by the associated return instruction and push the return address onto the return stack. Subsequently, when a prediction is to be made for the associated return instruction, an entry can be popped from the return stack to identify the target address to be predicted.

[0051] Specific examples will now be described with reference to the accompanying drawings.

[0052] Figure 1An example of a data processing apparatus 2 having a processing pipeline including several pipeline stages is schematically illustrated. The pipeline includes a branch predictor 4 for predicting the outcome of a branch instruction and generating a series of fetch addresses of instructions to be fetched. A fetch stage 6 fetches instructions identified by the fetch addresses from an instruction cache 8. A decode stage 10 decodes the fetched instructions to generate control information for controlling subsequent stages of the pipeline. A rename stage 12 performs register renaming to map architectural register designators identified by the instructions to physical register designators identifying registers 14 provided in hardware. Register renaming may be useful for supporting out-of-order execution, as this may allow elimination of risk between instructions that specify the same architectural registers by mapping them to different physical registers in the hardware register file to increase the likelihood that the instructions may be executed in an order different from their program order in which they were fetched from the cache 8, which may improve performance by allowing later instructions to be executed while earlier instructions are waiting for operands to become available. The ability to map architectural registers to different physical registers may also facilitate rollback of architectural state in the event of a branch misprediction. Issue stage 16 queues instructions waiting to be executed until operands required to process these instructions are available in registers 14. Execute stage 18 executes the instructions to implement the corresponding processing operations. Writeback stage 20 writes the results of the executed instructions back to registers 14.

[0053] The execution stage 18 may include several execution units, such as a branch unit 21 for evaluating whether a branch instruction has been correctly predicted, an ALU (arithmetic logic unit) 22 for performing arithmetic or logical operations, a floating point unit 24 for performing operations using floating point operands, and a load / store unit 26 for performing load operations to load data from the memory system to the register 14 or performing storage operations to store data from the register 14 to the memory system. In this example, the memory system includes a level 1 instruction cache 8, a level 1 data cache 30, a level 2 cache 32 shared between data and instructions, and a main memory 34, but it will be appreciated that this is only one example of a possible memory hierarchy and other implementations may have more levels of cache or a different arrangement. The load / store unit 26 may use a translation lookaside buffer (TLB) 36 and the fetch unit 6 may use a translation lookaside buffer 37 to map the virtual addresses generated by the pipeline to physical addresses identifying locations within the memory system. It will be appreciated that Figure 1 The pipeline shown in is just one example, and other examples may have a different set of pipeline stages or execution units. For example, an in-order processor may not have a rename stage 12.

[0054] Figure 2 is shown in accordance with an example arrangement Figure 14. The branch prediction circuit includes a branch direction predictor (BDP) 100 for predicting whether a branch instruction is to be taken, and a branch target buffer (BTB) 105 for predicting a target address to which the branch instruction will redirect program execution if the branch instruction is taken. However, the BTB is only one of the branch prediction mechanisms for predicting the target address, and additional branch prediction mechanisms such as a multi-target branch target predictor 115 and a return address predictor 118 may also be provided, as will be discussed in more detail later.

[0055] A fetch queue 110 is provided to queue fetch addresses identifying blocks of program instructions to be fetched from the instruction cache 8 (note that in some cases the fetch queue may be considered to be part of the fetch stage 6 rather than part of the branch predictor 4, but the functionality will be the same). The address placed in the fetch queue 110 represents the address of the block of instructions to be fetched from the instruction cache 8, as derived from a previous prediction by the branch predictor. The unit of instructions fetched from the cache 8 in a block may be referred to as a "fetch block" and may have a certain default size, such as 16, 32 or 64 bytes, although in some cases, when the starting address from which a fetch is to be performed is not aligned with a natural fetch block boundary, the fetching of a partial fetch block may be performed at a size less than the default size.

[0056] The BDP 100, which may also be referred to as a branch history buffer or branch outcome predictor, records branch history information for predicting the taken / not taken outcome of branch instructions. Any known branch taken / not taken prediction scheme may be used for the BDP 100. For example, gshare or TAGE are examples of known branch direction prediction algorithms.

[0057] BTB 105 stores prediction information for several branch instructions and may include, for example, Figure 3 Multiple entries are schematically illustrated in FIG. Figure 3As shown in , each entry may include several fields. Specifically, field 150 may be provided to indicate a memory address indication for the entry, which may, for example, provide a tag corresponding to a portion of the instruction address for the branch instruction (and which may depend on other information, such as a context identifier or other identifier of the current execution environment). A valid field 155 may be set to identify that the entry is valid, and field 160 may be used to identify the type of branch instruction predicted to exist in association with the memory address indication 150. For example, the branch type information may identify whether the branch is a direct branch, an indirect branch, a function call (e.g., a link branch instruction), a function return (e.g., a return instruction), and the like. The branch type information is used to identify which of the various branch prediction mechanisms 105, 115, 118 will be used to predict the target address for the branch instruction, i.e., it identifies which mechanism will be used to identify the target address if the branch is assumed to be taken. For example, for some branch instructions, they may be conditional, and therefore whether the branch is taken will depend on the evaluation of these conditions, wherein the BDP 100 is used to make a prediction about whether the branch is taken. However, other types of branch instructions may be executed unconditionally. For example, it is typically the case that a return instruction will always be executed and a branch will always be taken, and therefore for all such occurrences of a return instruction a prediction of the target address is used.

[0058] For some types of branch instructions, the target address may be specified by a BTB entry, and thus a target address field 170 is provided to provide that target address information. A further field 175 is provided to capture other information, such as the instruction address (program counter or PC) of the corresponding branch instruction, some attributes about the branch, such as whether it is unconditional, and the like.

[0059] Of particular interest to the present disclosure is how return instructions are handled, and in particular how a prediction of a target address is made for such return instructions. By default, the return address predictor 118 is used to predict the target address, and so if a hit occurs within the BTB 105 for the supplied fetch address, and the entry indicates that the branch type is a return instruction, an entry will be retrieved from the return address predictor 118 to be used as the predicted return address.

[0060] The return address predictor can take many forms, but in one example is a return stack 118. Thus, according to this form of return address predictor, when a prediction is made for a function call (e.g., a linked branch instruction), the return address is calculated. In particular, it is typically the case that the return address will be the address of the instruction that follows the linked branch instruction. This return address is then pushed onto the return stack 118, because it is this address that will be used as the predicted target address of the associated return instruction when it is subsequently encountered during the prediction process. Thus, when a prediction is required for an associated return instruction, an entry is popped from the return stack 118 and forwarded to the branch prediction control logic 125.

[0061] The branch predictor 4 may also include a multi-target branch target predictor 115, which is a special kind of branch target buffer for predicting the target address of polymorphic branch instructions, that is, whose branch target address changes from time to time, so that different execution instances of branch instructions at the same instruction fetch address may result in different target addresses depending on the results of previous instructions before the branch. Branch history information may be input to the multi-target branch target predictor for determining the target address for such polymorphic branch instructions.

[0062] like Figure 2 As also shown in , the branch predictor 4 may include a region table 120 , which is used to compress the context identifier into a shorter region identifier, which may be used as tag information for the BTB 105 .

[0063] When the fetch stage 6 starts a new fetch for a given fetch address to fetch a block of instructions from the instruction cache 8, the branch predictor 4 also looks up the fetch address in the BDP 100, BTB 105, and multi-target branch target predictor 115. When the BTB 105 holds predicted branch information for the program counter address represented by the current fetch address, then the information is read from the BTB 105 and qualified by the BDP 100. This lookup can be controlled by the branch prediction control logic 125. When the BDP 100 predicts that the branch is not taken, the branch prediction control logic 125 selects an incremented version of the current fetch address incremented by a certain stride amount by the adder 130 as the next fetch address, so that the next fetch address used in the next cycle will be a sequential continuation from the current fetch address. On the other hand, if the BDP 100 predicts that the branch is taken, the branch prediction control logic 150 selects the target address determined from one of the branch target prediction mechanisms 105, 115, 118 as the next fetch address. In particular, if the branch instruction is of a type where the target address is provided by the BTB, the target address output by the BTB is used. Conversely, if the hit entry in the BTB indicates that the branch has been previously detected as polymorphic, the branch prediction control logic 125 is used to select the branch target address output by the multi-target branch target predictor 115 as the next fetch address rather than the output of the BTB 105. The multi-target branch target predictor bases its predicted target address on the branch history of earlier branches prior to the current point of execution identified by the current instruction fetch address, in contrast to the prediction made by the BTB 105 which is independent of the branch history.

[0064] As another example, if the hit entry in the BTB indicates that the branch instruction is a return instruction, then by default the entry may be popped from the return stack and the branch prediction control logic 125 will use the target address as the next fetch address.

[0065] The next fetch address output by the branch prediction control logic 125 is distributed to the fetch queue 110 so that, in due time, when the address reaches the front of the queue, the corresponding instruction block is fetched from the instruction cache by the fetch stage 6. Additionally, the next fetch address is input as the current fetch address in the next processing cycle to trigger another lookup of the fetch address by the branch predictor 4. This process continues cycle by cycle in order to step through the program code being executed. If Figure 1 When a branch misprediction is detected at the branch unit 21, when the actual result of the branch instruction is different from the predicted result generated by the branch predictor 4, a signal is sent back to the acquisition stage 6 to reset the acquisition queue and continue to start acquiring from the actual branch target address, and based on the actual result of the branch, the contents of the various prediction structures 100, 105, 115, 118 are updated to increase the possibility of the prediction being correct in the future.

[0066] In summary, the BTB 105 (which may also be referred to as a branch target address cache or BTAC) is a component of the branch predictor 4 that is the primary mechanism for identifying the predicted target address of a branch, either by directly identifying the target address or by identifying which other branch prediction mechanism 115, 118 should be used to identify the predicted target address. Thus, the BTB may actually be a small cache of entries, where each entry provides information about the branch location, the type of branch predicted at that location, and certain other attributes, such as the target address in the target address field 170. Since different execution contexts (e.g., different processes executed by the processor 2, or different virtual machines) may use the same virtual address to reference different branches, one way to avoid unnecessary address conflicts is to tag each BTB entry with context information such as a process identifier and / or a virtual machine identifier.

[0067] The cache provided for the BTB may be implemented in different ways. In some examples, the cache should be fully associative, so that branch information for a given branch can be placed anywhere within the BTB. However, in practice, a set associative cache implementation may be more efficient, and in this case, the locations where branch information for a given branch is allowed to be stored may be limited to a specific set (identified based on the instruction fetch address of the instruction block containing the branch) in order to reduce the number of entries that must be looked up during a branch target buffer lookup for a given fetch address.

[0068] Typically, the BTB may have a relatively large number of entries, such as on the order of about 1000, so that sufficient branch information can be stored to provide sufficiently high performance. However, in practice, the number of contexts for which information is stored in the BTB at a given time may be much smaller, such as up to 10, because one execution context may cache branch information for many instructions of the context. In addition, many of the branch instructions associated with a given context may all share the same value for the more significant portion of the branch instruction address. As a result, it is generally considered inefficient to attempt to store all context ID information associated with each individual entry, and instead, in order to improve the area efficiency of the BTB, the branch predictor 4 may use a region table 120 to compress this redundant information into a shorter value. In this way, the memory address indication field 150 may be arranged to store only the lower portion of the tag portion of the branch instruction address, and the region identifier pointing to the corresponding region entry of the higher portion of the specified context identifier and the branch instruction address in the region table. The region table 120 may be much smaller than the BTB 105, for example, having 10 to 100 entries.

[0069] Thus, this enables the size of the memory address indication field of a BTB entry to be significantly reduced by using region table 120. This exploits the fact that it is not necessary to distinguish the current execution context from every other execution context in order to look up in BTB 105, instead all that is required is to distinguish the current execution context from any other execution context that currently has branch information in the BTB.

[0070] However, through the use of such a structure, this can lead to some false hits, where the branch predictor determines that a branch from one execution context matches branch information in a BTB entry assigned by a different execution context. This is a mechanism that can therefore be exploited when attempting to implement the branch target injection attack mentioned earlier, where untrusted code attempts to train the branch predictor mechanism so that it causes other code to perform speculative memory accesses. In particular, the BTB can be susceptible to such training mechanisms, which then later opens up the possibility of cache timing side channel analysis attempting to obtain information about data that the untrusted code should not be able to access.

[0071] Thus, it has been recognized that an attacker may be able to use the above-described properties of the BTB to control the speculative execution of another execution context that is not under the attacker's control to expose information about secret data managed by that process. This is possible if both of the following conditions are met:

[0072] Condition 1: Process A may use a target address provided by another process B in the BTB 105 .

[0073] Condition 2: Process B can control the target address in the BTB entry accessed by process A.

[0074] For later reference Figure 8B As will be discussed in greater detail, a Retpoline software construct has been developed that can be used to effectively prevent such a compromised BTB from being used to predict a target address for an indirect branch instruction, and instead, the prediction is made using the contents of the return stack 118. However, due to the way the software construct is configured, the prediction made from the return stack is a misprediction, which will later be detected by the branch unit 21 within the execute stage 18 of the CPU pipeline. While this scheme protects against branch target injection attacks, it has a significant performance impact due to the mispredictions that occur.

[0075] It is recognized that in future processors, techniques will be developed to make the BTB 105 robust against such branch target injection attacks. However, at this stage, there may be legacy code that still uses the Retpoline software construction scheme to attempt to protect against branch target injection attacks. Considering that the BTB itself is robust to attacks, this software construction is no longer necessary and will significantly affect performance. However, as described below, the present technology provides a mechanism to detect mispredictions that occur about the return stack, and in this case switch back to using the expected branch prediction mechanism, i.e., in this case, the BTB 105, in order to restore performance.

[0076] In particular, return Figure 3 , an additional field 165 is provided which may store prediction control information for determining which branch prediction mechanism is used to provide a predicted target address in the event that the branch type stored in the branch type field 160 identifies the branch as a return instruction. By default, the return stack 118 will be used in this case. However, if the prediction control information has a specific form indicating the presence of a triggering condition, this will cause an alternative prediction structure to be used to identify the target address. This may, for example, be the BTB entry itself, using the target address in field 170, or may be a multi-target branch target predictor 115.

[0077] The prediction control information may take a variety of forms and may be updated in a variety of ways. However, typically the prediction control information will be updated based on information fed back from the execution stage 18 indicating whether a misprediction has been observed using the target address information provided by the prediction circuitry.

[0078] Figure 4 2 is a flow chart illustrating a process for predicting a return instruction according to an example arrangement. At step 200, a determination is made as to whether the BTB 105 has received a new fetch address. If so, a lookup is performed within the BTB and a determination is made at step 205 as to whether there is a hit within the BTB for an entry associated with a return instruction, i.e., where the branch type field 160 identifies that the branch within the hit entry is a return instruction. If not, then at step 210 normal branch prediction behavior occurs, as described earlier with reference to Figure 2 Thus, whenever there is a miss in the BTB, or a hit for a branch instruction of a branch type that is not a return instruction, the process proceeds to step 210 where the normal behavior of the branch prediction circuitry occurs.

[0079] However, if a hit is detected in the BTB entry associated with the return instruction at step 205, then at step 215, a determination is made as to whether the prediction control information within the entry indicates that a trigger condition exists. If the trigger condition does not exist, the process proceeds to step 220 where, again, normal branch prediction behavior occurs. In this case, the branch prediction control logic 125 pops the target address from the return stack 118 and uses the target address as the predicted target address for the return instruction.

[0080] However, if, at step 215, the prediction control information indicates the presence of a trigger condition, then, at step 225, a prediction mechanism other than the return stack is used to predict a target address for the return instruction. In this case, information output by the BTB can be used to identify which of the other branch prediction mechanisms are to be used for this purpose. For example, the information can identify that a target address output by the BTB itself should be used, or can identify that an output from the multi-target branch target predictor 115 should be used. It should be noted that in order to keep the return stack synchronized with respect to the number of linked branch instructions and the number of associated return instructions, it is still appropriate to pop entries from the return stack even when another prediction mechanism is used to predict the target address at prediction time or when feedback is sent to the branch prediction circuit after execution of the return instruction.

[0081] Figure 5 1 is a flow chart illustrating how prediction control information may be updated in one example arrangement, assuming in this example that the prediction control information takes the form of a confidence indication. At step 250, it is assumed that an entry is allocated in the BTB for a return instruction, and at step 255, the confidence indication within field 165 is then initialized, for example by being set to a value of zero. It is then assumed that a prediction is made for the return instruction, and at some point in the future, the return instruction is subsequently executed by the execution pipeline, resulting in feedback information being provided from the execution pipeline at step 260. The feedback information will, for example, identify whether the predicted target address is correct. Thus, at step 265, it may be determined from the feedback information whether a misprediction has occurred based on the target address provided by the prediction circuitry for the return instruction. If so, the confidence indication value is incremented at 270 and the process returns to step 260. If no misprediction has occurred, in one arrangement, the process may simply return to step 260. However, as indicated at step 275, an optional step is to decrement the confidence indication after a correct prediction, if the confidence indication is currently non-zero, i.e., if it is not at the initialization value set at step 255.

[0082] Figure 6 The diagram shows that when using Figure 5In step 300, it is determined whether the confidence indication is below a selected threshold. If so, then in step 305, it is determined that the trigger condition does not exist. However, if the confidence indication is not below the threshold, then in step 310, it is determined that the trigger condition exists. As described earlier, when the trigger condition does not exist, the return stack 118 will continue to be used as a prediction mechanism for predicting the target address for the return instruction. However, when the trigger condition exists, the replacement prediction mechanism will be used to predict the target address for the return instruction.

[0083] The threshold amount considered at step 300 may be changed depending on the implementation. For example, in one particular implementation, it may be determined that once the confidence indicator value is non-zero, the threshold is reached, i.e., a single occurrence of a misprediction is sufficient to switch from the absence of a trigger condition to the presence of a trigger condition. However, if desired, the threshold may be set at a higher level such that multiple occurrences of mispredictions are required before determining that a trigger condition exists.

[0084] In one example arrangement, in the presence of a trigger condition, the replacement branch prediction mechanism is initially set to be BTB 105, and therefore the branch target address within field 170 of the hit entry will be used as the next predicted target address of the return instruction. Figure 7 As shown, in an example arrangement, if more mispredictions occur, the replacement branch prediction mechanism may be changed. In particular, at step 350, once it is determined that the trigger condition exists, the BTB is initially selected as the replacement branch prediction mechanism. However, at step 355, it is then determined whether a misprediction of the target address occurs using the BTB entry. If not, this indicates that the target address is static and the BTB is correctly identifying the target address. Therefore, as indicated at step 360, the process continues to use the BTB to determine the target address for the next instance of the return instruction (i.e., when the return instruction at the same fetch address is encountered the next time). However, if another misprediction does occur when the BTB is used to predict the target address, this may indicate that the return instruction is a polymorphic situation, and in this case the process proceeds to step 365, in which a switch is made to use a multi-target branch target predictor as the replacement branch prediction mechanism. The return instruction may be polymorphic for several reasons, for example because there may be many chained branch instructions associated with the same return instruction (wherein the function may be called from different places in the code). Similarly, there may be many return instructions associated with the same linked branch instruction (in the case where a function may have multiple exit points).

[0085] Fig. 8Ais a diagram schematically illustrating the operation of calling a function, in this case the calling function is implemented by a branch with link instruction (BL). If the branch with link instruction is predicted to be taken (note that the execution of the branch can be conditional), the incremented address is stored in the link register as the return address. In this case, it is assumed that the branch with link instruction is at address "X" and therefore the address stored in the link register is "X+1", where the unit of addressing is assumed to be the instruction length. The target address of the branch with link instruction is in this case determined from the contents of register Xn and it is assumed in this case that the determined target address is address "X+Y". It is then assumed that a function is executed, where the first instruction in the function is at address X+Y, and at the end of the function, a return instruction is used to return to address X+1.

[0086] Figure 8B The Retpoline software construct that can be used to protect against branch target injection attacks when executing indirect branch instructions is illustrated. In particular, for indirect branch instructions, normally the BTB would be used to predict the target address, but the software construct is such that instead, the prediction comes from the return stack and is a misprediction, and thus this effectively isolates the performance of the indirect branch operation from speculative execution.

[0087] like Figure 8B As shown in , the branch instruction (BR) is replaced by a branch with link instruction (BL) that branches to address "SET_UP_TARGET". With respect to the return address, the link register is initially set to address "CAPTURE_SPEC" when the branch with link instruction is executed, and this address is also pushed onto the return stack when the branch with link instruction is encountered within the prediction circuitry. For the branch with link instruction itself, this is predicted to be taken, with the known target address SET_UP_TARGET, but when prediction is requested for the associated return instruction, address "CAPTURE_SPEC" will be popped from the return stack. As a result, speculative execution will begin at address "CAPTURE_SPEC" and as if from Figure 8B As will be seen, this introduces a loop in which the code continually branches to itself during speculative execution. When the branch with link instruction is subsequently executed, the process branches to the SET_UP_TARGET address, where a move (MOV) instruction is used to update the link register (in this case register X30) to the correct target address based on the contents of register Xn. The return instruction is then executed, causing the process to return to the desired target address of the original branch instruction.

[0088] It will be appreciated that this effectively prevents speculative execution from being used to cause branch target injection attacks by training the BTB, since the BTB is not used during speculative execution, and instead incorrect branch predictions are made using the contents of the return stack. However, it does have a significant performance impact, since when the return instruction is executed in due course, it will be determined that the misprediction was wrong, and therefore corrective steps will be required to fetch instructions from the required target address.

[0089] However, as will be apparent from the earlier discussion, this misprediction will be detected by the branch unit within the execute stage 18, and this misprediction information may be returned to the branch predictor 4 to cause the prediction control information 165 to be updated for the return instruction.

[0090] Thus, when it is known that the expected branch prediction mechanism for the original branch instruction - in this case, the BTB 105 - is constructed in a manner that is robust to branch target injection attacks, then the misprediction information returned from the execute stage of the pipeline can be used to adjust the prediction control information so that the trigger condition will then be detected at some point in time (as described earlier, in some cases a single misprediction may be sufficient to cause the trigger condition to be detected), and the branch prediction circuitry can then switch to using the alternate branch prediction mechanism to predict the target address. In particular, for subsequent iterations of the return instruction, rather than predicting the address "CAPTURE_SPEC" based on the contents of the return stack, the BTB can be used to predict the appropriate target address based on the contents of register Xn so that another misprediction does not occur in association with the return instruction.

[0091] While in one example arrangement, feedback about mispredictions may be used to adjust the confidence indicator, in another example arrangement, the branch circuitry 21 may be arranged to specifically detect the use of a software construct, and indicate the presence of the software construct to the branch prediction circuitry for use by the branch prediction circuitry in detecting the presence of a trigger condition. This will therefore enable earlier detection of the use of the software construct, and therefore enable an earlier switch to use of an alternative branch prediction mechanism.

[0092] Fig. 9Another example scenario is illustrated in which the ability of the return stack to accurately predict return instructions may be compromised. In this case, it is assumed that the code executed on the processing circuit includes a considerable number of nested code portions. Each code portion is initiated by a function call using a linked branch instruction and then terminated by a function return implemented using a return instruction. Therefore, the initial BL instruction 400 has an associated return instruction 405. Similarly, each other nested routine has a BL instruction and an associated return instruction. Each time a BL instruction is encountered by the branch prediction circuit, the return address (i.e., the next sequential address after the linked branch instruction) is pushed onto the return stack. If there are enough entries in the return stack, it will be seen that once the final linked branch (BL) instruction 410 is encountered, the return stack will contain all the required target addresses of the corresponding return instruction. Due to the nested nature of the code portions, it will be seen that the return instruction associated with the subsequent linked branch instruction (these return instructions are referred to as subsequent return instructions in this article) will actually be encountered first in the branch prediction circuit, and the relevant target address can be obtained by popping the top entry from the return stack in each case. However, if one of the linked branch instructions is encountered later (assuming this is Fig. 9 ), all entries in the return stack are currently filled, then pushing the target address of the associated return (RET) instruction 415 onto the return stack will cause the target address of the oldest return instruction (i.e., instruction 405) to be overwritten (this is called an overflow condition).

[0093] As a result, when the return instruction 405 is encountered at the appropriate time, the return stack will have no valid entries left, and therefore the target address cannot be predicted. This indicates the state of misprediction using the return stack, and at this point in time, a trigger condition can be raised so that the prediction of the target address is obtained from the replacement branch prediction mechanism (e.g., BTB). In one implementation, once it is determined that the return stack has no valid entries, a trigger condition can be raised to cause a switch to the replacement branch prediction mechanism at this point in time. However, if this is the first time this happens, it is possible that the target address associated with the return instruction will not be found in the replacement branch prediction mechanism (in one example, the BTB), and therefore using the replacement branch prediction mechanism, misprediction will occur for the return instruction for the first time. However, this will cause the allocation of the target address to be made in the BTB, so that subsequent predictions for the return instruction can be accurately made using the BTB content.

[0094] This process Fig.10. In particular, at step 450, it is determined whether an overflow condition has occurred for the return stack. As mentioned earlier, this will occur when the degree of nesting is high enough that target address information that has not yet been consumed by the associated return instruction is overwritten by the new target address information of the later return instruction. Thus, in the event of an overflow condition at step 450, the target address of the old pending return is overwritten by the target address of the newer pending return. As mentioned earlier with reference to Fig. 9 As discussed, due to the nested nature of the code sections, the newer pending returns are actually executed first.

[0095] At step 460, when appropriate, the return stack will be empty when the target address is predicted for the old pending return whose target address is overwritten, so that the trigger condition is detected and the alternate branch prediction mechanism is used to predict the target address.

[0096] Therefore, in Fig.10 In the example shown, the trigger condition is detected when the return stack is empty when the target address is to be predicted for the return instruction. In an example arrangement, if the same return instruction is encountered in the future, and the return stack is not empty at that time (meaning that no overflow has occurred at this time), the trigger condition will not be detected and the return stack will be used to predict the target address as normal.

[0097] From the above examples, it can be seen that when a trigger condition is detected, the alternative branch prediction mechanism can be used to make a prediction for the current instance of the return instruction, or to make a prediction for one or more subsequent instances of the return instruction.

[0098] The decision about when to fill the replacement branch prediction mechanism so that the target address prediction can be made for the return instruction can be changed depending on the implementation. In fact, considering any specific implementation situation, there is a compromise that can be coordinated. The faster the target address information is distributed in the replacement branch prediction mechanism, the faster the target address information of this distribution can be used to make correct predictions when the trigger condition is detected. However, if this target address information is even filled in the replacement branch prediction mechanism before the trigger condition exists, it is possible that this information will not be needed, and therefore the distribution of this information can pollute the replacement branch prediction mechanism by wasting the space that can be used to make predictions with respect to other branch instructions.

[0099] In an example implementation, the BTB will already have an entry for the return instruction, and will recognize by default that the return stack should be used for the target address. However, even before the trigger condition is detected, the target address field 170 in the BTB can be filled with the predicted target address, so that once the trigger condition is detected, the BTB can predict the target address for the current or subsequent instance of the return instruction. In the case where the multi-target branch target predictor 115 will be used as a replacement branch prediction mechanism, a decision can be made about the following point in time: at this point in time, entries are allocated to the multi-target branch target predictor for the relevant return instruction so that it can begin to provide the target address when called. It is possible that the taking of this step will not be significantly ahead of the trigger condition being detected, because it is not desirable to unnecessarily utilize the entries of the multi-target branch target predictor. In fact, in some cases, the entries in the multi-target branch target predictor will not be made until after the trigger condition is detected.

[0100] It has been discovered that the above-described techniques may provide a mechanism for improving the efficiency of branch prediction in certain situations.

[0101] In this application, the word "configured to" is used to mean that elements of an apparatus have a configuration that is capable of performing a defined operation. In this context, "configuration" refers to the arrangement or interconnection of hardware or software. For example, an apparatus may have dedicated hardware that provides a defined operation, or a processor or other processing device may be programmed to perform the function. "Configured to" does not mean that the elements of the apparatus need to be changed in any way in order to provide the defined operation.

[0102] Although illustrative embodiments of the present invention have been described in detail herein with reference to the accompanying drawings, it is to be understood that the present invention is not limited to these exact embodiments and that various changes, additions and modifications may be implemented therein by those skilled in the art without departing from the scope and spirit of the present invention as defined in the appended claims. For example, the features of the dependent claims may be variously combined with the features of the independent claims without departing from the scope of the present invention.

Claims

1. A device for controlling branch prediction, comprising: processing circuitry for executing instructions; as well as branch prediction circuitry including a plurality of branch prediction mechanisms for predicting a target address for a branch instruction to be executed by the processing circuitry, the branch instruction including a plurality of branch types, one of the branch types being a return instruction; wherein the branch prediction mechanism comprises a return prediction mechanism, the return prediction mechanism comprising a return stack, the return stack being used by default to predict a target address when a return instruction is detected by the branch prediction circuitry, wherein each target address pushed to the return stack is a return address calculated for a function call and forms an entry on the return stack, and each time the return stack is used to predict a target address of a return instruction detected by the branch prediction circuitry, the return stack is arranged to determine the predicted target address of the return instruction by popping an entry from the return stack; The branch prediction circuit switches to using an alternative branch prediction mechanism to predict a target address for at least one subsequent instance of a given return instruction in response to a trigger condition indicating a misprediction of the target address when using entries popped from the return stack to predict the target address for one or more prior instances of the given return instruction.

2. An apparatus as claimed in claim 1, wherein the branch prediction circuit is arranged to maintain prediction control information for one or more return instructions, and for each return instruction, the prediction control information identifies whether the trigger condition exists, thereby controlling for each return instruction whether the target address of the return instruction is predicted using the return prediction mechanism or the replacement branch prediction mechanism.

3. Apparatus as claimed in claim 2, wherein when the processing circuitry executes the given return instruction, the processing circuitry is arranged to determine whether the target address determined by the branch prediction circuitry is correct and to provide feedback information to the branch prediction circuitry indicating whether the target address determined by the branch prediction circuitry is correct, the branch prediction circuitry being arranged to use the feedback information to maintain the prediction control information.

4. The device as claimed in claim 2, wherein: the prediction control information includes a confidence indication indicating, for an associated return instruction, a level of mispredictions observed when using the return prediction mechanism to predict a target address for the associated return instruction; and The trigger condition exists when the level of misprediction indicated by the confidence indication reaches a selected level of misprediction.

5. An apparatus as claimed in claim 4, wherein the selected level of misprediction comprises a single instance of misprediction confirmed by the processing circuitry for the associated return instruction, such that following the single instance of misprediction, the confidence indication causes the branch prediction circuitry to use the alternate branch prediction mechanism to predict a target address for at least one subsequent instance of the associated return instruction.

6. Apparatus as claimed in claim 4, wherein the selected level of misprediction comprises a determined plurality of instances of misprediction identified by the processing circuitry for the associated return instruction, and for each misprediction instance, the branch prediction circuitry is arranged to adjust the value of the confidence indication such that, following the determined plurality of instances of misprediction, the confidence indication has a value indicative of the trigger condition to cause the branch prediction circuitry to predict a target address for at least one subsequent instance of the associated return instruction using the alternative branch prediction mechanism.

7. Apparatus as claimed in claim 6, wherein the confidence indication is initialised to an initial value and the branch prediction circuitry is arranged to increment the confidence indication in response to each misprediction instance.

8. The device as claimed in claim 7, wherein: Where the confidence indication is greater than the initial value, the branch prediction circuitry is arranged to decrement the confidence indication in response to each correct prediction instance.

9. Apparatus as claimed in any one of claims 2 to 8, wherein: The plurality of branch prediction mechanisms include a branch target buffer having a plurality of entries, each entry maintaining a memory address indication and a branch type indication to identify a branch type of a branch instruction predicted in association with the memory address indication, wherein the branch type indication is used by default to determine a branch prediction mechanism for predicting a target address for the branch instruction predicted by the entry.

10. Apparatus as claimed in claim 9, wherein the branch target buffer is arranged to maintain the prediction control information in each entry for which the predicted branch type is a return instruction.

11. An apparatus as claimed in claim 10, wherein the prediction control information is encoded with the branch type indication such that in the absence of the trigger condition, the branch type indication identifies that the return prediction mechanism is to be used to predict a target address for the associated return instruction, and in the presence of the trigger condition, the branch type indication identifies that the replacement branch prediction mechanism is to be used to predict a target address for the associated return instruction.

12. Apparatus as claimed in claim 9, wherein each entry in the branch target buffer includes a target address field to indicate a target address when a branch instruction is predicted in association with the memory address indication, and the branch target buffer includes the alternative branch prediction mechanism.

13. Apparatus as claimed in any one of claims 1 to 8, wherein the alternative branch prediction mechanism is a multi-target branch target prediction mechanism for predicting target addresses of polymorphic branch instructions.

14. Apparatus as claimed in any one of claims 1 to 8, wherein: The processing circuitry is arranged to execute software employing a software construct for causing a branch misprediction when executing an indirect branch instruction by causing a mispredicted target address to be obtained from the return prediction mechanism rather than via the expected branch prediction mechanism in a situation where the expected branch prediction mechanism is susceptible to exploitation to effectuate a branch target injection attack; The branch prediction circuit includes the desired branch prediction mechanism but is configured to be robust to the branch target injection attack; and The alternative branch prediction mechanism includes the desired branch prediction mechanism, and use of the trigger condition enables mispredictions caused by the software construct to be detected and subsequent predictions to be made using the desired branch prediction mechanism.

15. Apparatus as claimed in claim 14, wherein the processing circuitry comprises detection circuitry to detect use of the software construct and to indicate the detection to the branch prediction circuitry for use by the branch prediction circuitry in detecting the presence of the trigger condition.

16. Apparatus as claimed in any one of claims 1 to 8, wherein: The processing circuitry is arranged to execute software comprising a plurality of nested code portions, each nested code portion having an associated return instruction; the plurality of nested code portions being sufficient to cause an overflow condition in the return prediction mechanism wherein at least one target address of an older pending return instruction is overwritten with a target address of a later pending return instruction; Wherein when a prediction of the target address is to be made for the older pending return instruction, if no relevant entry exists within the return prediction mechanism, then the trigger condition is detected, thereby causing the replacement branch prediction mechanism to be used to predict the target address.

17. A method of controlling branch prediction in an apparatus having processing circuitry for executing instructions, the method comprising: providing branch prediction circuitry, the branch prediction circuitry comprising a plurality of branch prediction mechanisms, the plurality of branch prediction mechanisms for predicting a target address for a branch instruction to be executed by the processing circuitry, the branch instruction comprising a plurality of branch types, one of the branch types being a return instruction; when a return instruction is detected by the branch prediction circuitry, a return prediction mechanism including a return stack among the plurality of branch prediction mechanisms is used by default to predict a target address, wherein each target address pushed to the return stack is a return address calculated for a function call and forms an entry on the return stack, and each time the return stack is used to predict a target address of a return instruction detected by the branch prediction circuitry, the return stack is arranged to determine a predicted target address of the return instruction by popping an entry from the return stack; and switching to using an alternative branch prediction mechanism among the plurality of branch prediction mechanisms to predict a target address for at least one subsequent instance of a given return instruction in response to a trigger condition indicative of a misprediction of a target address when the return prediction mechanism used entries popped from the return stack to predict target addresses for one or more prior instances of the given return instruction.

18. An apparatus for controlling branch prediction, comprising: a processing device for executing instructions; as well as a branch prediction device, comprising a plurality of branch prediction mechanisms, the plurality of branch prediction mechanisms being used to predict a target address for a branch instruction to be executed by the processing device, the branch instruction comprising a plurality of branch types, one of the branch types being a return instruction; wherein the branch prediction mechanism comprises a return prediction mechanism, the return prediction mechanism comprising a return stack, the return stack being used by default to predict a target address when a return instruction is detected by the branch prediction means, wherein each target address pushed to the return stack is a return address calculated for a function call and forms an entry on the return stack, and each time the return stack is used to predict a target address of a return instruction detected by the branch prediction means, the return stack is arranged to determine the predicted target address of the return instruction by popping an entry from the return stack; The branch prediction apparatus switches to using an alternative branch prediction mechanism to predict a target address for at least one subsequent instance of a given return instruction in response to a trigger condition indicative of a misprediction of a target address when the return prediction mechanism used entries popped from the return stack to predict target addresses for one or more prior instances of the given return instruction.